pythonwrench.collections.collections module

class pythonwrench.collections.collections.SizedGenerator(generator: Generator[T, None, None], size: int)[source]

Bases: Generic[T]

Wraps a generator and size to provide a sized iterable object.

pythonwrench.collections.collections.contained(x: __SPHINX_IMMATERIAL_TYPE_VAR__V_T, include: ~typing.Iterable[__SPHINX_IMMATERIAL_TYPE_VAR__V_T] | None = None, exclude: ~typing.Iterable[__SPHINX_IMMATERIAL_TYPE_VAR__V_T] | None = None, *, match_fn: ~typing.Callable[[__SPHINX_IMMATERIAL_TYPE_VAR__V_T, __SPHINX_IMMATERIAL_TYPE_VAR__V_T], bool] = <built-in function eq>, order: ~typing.Literal['left', 'right'] = 'right') bool[source]

Returns True if name in include set and not in exclude set.

pythonwrench.collections.collections.dict_list_to_list_dict(dic: Mapping[T, Iterable[U]], key_mode: 'union' = 'union', default_val: W = None) list[dict[T, U | W]][source]
pythonwrench.collections.collections.dict_list_to_list_dict(dic: Mapping[T, Iterable[U]], key_mode: 'same' | 'intersect', default_val: Any = None) list[dict[T, U]]

Convert dict of lists with same sizes to list of dicts.

Example 1

>>> dic = {"a": [1, 2], "b": [3, 4]}
>>> dict_list_to_list_dict(dic)
... [{"a": 1, "b": 3}, {"a": 2, "b": 4}]

Example 2

>>> dic = {"a": [1, 2, 3], "b": [4], "c": [5, 6]}
>>> dict_list_to_list_dict(dic, key_mode="union", default=-1)
... [{"a": 1, "b": 4, "c": 5}, {"a": 2, "b": -1, "c": 6}, {"a": 3, "b": -1, "c": -1}]
pythonwrench.collections.collections.dump_dict(dic: Mapping[str, T] | None = None, /, join: str = ', ', fmt: str = '{key}={value}', ignore_lst: Iterable[T] = (), **kwargs) str[source]

Dump dictionary of scalars to string function to customize representation.

Example 1:

>>> d = {"a": 1, "b": 2}
>>> dump_dict(d)
... 'a=1, b=2'
pythonwrench.collections.collections.duplicate_list(lst: list[T], sizes: list[int]) list[T][source]

Duplicate elements elements of a list with the corresponding sizes.

Example

>>> lst = ["a", "b", "c", "d", "e"]
>>> sizes = [1, 0, 2, 1, 3]
>>> duplicate_list(lst, sizes)
... ["a", "c", "c", "d", "e", "e", "e"]
pythonwrench.collections.collections.filter_iterable(it: ~typing.Iterable[__SPHINX_IMMATERIAL_TYPE_VAR__V_T], include: ~typing.Iterable[__SPHINX_IMMATERIAL_TYPE_VAR__V_T] | None = None, exclude: ~typing.Iterable[__SPHINX_IMMATERIAL_TYPE_VAR__V_T] | None = None, *, match_fn: ~typing.Callable[[__SPHINX_IMMATERIAL_TYPE_VAR__V_T, __SPHINX_IMMATERIAL_TYPE_VAR__V_T], bool] = <built-in function eq>, order: ~typing.Literal['left', 'right'] = 'right') list[T][source]
pythonwrench.collections.collections.find(target: T, it: Iterable[V], *, match_fn: Callable[[V, T], bool] = operator.eq, order: 'right' = 'right', default: U = -1, return_value: False = False) int | U[source]
pythonwrench.collections.collections.find(target: T, it: Iterable[V], *, match_fn: Callable[[T, V], bool] = operator.eq, order: 'left', default: U = -1, return_value: False = False) int | U
pythonwrench.collections.collections.find(target: T, it: Iterable[V], *, match_fn: Callable[[V, T], bool] = operator.eq, order: 'right' = 'right', default: U = -1, return_value: True) tuple[int | U, V | U]
pythonwrench.collections.collections.find(target: T, it: Iterable[V], *, match_fn: Callable[[T, V], bool] = operator.eq, order: 'left', default: U = -1, return_value: True) tuple[int | U, V | U]
pythonwrench.collections.collections.flat_dict_of_dict(nested_dic: Mapping[str, Any], *, sep: str = '.', flat_iterables: bool = False, overwrite: bool = True) dict[str, Any][source]

Flat a nested dictionary.

Example 1

>>> dic = {
...     "a": 1,
...     "b": {
...         "a": 2,
...         "b": 10,
...     },
... }
>>> flat_dict_of_dict(dic)
... {"a": 1, "b.a": 2, "b.b": 10}

Example 2

>>> dic = {"a": ["hello", "world"], "b": 3}
>>> flat_dict_of_dict(dic, flat_iterables=True)
... {"a.0": "hello", "a.1": "world", "b": 3}
Args:

nested_dic: Nested mapping containing sub-mappings or iterables. sep: Separators between keys. flat_iterables: If True, flat iterable and use index as key. overwrite: If True, overwrite duplicated keys in output. Otherwise duplicated keys will raises a ValueError.

pythonwrench.collections.collections.flat_list_of_list(lst: Iterable[Sequence[T]], return_sizes: True = True) tuple[list[T], list[int]][source]
pythonwrench.collections.collections.flat_list_of_list(lst: Iterable[Sequence[T]], return_sizes: False) list[T]

Return a flat version of the input list of sublists with each sublist size.

