Skip to content

coola.nested

coola.nested

Contain simple operations on nested data structure.

coola.nested.convert_to_dict_of_lists

convert_to_dict_of_lists(
    seq_of_mappings: Sequence[Mapping[Any, Any]],
) -> dict[Any, list[Any]]

Convert a sequence of mappings to a dictionary of lists.

All the dictionaries should have the same keys. The first mapping in the sequence is used to find the keys.

Parameters:

Name Type Description Default
seq_of_mappings Sequence[Mapping[Any, Any]]

The sequence of mappings to convert.

required

Returns:

Type Description
dict[Any, list[Any]]

A dictionary of lists.

Example
>>> from coola.nested import convert_to_dict_of_lists
>>> convert_to_dict_of_lists(
...     [{"key1": 1, "key2": 10}, {"key1": 2, "key2": 20}, {"key1": 3, "key2": 30}]
... )
{'key1': [1, 2, 3], 'key2': [10, 20, 30]}

coola.nested.convert_to_list_of_dicts

convert_to_list_of_dicts(
    mapping_of_seqs: Mapping[Any, Sequence[Any]],
) -> list[dict[Any, Any]]

Convert a mapping of sequences to a list of dictionaries.

All the sequences should have the same length.

Parameters:

Name Type Description Default
mapping_of_seqs Mapping[Any, Sequence[Any]]

The mapping of sequences to convert.

required

Returns:

Type Description
list[dict[Any, Any]]

A dictionary of lists.

Example
>>> from coola.nested import convert_to_list_of_dicts
>>> convert_to_list_of_dicts({"key1": [1, 2, 3], "key2": [10, 20, 30]})
[{'key1': 1, 'key2': 10}, {'key1': 2, 'key2': 20}, {'key1': 3, 'key2': 30}]

coola.nested.get_first_value

get_first_value(data: Mapping[Any, T]) -> T

Get the first value of a mapping.

Parameters:

Name Type Description Default
data Mapping[Any, T]

The input mapping.

required

Returns:

Type Description
T

The first value in the mapping.

Raises:

Type Description
ValueError

if the mapping is empty.

Example
>>> from coola.nested import get_first_value
>>> get_first_value({"key1": 1, "key2": 2})
1

coola.nested.remove_keys_starting_with

remove_keys_starting_with(
    mapping: Mapping[Any, Any], prefix: str
) -> dict[Any, Any]

Remove the keys that start with a given prefix.

Parameters:

Name Type Description Default
mapping Mapping[Any, Any]

The original mapping.

required
prefix str

The prefix used to filter the keys.

required

Returns:

Type Description
dict[Any, Any]

A new dict without the removed keys.

Example
>>> from coola.nested import remove_keys_starting_with
>>> remove_keys_starting_with(
...     {"key": 1, "key.abc": 2, "abc": 3, "abc.key": 4, 1: 5, (2, 3): 6},
...     "key",
... )
{'abc': 3, 'abc.key': 4, 1: 5, (2, 3): 6}

coola.nested.to_flat_dict

to_flat_dict(
    data: object,
    prefix: str | None = None,
    separator: str = ".",
    to_str: type[Any] | tuple[type[Any], ...] | None = None,
) -> dict[str, Any]

Return a flat representation of a nested dict with the dot format.

Parameters:

Name Type Description Default
data object

The nested dict to flat.

required
prefix str | None

The prefix to use to generate the name of the key. None means no prefix.

None
separator str

The separator to concatenate keys of nested collections.

'.'
to_str type[Any] | tuple[type[Any], ...] | None

The data types which will not be flattened out, instead converted to string.

None

Returns:

Type Description
dict[str, Any]

The flatted dictionary.

Example
>>> from coola.nested import to_flat_dict
>>> data = {
...     "str": "def",
...     "module": {
...         "component": {
...             "float": 3.5,
...             "int": 2,
...         },
...     },
... }
>>> to_flat_dict(data)
{'str': 'def', 'module.component.float': 3.5, 'module.component.int': 2}
>>> # Example with lists (also works with tuple)
>>> data = {
...     "module": [[1, 2, 3], {"bool": True}],
...     "str": "abc",
... }
>>> to_flat_dict(data)
{'module.0.0': 1, 'module.0.1': 2, 'module.0.2': 3, 'module.1.bool': True, 'str': 'abc'}
>>> # Example with lists with to_str=(list) (also works with tuple)
>>> data = {
...     "module": [[1, 2, 3], {"bool": True}],
...     "str": "abc",
... }
>>> to_flat_dict(data)
{'module.0.0': 1, 'module.0.1': 2, 'module.0.2': 3, 'module.1.bool': True, 'str': 'abc'}