here.platform.utils.collection module#

This module contains generic utility functions for python collections and iterables.

here.platform.utils.collection.flatten_iterator(iterator: Iterator[Tuple[Any, Any]], max_level: int = -1, prefix: str = '', sep: str = '.', exclude_keys: Collection = []) Iterator[Tuple[str, Any]][source]#

Scan a tuple iterator and flatten keys and values, in case values contain nested dictionaries.

Keys are converted to string and composed in hierarchical paths, separated by sep. An initial prefix, if present, is prepended and separated by sep to the keys of the passed and nested dictionaries. Order of elements is preserved.

Among other uses, the output of the function can be passed directly to the constructor of dict to compose a new, flat dictionary without overhead.

Parameters:
  • iterator – the iterator to scan recursively

  • max_level – the maximum level of the recursion. 0 performs no flattening, 1 flattens only the first nested dictionaries, 2 only the first and second nested dictionaries, and so on. A negative number represents no limit.

  • prefix – what to prepend, separate using sep, to all the returned keys

  • sep – the separator to use when concatenating keys

  • exclude_keys – full path of keys that should not be flattened

Yield:

tuples of concatenated keys and unchanged values

Usage:

>>> data = { 1: 10, 2: 20, 3: { "a": "AAA", "b": "BBB" }, 4: 40 }
>>> list(flatten_iterator(iter(data.items())))
[('1', 10), ('2', 20), ('3.a', 'AAA'), ('3.b', 'BBB'), ('4', 40)]

>>> dict(flatten_iterator(iter(data.items())))
{'1': 10, '2': 20, '3.a': 'AAA', '3.b': 'BBB', '4': 40}

>>> dict(flatten_iterator(iter(data.items()), prefix="x"))
{'x.1': 10, 'x.2': 20, 'x.3.a': 'AAA', 'x.3.b': 'BBB', 'x.4': 40}
here.platform.utils.collection.grouper(size: int, iterable: Iterable[T], fill_value=None) Iterator[T][source]#

Create groups of size each from given iterable.

Parameters:
  • size – An int representing size of each group.

  • iterable – An iterable.

  • fill_value – Value to put for the last group.

Returns:

A Generator.

here.platform.utils.collection.iter_tuples(iterable)[source]#

Return an iterator of key-value tuples from a variety of collections types.

This is used to convert mapping to their items, but also supporting collections or iterators that are already in the form of key-value tuples.

Supported types: - mappings (dict), yielding key-value tuples - every collection that, when iterated, yields tuples

in this case the tuples are not modified and may contain more than two elements

Parameters:

iterable – a collection containing keys and values

Returns:

an iterator over the keys and values of the collection, as tuples

Usage:

>>> dict_data = { 1: 10, 2: 20, 3: 30, 4: 40 }
>>> list(iter_tuples(dict_data))
[(1, 10), (2, 20), (3, 30), (4, 40)]

>>> list_data = [(1, 10), (2, 20), (3, 30), (4, 40)]
>>> list(iter_tuples(list_data))
[(1, 10), (2, 20), (3, 30), (4, 40)]

>>> map_data = map(lambda x: (x, x * 10), range(1, 5))
>>> list(iter_tuples(map_data))
[(1, 10), (2, 20), (3, 30), (4, 40)]