pythonwrench.collections.collections.flatten(x: T_BuiltinScalar, start_dim: int = 0, end_dim: int | None = None) list[T_BuiltinScalar][source]
pythonwrench.collections.collections.flatten(x: Iterable[T_BuiltinScalar], start_dim: int = 0, end_dim: int | None = None) list[T_BuiltinScalar]
pythonwrench.collections.collections.flatten(x: Any, start_dim: int = 0, end_dim: int | None = None, is_scalar_fn: Callable[[Any], TypeGuard[T]] | Callable[[Any], TypeIs[T]] = is_builtin_scalar) list[Any]
pythonwrench.collections.collections.intersect_lists(lst_of_lst: Sequence[Iterable[T]]) list[T][source]

Performs intersection of elements in lists (like set intersection), but keep their original order.

pythonwrench.collections.collections.list_dict_to_dict_list(lst: Iterable[Mapping[K, V]], key_mode: 'intersect' | 'same' = 'same', default_val: Any = None, *, default_val_fn: Any = None, list_fn: None = None) dict[K, list[V]][source]
pythonwrench.collections.collections.list_dict_to_dict_list(lst: Iterable[Mapping[K, V]], key_mode: 'union', default_val: Any = None, *, default_val_fn: Callable[[K], X], list_fn: None = None) dict[K, list[V | X]]
pythonwrench.collections.collections.list_dict_to_dict_list(lst: Iterable[Mapping[K, V]], key_mode: 'union', default_val: W = None, *, default_val_fn: None = None, list_fn: None = None) dict[K, list[V | W]]
pythonwrench.collections.collections.list_dict_to_dict_list(lst: Iterable[Mapping[K, V]], key_mode: 'intersect' | 'same' | 'union' | Iterable[K] = 'same', default_val: W = None, *, default_val_fn: Callable[[K], X] | None = None, list_fn: Callable[[list[V | W | X]], Y]) dict[K, Y]

Convert list of dicts to dict of lists.

Args:

lst: The list of dict to merge. Cannot be a Generator. key_mode: Can be “same” or “intersect”. - If “same”, all the dictionaries must contains the same keys otherwise a ValueError will be raised. - If “intersect”, only the intersection of all keys will be used in output. - If “union”, the output dict will contains the union of all keys, and the missing value will use the argument default_val. - If an iterable of elements, use them as keys for output dict. default_val: Default value of an element when key_mode is “union”. defaults to None. default_val_fn: Function to return the default value according to a specific key. defaults to None. list_fn: Optional function to build the values. defaults to identity.

pythonwrench.collections.collections.recursive_generator(x: Any) Generator[tuple[Any, int, int], None, None][source]
pythonwrench.collections.collections.shuffled(x: MutableSequence[T], *, seed: int | None = None, deep: bool = False) MutableSequence[T][source]
pythonwrench.collections.collections.sorted_dict(x: Mapping[K, V], /, *, by: 'key' = 'key', key: Callable[[K], Any] | None = None, reverse: bool = False) dict[K, V][source]
pythonwrench.collections.collections.sorted_dict(x: Mapping[K, V], /, *, by: 'value', key: Callable[[V], Any] | None = None, reverse: bool = False) dict[K, V]
pythonwrench.collections.collections.sorted_dict(x: Mapping[K, V], /, *, by: 'item', key: Callable[[tuple[K, V]], Any] | None = None, reverse: bool = False) dict[K, V]

Sort a dictionnary by key, value or item.

pythonwrench.collections.collections.unflat_dict_of_dict(dic: Mapping[str, Any], *, sep: str = '.') dict[str, Any][source]

Unflat a dictionary.

Example 1

>>> dic = {
    "a.a": 1,
    "b.a": 2,
    "b.b": 3,
    "c": 4,
}
>>> unflat_dict_of_dict(dic)
... {"a": {"a": 1}, "b": {"a": 2, "b": 3}, "c": 4}
pythonwrench.collections.collections.unflat_list_of_list(flatten_lst: Sequence[T], sizes: Iterable[int]) list[list[T]][source]

Unflat a list to a list of sublists of given sizes.

pythonwrench.collections.collections.union_dicts(dicts: Iterable[dict[K, V]]) dict[K, V][source]

Performs union of dictionaries.

pythonwrench.collections.collections.union_lists(lst_of_lst: Iterable[Iterable[K]]) list[K][source]

Performs union of elements in lists (like set union), but keep their original order.

pythonwrench.collections.collections.unzip(lst: Iterable[tuple]) tuple[source]
pythonwrench.collections.collections.unzip(lst: Iterable[tuple[T]]) tuple[list[T]]
pythonwrench.collections.collections.unzip(lst: Iterable[tuple[T, U]]) tuple[list[T], list[U]]
pythonwrench.collections.collections.unzip(lst: Iterable[tuple[T, U, V]]) tuple[list[T], list[U], list[V]]
pythonwrench.collections.collections.unzip(lst: Iterable[tuple[T, U, V, W]]) tuple[list[T], list[U], list[V], list[W]]
pythonwrench.collections.collections.unzip(lst: Iterable[tuple[T, U, V, W, X]]) tuple[list[T], list[U], list[V], list[W], list[X]]
pythonwrench.collections.collections.unzip(lst: Iterable[tuple[T, ...]]) tuple[list[T], ...]

Invert function of builtin zip().

Example

>>> lst1 = [1, 2, 3, 4]
>>> lst2 = [5, 6, 7, 8]
>>> zipped_list = list(zip(lst1, lst2))
>>> zipped_list
... [(1, 5), (2, 6), (3, 7), (4, 8)]
>>> unzip(zipped_list)
... [1, 2, 3, 4], [5, 6, 7, 8]