Skip to content

Commit 359e444

Browse files
committed
API : make __iter__ be finite
It is far simpler for users that want infinite cycling to wrap the ``Cycler`` in a call to `itertools.cycle` than it is for the user that wants a finite iterator to sort out how to get the finite version. This simplifies the API and makes it a bit more guessable/intuitive.
1 parent 1ba8de6 commit 359e444

File tree

2 files changed

+9
-32
lines changed

2 files changed

+9
-32
lines changed

cycler.py

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
unicode_literals)
33

44
import six
5-
from itertools import product, cycle
5+
from itertools import product
66
from six.moves import zip
77
from operator import mul
88
import copy
@@ -58,37 +58,12 @@ def __init__(self, left, right=None, op=None):
5858
def keys(self):
5959
return set(self._keys)
6060

61-
def finite_iter(self):
62-
"""
63-
Return a finite iterator over the configurations in
64-
this cycle.
65-
"""
66-
if self._right is None:
67-
try:
68-
return self._left.finite_iter()
69-
except AttributeError:
70-
return iter(self._left)
71-
return self._compose()
72-
73-
def to_list(self):
74-
"""
75-
Return a list of the dictionaries yielded by
76-
this Cycler.
77-
78-
Returns
79-
-------
80-
cycle : list
81-
All of the dictionaries yielded by this Cycler in order.
82-
"""
83-
return list(self.finite_iter())
84-
8561
def _compose(self):
8662
"""
8763
Compose the 'left' and 'right' components of this cycle
8864
with the proper operation (zip or product as of now)
8965
"""
90-
for a, b in self._op(self._left.finite_iter(),
91-
self._right.finite_iter()):
66+
for a, b in self._op(self._left, self._right):
9267
out = dict()
9368
out.update(a)
9469
out.update(b)
@@ -120,7 +95,10 @@ def _from_iter(cls, label, itr):
12095
return ret
12196

12297
def __iter__(self):
123-
return cycle(self.finite_iter())
98+
if self._right is None:
99+
return iter(self._left)
100+
101+
return self._compose()
124102

125103
def __add__(self, other):
126104
return Cycler(self, other, zip)
@@ -156,7 +134,7 @@ def __repr__(self):
156134
op_map = {zip: '+', product: '*'}
157135
if self._right is None:
158136
lab = self.keys.pop()
159-
itr = list(v[lab] for v in self.finite_iter())
137+
itr = list(v[lab] for v in self)
160138
return "cycler({lab!r}, {itr!r})".format(lab=lab, itr=itr)
161139
else:
162140
op = op_map.get(self._op, '?')
@@ -192,6 +170,6 @@ def cycler(label, itr):
192170
return copy.copy(itr)
193171
else:
194172
lab = keys.pop()
195-
itr = list(v[lab] for v in itr.finite_iter())
173+
itr = list(v[lab] for v in itr)
196174

197175
return Cycler._from_iter(label, itr)

test_cycler.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212
def _cycler_helper(c, length, keys, values):
1313
assert_equal(len(c), length)
14-
assert_equal(len(c), len(list(c.finite_iter())))
15-
assert_equal(len(c), len(c.to_list()))
14+
assert_equal(len(c), len(list(c)))
1615
assert_equal(c.keys, set(keys))
1716

1817
for k, vals in zip(keys, values):

0 commit comments

Comments
 (0)