Skip to content

Commit 1b3fed5

Browse files
committed
-
1 parent 637d8d9 commit 1b3fed5

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

python_toolbox/comparison_tools.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88

99
def underscore_hating_key(string):
10+
'''Key function for sorting that treats `_` as last character.'''
1011
assert isinstance(string, basestring)
1112
return unicode(string).replace('_', unichr(sys.maxunicode))
1213

python_toolbox/cute_iter_tools.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ def consecutive_pairs(iterable, wrap_around=False):
3838
# `first_item`, because it may need to be garbage-collected:
3939
del first_item
4040

41+
try:
42+
second_item = iterator.next()
43+
except StopIteration:
44+
if wrap_around:
45+
yield (first_item, first_item)
46+
raise StopIteration
47+
48+
current = second_item
49+
yield (old, current)
50+
old = current
51+
4152
for current in iterator:
4253
yield (old, current)
4354
old = current

test_python_toolbox/test_cute_iter_tools/test_consecutive_pairs.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,8 @@ def test():
2424

2525
assert tuple(consecutive_pairs('meow')) == \
2626
(('m', 'e'), ('e', 'o'), ('o', 'w'))
27+
28+
assert tuple(consecutive_pairs([1], wrap_around=True)) == \
29+
((1, 1),)
2730

2831

0 commit comments

Comments
 (0)