Skip to content

Commit 7eac6f2

Browse files
committed
Address comments and some code simplifications
1 parent 1898aa2 commit 7eac6f2

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

cycler.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def keys(self):
138138
"""
139139
return set(self._keys)
140140

141-
def key_change(self, old, new):
141+
def change_key(self, old, new):
142142
"""
143143
Change a key in this cycler to a new name.
144144
Modification is performed in-place.
@@ -161,20 +161,17 @@ def key_change(self, old, new):
161161
self._keys.add(new)
162162

163163
if self._right is not None and old in self._right.keys:
164-
self._right.key_change(old, new)
165-
elif self._left is not None:
166-
if isinstance(self._left, Cycler):
167-
self._left.key_change(old, new)
168-
else:
169-
Cycler._key_change(iter(self._left), old, new)
170-
171-
@staticmethod
172-
def _key_change(itr, old, new):
173-
entry = six.next(itr)
174-
if old in entry:
175-
entry[new] = entry[old]
176-
del entry[old]
177-
for entry in itr:
164+
self._right.change_key(old, new)
165+
166+
# self._left should always be non-None
167+
# if self._keys is non-empty.
168+
elif isinstance(self._left, Cycler):
169+
self._left.change_key(old, new)
170+
else:
171+
# It should be completely safe at this point to
172+
# assume that the old key can be found in each
173+
# iteration.
174+
for entry in self._left:
178175
entry[new] = entry[old]
179176
del entry[old]
180177

test_cycler.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,25 +221,25 @@ def test_keychange():
221221
c2 = cycler('lw', [1, 2, 3])
222222
c3 = cycler('ec', 'yk')
223223

224-
c3.key_change('ec', 'edgecolor')
224+
c3.change_key('ec', 'edgecolor')
225225
assert_equal(c3, cycler('edgecolor', c3))
226226

227227
c = c1 + c2
228-
c.key_change('lw', 'linewidth')
228+
c.change_key('lw', 'linewidth')
229229
assert_equal(c, c1 + cycler('linewidth', c2))
230230

231231
c = (c1 + c2) * c3
232-
c.key_change('c', 'color')
232+
c.change_key('c', 'color')
233233
assert_equal(c, (cycler('color', c1) + c2) * c3)
234234

235235
# Perfectly fine, it is a no-op
236-
c.key_change('color', 'color')
236+
c.change_key('color', 'color')
237237
assert_equal(c, (cycler('color', c1) + c2) * c3)
238238

239239
# Can't change a key to one that is already in there
240-
assert_raises(ValueError, Cycler.key_change, c, 'color', 'linewidth')
240+
assert_raises(ValueError, Cycler.change_key, c, 'color', 'linewidth')
241241
# Can't change a key you don't have
242-
assert_raises(KeyError, Cycler.key_change, c, 'c', 'foobar')
242+
assert_raises(KeyError, Cycler.change_key, c, 'c', 'foobar')
243243

244244

245245
def _eq_test_helper(a, b, res):

0 commit comments

Comments
 (0)