|
2 | 2 | unicode_literals)
|
3 | 3 |
|
4 | 4 | import six
|
5 |
| -from itertools import product, cycle |
| 5 | +from itertools import product |
6 | 6 | from six.moves import zip
|
7 | 7 | from operator import mul
|
8 | 8 | import copy
|
@@ -58,37 +58,12 @@ def __init__(self, left, right=None, op=None):
|
58 | 58 | def keys(self):
|
59 | 59 | return set(self._keys)
|
60 | 60 |
|
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 |
| - |
85 | 61 | def _compose(self):
|
86 | 62 | """
|
87 | 63 | Compose the 'left' and 'right' components of this cycle
|
88 | 64 | with the proper operation (zip or product as of now)
|
89 | 65 | """
|
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): |
92 | 67 | out = dict()
|
93 | 68 | out.update(a)
|
94 | 69 | out.update(b)
|
@@ -120,7 +95,10 @@ def _from_iter(cls, label, itr):
|
120 | 95 | return ret
|
121 | 96 |
|
122 | 97 | 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() |
124 | 102 |
|
125 | 103 | def __add__(self, other):
|
126 | 104 | return Cycler(self, other, zip)
|
@@ -156,7 +134,7 @@ def __repr__(self):
|
156 | 134 | op_map = {zip: '+', product: '*'}
|
157 | 135 | if self._right is None:
|
158 | 136 | lab = self.keys.pop()
|
159 |
| - itr = list(v[lab] for v in self.finite_iter()) |
| 137 | + itr = list(v[lab] for v in self) |
160 | 138 | return "cycler({lab!r}, {itr!r})".format(lab=lab, itr=itr)
|
161 | 139 | else:
|
162 | 140 | op = op_map.get(self._op, '?')
|
@@ -192,6 +170,6 @@ def cycler(label, itr):
|
192 | 170 | return copy.copy(itr)
|
193 | 171 | else:
|
194 | 172 | lab = keys.pop()
|
195 |
| - itr = list(v[lab] for v in itr.finite_iter()) |
| 173 | + itr = list(v[lab] for v in itr) |
196 | 174 |
|
197 | 175 | return Cycler._from_iter(label, itr)
|
0 commit comments