polymerist.genutils.iteration

Tools for simplifying iteration over collections of items

Attributes

T

T1

T2

V

Functions

iter_len(itera)

Get size of an iterable object where ordinary len() call is invalid (namely a generator)

product(→ T)

Multiplicative analogue to builtin sum()

sliding_window(→ Generator[tuple[T], None, None])

Generates sliding windows of width n over an iterable collection of items

powerset(→ Generator[tuple[T], None, None])

Generate all possible subsets of a set. Can optionally exclude the empty set or the complete set (or both)

asiterable(→ Iterable[T])

Permits functions expecting iterable arguments to accept singular values

modify_dict(→ None)

Recursively modifies all values in a dict in-place according to some function

sort_dict_by_keys(→ dict[Any, Any])

Sort a dictionary according to the values of each key

sort_dict_by_values(→ dict[Any, Any])

Sort a dictionary according to the values of each key

cartesian_grid(→ Generator[dict[str, Any], None, None])

Accepts a dict maping keys to all valid parameter values for that key

Module Contents

polymerist.genutils.iteration.T
polymerist.genutils.iteration.T1
polymerist.genutils.iteration.T2
polymerist.genutils.iteration.iter_len(itera: Iterable)[source]

Get size of an iterable object where ordinary len() call is invalid (namely a generator) Note that this will “use up” a generator upon iteration

polymerist.genutils.iteration.product(itera: Iterable[T]) T[source]

Multiplicative analogue to builtin sum()

polymerist.genutils.iteration.sliding_window(items: Iterable[T], n: int = 1) Generator[tuple[T], None, None][source]

Generates sliding windows of width n over an iterable collection of items E.g. : sliding_window(‘ABCDE’, 3) –> (A, B, C), (B, C, D), (C, D, E)

polymerist.genutils.iteration.powerset(items: Iterable[T], exclude_empty: bool = False, exclude_full: bool = False) Generator[tuple[T], None, None][source]

Generate all possible subsets of a set. Can optionally exclude the empty set or the complete set (or both)

polymerist.genutils.iteration.asiterable(arg_val: T | Iterable[T]) Iterable[T][source]

Permits functions expecting iterable arguments to accept singular values

polymerist.genutils.iteration.V
polymerist.genutils.iteration.modify_dict(some_dict: dict[Hashable, V], modifier_fn: Callable[[Hashable, V], Any]) None[source]

Recursively modifies all values in a dict in-place according to some function

polymerist.genutils.iteration.sort_dict_by_keys(targ_dict: dict, reverse: bool = False) dict[Any, Any][source]

Sort a dictionary according to the values of each key

polymerist.genutils.iteration.sort_dict_by_values(targ_dict: dict, reverse: bool = False) dict[Any, Any][source]

Sort a dictionary according to the values of each key

polymerist.genutils.iteration.cartesian_grid(param_dict: dict[str, Iterable[Any]]) Generator[dict[str, Any], None, None][source]

Accepts a dict maping keys to all valid parameter values for that key Generates all possible choices of parameters as dicts with exactly one parameter value per key