Skip to content

Commit 4cce436

Browse files
committed
-
1 parent d173850 commit 4cce436

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

source_py3/python_toolbox/cute_iter_tools.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
import itertools
1010
import builtins
1111

12-
from python_toolbox import nifty_collections
13-
1412
infinity = float('inf')
1513

1614

@@ -32,10 +30,11 @@ def iterate_overlapping_subsequences(iterable, length=2, wrap_around=False,
3230
If `lazy_tuple=True`, returns a `LazyTuple` rather than an iterator.
3331
'''
3432
iterator = _iterate_overlapping_subsequences(
35-
length=length, wrap_around=wrap_around
33+
iterable=iterable, length=length, wrap_around=wrap_around
3634
)
3735

3836
if lazy_tuple:
37+
from python_toolbox import nifty_collections # Avoiding circular import.
3938
return nifty_collections.LazyTuple(iterator)
4039
else:
4140
return iterator
@@ -95,6 +94,7 @@ def shorten(iterable, n, lazy_tuple=False):
9594
iterator = _shorten(iterable=iterable, n=n)
9695

9796
if lazy_tuple:
97+
from python_toolbox import nifty_collections # Avoiding circular import.
9898
return nifty_collections.LazyTuple(iterator)
9999
else:
100100
return iterator
@@ -117,7 +117,7 @@ def _shorten(iterable, n):
117117
raise StopIteration
118118

119119

120-
def enumerate(reversible, reverse_index=False):
120+
def enumerate(reversible, reverse_index=False, lazy_tuple=False):
121121
'''
122122
Iterate over `(i, item)` pairs, where `i` is the index number of `item`.
123123
@@ -131,6 +131,7 @@ def enumerate(reversible, reverse_index=False):
131131
iterator = _enumerate(reversible=reversible, reverse_index=reverse_index)
132132

133133
if lazy_tuple:
134+
from python_toolbox import nifty_collections # Avoiding circular import.
134135
return nifty_collections.LazyTuple(iterator)
135136
else:
136137
return iterator
@@ -179,6 +180,7 @@ def iter_with(iterable, context_manager, lazy_tuple=False):
179180
iterator = _iter_with(iterable=iterable, context_manager=context_manager)
180181

181182
if lazy_tuple:
183+
from python_toolbox import nifty_collections # Avoiding circular import.
182184
return nifty_collections.LazyTuple(iterator)
183185
else:
184186
return iterator
@@ -254,6 +256,7 @@ def make_false_iterator():
254256
iterators = (make_true_iterator(), make_false_iterator())
255257

256258
if lazy_tuple:
259+
from python_toolbox import nifty_collections # Avoiding circular import.
257260
return tuple(map(nifty_collections.LazyTuple, iterators))
258261
else:
259262
return iterators
@@ -297,6 +300,7 @@ def fill(iterable, fill_value=None, fill_value_maker=None, length=infinity,
297300
length=length)
298301

299302
if lazy_tuple:
303+
from python_toolbox import nifty_collections # Avoiding circular import.
300304
return nifty_collections.LazyTuple(iterator)
301305
elif sequence_type is None:
302306
return iterator

source_py3/test_python_toolbox/test_cute_iter_tools/test_double_filter.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Copyright 2009-2014 Ram Rachum.
22
# This program is distributed under the MIT license.
33

4+
from python_toolbox import nifty_collections
5+
46
from python_toolbox.cute_iter_tools import double_filter
57

68

@@ -15,4 +17,17 @@ def test_double_filter():
1517
double_filter(lambda value: value % 3 == 0, range(20))
1618
assert tuple(first_iterable) == tuple(range(0, 20, 3))
1719
assert tuple(second_iterable) == tuple(i for i in range(20) if i % 3 != 0)
20+
21+
(first_lazy_tuple, second_lazy_tuple) = \
22+
double_filter(lambda value: value % 3 == 0, range(20), lazy_tuple=True)
23+
24+
assert isinstance(first_lazy_tuple, nifty_collections.LazyTuple)
25+
assert isinstance(second_lazy_tuple, nifty_collections.LazyTuple)
26+
assert first_lazy_tuple.collected_data == \
27+
second_lazy_tuple.collected_data == []
28+
29+
assert first_lazy_tuple == nifty_collections.LazyTuple(range(0, 20, 3))
30+
assert second_lazy_tuple == nifty_collections.LazyTuple(
31+
i for i in range(20) if i % 3 != 0
32+
)
1833

0 commit comments

Comments
 (0)