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 a cycler-specific chain function. #29

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all 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
27 changes: 26 additions & 1 deletion cycler.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
unicode_literals)

import six
from itertools import product, cycle
from itertools import product, cycle, chain as itertools_chain
from six.moves import zip, reduce
from operator import mul, add
import copy
Expand Down Expand Up @@ -549,3 +549,28 @@ def _cycler(label, itr):
itr = (v[lab] for v in itr)

return Cycler._from_iter(label, itr)


def chain(*args):
Copy link
Member

Choose a reason for hiding this comment

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

Can we rename this to cychain?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. Would that change your 30/70 feeling on putting it in?

Copy link
Member

Choose a reason for hiding this comment

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

I think I would merge it with this change.

"""
Concatenate cyclers.

This is identical to `itertools.chain`, but it will raise if any arguments
do not have a `keys` attribute with the same contents.

Parameters
----------
*args : one or more Cycler objects

Returns
-------
cycler : Cycler
New `Cycler`, concanetating input `Cycler` objects
"""
_args = iter(args)
expected_keys = next(_args.keys)
for arg in _args:
if arg.keys != expected_keys:
raise ValueError("Keys of input cyclers must match. {} does not "
"match {}".format(arg.keys, expected_keys))
return itertools_chain(*args)