pescador.mux.ChainMux

class pescador.mux.ChainMux(streamers, mode='exhaustive', random_state=None)

As in itertools.chain(). Runs the first streamer to exhaustion, then the second, then the third, etc.

Examples

Run Chain once through until the end.

>>> a = pescador.Streamer("abc")
>>> b = pescador.Streamer("def")
>>> mux = pescador.ChainMux([a, b], mode="exhaustive")
>>> "".join(mux)
"abcdef"

Chain restarts from the beginning once exhausted.

>>> a = pescador.Streamer("abc")
>>> b = pescador.Streamer("def")
>>> mux = pescador.ChainMux([a, b], mode="cycle")
>>> "".join(mux(max_iter=12))
"abcdefabcdef"

Chain a generator of streamers

>>> import string
>>> def gen_streamers(n_streamers, n_copies):
...     for i in range(n_streamers):
...         yield pescador.Streamer(string.ascii_letters[i] * n_copies)
>>> mux = pescador.ChainMux(gen_streamers(3, 5))
>>> "".join(mux)
"aaaaabbbbbccccc"
__init__(streamers, mode='exhaustive', random_state=None)
Parameters:
streamers : list of pescador.Streamers OR generator of

pescador.Streamrers

mode : [“exhaustive”, “cycle”]
exhaustive

`ChainMux will exit after each stream has been exhausted.

cycle

`ChainMux will restart from the beginning after each streamer has been run to exhaustion.

random_state : None, int, or np.random.RandomState

If int, random_state is the seed used by the random number generator;

If RandomState instance, random_state is the random number generator;

If None, the random number generator is the RandomState instance used by np.random.

Methods

__init__(streamers[, mode, random_state])
Parameters:
cycle([max_iter]) Iterate from the streamer infinitely.
iterate([max_iter]) Yields items from the mux, and handles stream exhaustion and replacement.

Attributes

active Returns true if the stream is active (ie there are still open / existing streams)
is_activated_copy is_activated_copy is true if this object is a copy of the original Streamer and has been activated.
n_streams Return the number of streamers.