Skip to content

Commit 786cc49

Browse files
committed
A bit more bullet-proof implementation (I hope)
1 parent f010caa commit 786cc49

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

cycler.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,17 @@ def _process_keys(left, right):
5858
5959
Parameters
6060
----------
61-
left, right : Cycler or None
61+
left, right : iterable of dictionaries or None
6262
The cyclers to be composed
6363
Returns
6464
-------
6565
keys : set
6666
The keys in the composition of the two cyclers
6767
"""
68-
l_key = left.keys if left is not None else set()
69-
r_key = right.keys if right is not None else set()
68+
l_peek = next(iter(left)) if left is not None else {}
69+
r_peek = next(iter(right)) if right is not None else {}
70+
l_key = set(l_peek.keys())
71+
r_key = set(r_peek.keys())
7072
if l_key & r_key:
7173
raise ValueError("Can not compose overlapping cycles")
7274
return l_key | r_key
@@ -112,27 +114,23 @@ def __init__(self, left, right=None, op=None):
112114
113115
Do not use this directly, use `cycler` function instead.
114116
"""
115-
self._keys = _process_keys(left, right)
116117
if isinstance(left, Cycler):
117-
self._left = left._shallow_copy()
118+
self._left = Cycler(left._left, left._right, left._op)
119+
elif left is not None:
120+
self._left = list(left)
118121
else:
119-
self._left = copy.copy(left)
122+
self._left = None
120123

121124
if isinstance(right, Cycler):
122-
self._right = right._shallow_copy()
125+
self._right = Cycler(right._left, right._right, right._op)
126+
elif right is not None:
127+
self._right = list(right)
123128
else:
124-
self._right = copy.copy(right)
129+
self._right = None
125130

131+
self._keys = _process_keys(self._left, self._right)
126132
self._op = op
127133

128-
def _shallow_copy(self):
129-
ret = Cycler(None)
130-
ret._keys = self.keys
131-
ret._left = copy.copy(self._left)
132-
ret._right = copy.copy(self._right)
133-
ret._op = copy.copy(self._op)
134-
return ret
135-
136134
@property
137135
def keys(self):
138136
"""
@@ -251,7 +249,7 @@ def __iadd__(self, other):
251249
self._keys = _process_keys(old_self, other)
252250
self._left = old_self
253251
self._op = zip
254-
self._right = other._shallow_copy()
252+
self._right = Cycler(other._left, other._right, other._op)
255253
return self
256254

257255
def __imul__(self, other):
@@ -270,7 +268,7 @@ def __imul__(self, other):
270268
self._keys = _process_keys(old_self, other)
271269
self._left = old_self
272270
self._op = product
273-
self._right = other._shallow_copy()
271+
self._right = Cycler(other._left, other._right, other._op)
274272
return self
275273

276274
def __eq__(self, other):

0 commit comments

Comments
 (0)