Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: add concat #20

Merged
merged 6 commits into from
Oct 12, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions cycler.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,55 @@ def simplify(self):
trans = self._transpose()
return reduce(add, (_cycler(k, v) for k, v in six.iteritems(trans)))

def concat(self, other):
"""Concatenate two cyclers.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great docstring - I'd be inclined to include the simplest possible example though, so that people can get a visual grasp of what is going on.


The keys must match exactly.

Parameters
----------
other : `Cycler`
The `Cycler` instances to concatenate to this one

Returns
-------
ret : `Cycler`
The concatenated `Cycler`
"""
return concat(self, other)


def concat(left, right):
"""Concatenate two cyclers.

The keys must match exactly.

This returns a single Cycler which is equivalent to
`itertools.chain(left, right)`

Parameters
----------
left, right : `Cycler`
The two `Cycler` instances to concatenate

Returns
-------
ret : `Cycler`
The concatenated `Cycler`
"""
if left.keys != right.keys:
msg = '\n\t'.join(["Keys do not match:",
"Intersection: {both!r}",
"Disjoint: {just_one!r}"
]).format(
both=left.keys&right.keys,
just_one=left.keys^right.keys)

raise ValueError(msg)

_l = left._transpose()
_r = right._transpose()
return reduce(add, (_cycler(k, _l[k] + _r[k]) for k in left.keys))

def cycler(*args, **kwargs):
"""
Expand Down
16 changes: 16 additions & 0 deletions doc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,22 @@ Integer Multiplication
2 * color_cycle


Concatenation
~~~~~~~~~~~~~

`Cycler` objects can be concatenated either via the :py:meth:`Cycler.concat` method

.. ipython:: python

color_cycle.concat(color_cycle)

or the top-level :py:func:`concat` function

.. ipython:: python

from cycler import concat
concat(color_cycle, color_cycle)


Slicing
-------
Expand Down
20 changes: 18 additions & 2 deletions test_cycler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import six
from six.moves import zip, range
from cycler import cycler, Cycler
from cycler import cycler, Cycler, concat
from nose.tools import (assert_equal, assert_not_equal,
assert_raises, assert_true)
from itertools import product, cycle
from itertools import product, cycle, chain
from operator import add, iadd, mul, imul


Expand Down Expand Up @@ -279,3 +279,19 @@ def test_starange_init():
c2 = cycler('lw', range(3))
cy = Cycler(list(c), list(c2), zip)
assert_equal(cy, c + c2)


def test_concat():
a = cycler('a', range(3))
for con, chn in zip(a.concat(a), chain(a, a)):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be inclined to test this with two different cyclers, the way this is tested it would be possible to completely ignore the other input and concatenate self with self (which of course, is not the behaviour we want).

assert_equal(con, chn)

for con, chn in zip(concat(a, a), chain(a, a)):
assert_equal(con, chn)


def test_concat_fail():
a = cycler('a', range(3))
b = cycler('b', range(3))
assert_raises(ValueError, concat, a, b)
assert_raises(ValueError, a.concat, b)