gluonts.itertools module#

class gluonts.itertools.Cached(iterable: gluonts.itertools.SizedIterable)[source]#

Bases: object

An iterable wrapper, which caches values in a list the first time it is iterated.

The primary use-case for this is to avoid re-computing the element of the sequence, in case the inner iterable does it on demand.

This should be used to wrap deterministic iterables, i.e. iterables where the data generation process is not random, and that yield the same elements when iterated multiple times.

cache: list#
iterable: gluonts.itertools.SizedIterable#
class gluonts.itertools.Cyclic(iterable: gluonts.itertools.SizedIterable)[source]#

Bases: object

Like itertools.cycle, but does not store the data.

iterable: gluonts.itertools.SizedIterable#
class gluonts.itertools.IterableSlice(iterable, length)[source]#

Bases: object

An iterable version of itertools.islice, i.e. one that can be iterated over multiple times:

>>> isl = IterableSlice(iter([1, 2, 3, 4, 5]), 3)
>>> list(isl)
[1, 2, 3]
>>> list(isl)
[4, 5]
>>> list(isl)
[]

This needs to be a class to support re-entry iteration.

class gluonts.itertools.Map(fn, iterable: gluonts.itertools.SizedIterable)[source]#

Bases: object

class gluonts.itertools.PseudoShuffled(iterable: gluonts.itertools.SizedIterable, shuffle_buffer_length: int)[source]#

Bases: object

Yield items from a given iterable in a pseudo-shuffled order.

iterable: gluonts.itertools.SizedIterable#
shuffle_buffer_length: int#
class gluonts.itertools.SizedIterable(*args, **kwds)[source]#

Bases: typing_extensions.Protocol

gluonts.itertools.batcher(iterable: Iterable[gluonts.itertools.T], batch_size: int) Iterator[List[gluonts.itertools.T]][source]#

Groups elements from iterable into batches of size batch_size.

>>> list(batcher("ABCDEFG", 3))
[['A', 'B', 'C'], ['D', 'E', 'F'], ['G']]

Unlike the grouper proposed in the documentation of itertools, batcher doesn’t fill up missing values.

gluonts.itertools.columns_to_rows(columns: Dict[gluonts.itertools.K, Sequence[gluonts.itertools.V]]) List[Dict[gluonts.itertools.K, gluonts.itertools.V]][source]#

Transpose column-orientation to row-orientation.

>>> columns_to_rows({'a': [1, 3], 'b': [2, 4]})
[{'a': 1, 'b': 2}, {'a': 3, 'b': 4}]
gluonts.itertools.maybe_len(obj) Optional[int][source]#
gluonts.itertools.partition(it: Iterator[gluonts.itertools.T], fn: Callable[[gluonts.itertools.T], bool]) Tuple[List[gluonts.itertools.T], List[gluonts.itertools.T]][source]#

Partition it into two lists given predicate fn.

This is similar to the recipe defined in Python’s itertools docs, however this method consumes the iterator directly and returns lists instead of iterators.

gluonts.itertools.prod(xs)[source]#

Compute the product of the elements of an iterable object.

gluonts.itertools.roundrobin(*iterables)[source]#

roundrobin(‘ABC’, ‘D’, ‘EF’) –> A D E B F C

Taken from: https://docs.python.org/3/library/itertools.html#recipes.

gluonts.itertools.rows_to_columns(rows: typing.Sequence[typing.Dict[gluonts.itertools.K, gluonts.itertools.V]], wrap: typing.Callable[[typing.Sequence[gluonts.itertools.V]], typing.Sequence[gluonts.itertools.V]] = <function <lambda>>) Dict[gluonts.itertools.K, Sequence[gluonts.itertools.V]][source]#

Transpose rows of dicts, to one dict containing columns.

>>> rows_to_columns([{'a': 1, 'b': 2}, {'a': 3, 'b': 4}])
{'a': [1, 3], 'b': [2, 4]}

This can also be understood as stacking the values of each dict onto each other.