Skip to content

Commit a9d170b

Browse files
committed
-
1 parent 05117c0 commit a9d170b

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

python_toolbox/cute_iter_tools.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,29 @@ def get_consecutive_subsequences(iterable, length=2, wrap_around=False):
3636
iterator = iter(iterable)
3737

3838
first_items = get_items(iterator, length)
39-
if wrap_around:
40-
if len(first_items) < length:
39+
if len(first_items) < length:
40+
if wrap_around:
4141
raise NotImplementedError(
4242
'`length` is greater than the length of the iterable, and '
4343
'`wrap_around` is set to `True`. Behavior for this is not '
4444
'implemented, because it would require repeating some members '
4545
'more than once.'
4646
)
47+
else:
48+
raise StopIteration
49+
50+
if wrap_around:
4751
first_items_except_last = first_items[:-1]
4852
iterator = itertools.chain(iterator, first_items_except_last)
4953

5054
deque = collections.deque(first_items)
5155
yield first_items
5256

53-
if not wrap_around:
54-
# If `wrap_around` is `False`, we avoid holding a reference to items in
55-
# `first_items`, because they may need to be garbage-collected:
56-
del first_items
57+
# Allow `first_items` to be garbage-collected:
58+
del first_items
59+
# (Assuming `wrap_around` is `True`, because if it's `False` then all the
60+
# first items except the last will stay saved in
61+
# `first_items_except_last`.)
5762

5863
for current in iterator:
5964
deque.popleft()

0 commit comments

Comments
 (0)