Skip to content

Commit 17f8108

Browse files
committed
ENH: add by_key
Promote `_transpose` to the public method `by_key`.
1 parent 78f477d commit 17f8108

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

cycler.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def _from_iter(cls, label, itr):
216216
def __getitem__(self, key):
217217
# TODO : maybe add numpy style fancy slicing
218218
if isinstance(key, slice):
219-
trans = self._transpose()
219+
trans = self.by_key()
220220
return reduce(add, (_cycler(k, v[key])
221221
for k, v in six.iteritems(trans)))
222222
else:
@@ -255,7 +255,7 @@ def __mul__(self, other):
255255
if isinstance(other, Cycler):
256256
return Cycler(self, other, product)
257257
elif isinstance(other, int):
258-
trans = self._transpose()
258+
trans = self.by_key()
259259
return reduce(add, (_cycler(k, v*other)
260260
for k, v in six.iteritems(trans)))
261261
else:
@@ -346,17 +346,21 @@ def _repr_html_(self):
346346
output += "</table>"
347347
return output
348348

349-
def _transpose(self):
350-
"""
351-
Internal helper function which iterates through the
352-
styles and returns a dict of lists instead of a list of
353-
dicts. This is needed for multiplying by integers and
354-
for __getitem__
349+
def by_key(self):
350+
"""Values by key
351+
352+
This returns the transposed values of the cycler. Iterating
353+
over a `Cycler` yields dicts with a single value for each key,
354+
this method returns a `dict` of `list` which are the values
355+
for the given key.
356+
357+
The returned value can be used to create an equivalent `Cycler`
358+
using only `+`.
355359
356360
Returns
357361
-------
358-
trans : dict
359-
dict of lists for the styles
362+
transpose : dict
363+
dict of lists of the values for each key.
360364
"""
361365

362366
# TODO : sort out if this is a bottle neck, if there is a better way
@@ -386,7 +390,7 @@ def simplify(self):
386390
# ((a + b) + (c + d))
387391
# I would believe that there is some performance implications
388392

389-
trans = self._transpose()
393+
trans = self.by_key()
390394
return reduce(add, (_cycler(k, v) for k, v in six.iteritems(trans)))
391395

392396
def concat(self, other):
@@ -453,8 +457,8 @@ def concat(left, right):
453457

454458
raise ValueError(msg)
455459

456-
_l = left._transpose()
457-
_r = right._transpose()
460+
_l = left.by_key()
461+
_r = right.by_key()
458462
return reduce(add, (_cycler(k, _l[k] + _r[k]) for k in left.keys))
459463

460464

doc/source/index.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,26 @@ Cycles can be sliced with :obj:`slice` objects
210210
211211
to return a sub-set of the cycle as a new `Cycler`.
212212

213+
Inspecting the `Cycler`
214+
-----------------------
215+
216+
To inspect the values of the transposed `Cycler` use
217+
the `Cycler.by_key` method:
218+
219+
.. ipython:: python
220+
221+
c_m.by_key()
222+
223+
This `dict` can be mutated and used to create a new `Cycler` with
224+
the updated values
225+
226+
.. ipython:: python
227+
228+
bk = c_m.by_key()
229+
bk['color'] = ['green'] * len(c_m)
230+
cycler(**bk)
231+
232+
213233
Examples
214234
--------
215235

0 commit comments

Comments
 (0)