pythonwrench.collections package¶
- class pythonwrench.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.all_eq(it: Iterable[T], eq_fn: Callable[[T, T], bool] | None =
None) bool[source]¶ Returns true if all elements in iterable are equal.
Note: This function returns True for iterable that contains 0 or 1 element.
-
pythonwrench.collections.all_ne(it: Iterable[T], ne_fn: Callable[[T, T], bool] | None =
None, use_set: bool =False) bool[source]¶ Returns true if all elements in iterable are differents.
Note: This function returns True for iterable that contains 0 or 1 element.
- pythonwrench.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.dict_list_to_list_dict(dic: Mapping[T, Iterable[U]], key_mode: 'intersect' | 'same' | 'union' =
'union', default_val: W =None) list[dict[T, U | W]][source]¶ 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.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.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.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.find(target: ~typing.Any, it: ~typing.Iterable[__SPHINX_IMMATERIAL_TYPE_VAR__V_V], *, match_fn: ~typing.Callable[[~typing.Any, ~typing.Any], bool] = <built-in function eq>, order: ~typing.Literal['left', 'right'] = 'right', default: __SPHINX_IMMATERIAL_TYPE_VAR__V_U = -1, return_value: bool = False) int | U | tuple[int | U, V | U][source]¶
-
pythonwrench.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.flat_list_of_list(lst: Iterable[Sequence[T]], return_sizes: bool =
True) tuple[list[T], list[int]] | list[T][source]¶ Return a flat version of the input list of sublists with each sublist size.
- pythonwrench.collections.flatten(x: ~typing.Any, start_dim: int = 0, end_dim: int | None = None, is_scalar_fn: ~typing.Callable[[~typing.Any], ~typing.TypeGuard[__SPHINX_IMMATERIAL_TYPE_VAR__V_T]] | ~typing.Callable[[~typing.Any], ~typing.TypeIs[__SPHINX_IMMATERIAL_TYPE_VAR__V_T]] = <function is_builtin_scalar>) list[Any][source]¶
-
pythonwrench.collections.intersect(*args, start=
None)[source]¶ Reduce elements using “and” operator (&).
- pythonwrench.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.is_full(it: Iterable[T], eq_fn: Callable[[T, T], bool] | None =
None) bool[source]¶ Returns true if all elements in iterable are equal.
Note: This function returns True for iterable that contains 0 or 1 element.
-
pythonwrench.collections.is_sorted(x: Iterable[Any], *, reverse: bool =
False, strict: bool =False) bool[source]¶
-
pythonwrench.collections.is_unique(it: Iterable[T], ne_fn: Callable[[T, T], bool] | None =
None, use_set: bool =False) bool[source]¶ Returns true if all elements in iterable are differents.
Note: This function returns True for iterable that contains 0 or 1 element.
- pythonwrench.collections.list_dict_to_dict_list(lst: ~typing.Iterable[~typing.Mapping[__SPHINX_IMMATERIAL_TYPE_VAR__V_K, __SPHINX_IMMATERIAL_TYPE_VAR__V_V]], key_mode: ~typing.Literal['intersect', 'same', 'union'] | ~typing.Iterable[__SPHINX_IMMATERIAL_TYPE_VAR__V_K] = 'same', default_val: __SPHINX_IMMATERIAL_TYPE_VAR__V_W = None, *, default_val_fn: ~typing.Callable[[__SPHINX_IMMATERIAL_TYPE_VAR__V_K], __SPHINX_IMMATERIAL_TYPE_VAR__V_X] | None = None, list_fn: ~typing.Callable[[~typing.List[__SPHINX_IMMATERIAL_TYPE_VAR__V_V | __SPHINX_IMMATERIAL_TYPE_VAR__V_W | __SPHINX_IMMATERIAL_TYPE_VAR__V_X]], __SPHINX_IMMATERIAL_TYPE_VAR__V_Y] | None = <function identity>) dict[K, Y][source]¶
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.recursive_generator(x: Any) Generator[tuple[Any, int, int], None, None][source]¶
-
pythonwrench.collections.reduce_add(*args, start=
None)[source]¶ Reduce elements using “add” operator (+).
-
pythonwrench.collections.reduce_and(*args, start=
None)[source]¶ Reduce elements using “and” operator (&).
-
pythonwrench.collections.reduce_matmul(*args, start=
None)[source]¶ Reduce elements using “mul” operator (*).
-
pythonwrench.collections.reduce_mul(*args, start=
None)[source]¶ Reduce elements using “mul” operator (*).
-
pythonwrench.collections.reduce_or(*args, start=
None)[source]¶ Reduce elements using “or” operator (|).
-
pythonwrench.collections.shuffled(x: MutableSequence[T], *, seed: int | None =
None, deep: bool =False) MutableSequence[T][source]¶
-
pythonwrench.collections.sorted_dict(x: Mapping[K, V], /, *, by: 'key' | 'value' | 'item' =
'key', key: Callable[[Any], Any] | None =None, reverse: bool =False) dict[K, V][source]¶ Sort a dictionnary by key, value or item.
-
pythonwrench.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.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.union_dicts(dicts: Iterable[dict[K, V]]) dict[K, V][source]¶
Performs union of dictionaries.
- pythonwrench.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.unzip(lst)[source]¶
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]
Submodules¶
- pythonwrench.collections.collections module
- pythonwrench.collections.collections.SizedGenerator
- pythonwrench.collections.collections.contained
- pythonwrench.collections.collections.dict_list_to_list_dict
- pythonwrench.collections.collections.dump_dict
- pythonwrench.collections.collections.duplicate_list
- pythonwrench.collections.collections.filter_iterable
- pythonwrench.collections.collections.find
- pythonwrench.collections.collections.flat_dict_of_dict
- pythonwrench.collections.collections.flat_list_of_list
- pythonwrench.collections.collections.flatten
- pythonwrench.collections.collections.intersect_lists
- pythonwrench.collections.collections.list_dict_to_dict_list
- pythonwrench.collections.collections.recursive_generator
- pythonwrench.collections.collections.shuffled
- pythonwrench.collections.collections.sorted_dict
- pythonwrench.collections.collections.unflat_dict_of_dict
- pythonwrench.collections.collections.unflat_list_of_list
- pythonwrench.collections.collections.union_dicts
- pythonwrench.collections.collections.union_lists
- pythonwrench.collections.collections.unzip
- pythonwrench.collections.prop module
- pythonwrench.collections.reducers module
- pythonwrench.collections.reducers.intersect
- pythonwrench.collections.reducers.prod
- pythonwrench.collections.reducers.reduce_add
- pythonwrench.collections.reducers.reduce_and
- pythonwrench.collections.reducers.reduce_matmul
- pythonwrench.collections.reducers.reduce_mul
- pythonwrench.collections.reducers.reduce_or
- pythonwrench.collections.reducers.sum
- pythonwrench.collections.reducers.union