energia.utils.dictionary#

dictionary utils

Functions

compare(tree, of)

How many periods make this period

dict_signature(d)

Hashable signature of a dict for grouping.

dictify(d)

Recursively convert defaultdict to dict.

flatten(d[, key])

Makes a flat dictionary from a nested dictionary.

get_depth(d)

Finds the depth of a dictionary.

is_(a, b, /)

Same as a is b.

merge_tree_levels(d)

Convert nested dict to level-wise compact structure, merging repeated keys at all levels.

merge_trees(d1, d2)

Recursively merge two tree-like dicts (values always dicts).

tupler(d[, path])

Makes a list of tuples of keys in a nested dictionary.

Classes

defaultdict

defaultdict(default_factory=None, /, [...]) --> dict with default factory

Exceptions

NotFoundError

Raised when two periods are not linked in the period tree.

get_depth(d: dict) int[source]#

Finds the depth of a dictionary.

Parameters:

d (dict) – The dictionary to measure.

Returns:

Depth of the dictionary.

Return type:

int

flatten(d: dict, key: tuple = ()) dict[source]#

Makes a flat dictionary from a nested dictionary.

Parameters:
  • d (dict) – The dictionary to flatten.

  • key (tuple, optional) – Current key path. Defaults to ().

Returns:

Flattened dictionary with tuple keys.

Return type:

dict

tupler(d: dict, path: tuple = ()) list[tuple[str]][source]#

Makes a list of tuples of keys in a nested dictionary.

Parameters:
  • d (dict) – The dictionary to traverse.

  • path (tuple, optional) – Current path. Defaults to ().

Returns:

List of tuples of keys representing the paths to each value.

Return type:

list[tuple[str]]

merge_trees(d1: dict, d2: dict) dict[source]#

Recursively merge two tree-like dicts (values always dicts).

Parameters:
  • d1 (dict) – First dictionary.

  • d2 (dict) – Second dictionary.

Returns:

Merged dictionary.

Return type:

dict

dictify(d: defaultdict | dict) dict[source]#

Recursively convert defaultdict to dict.

Parameters:

d (defaultdict | dict) – The dictionary to convert.

exception NotFoundError[source]#

Bases: Exception

Raised when two periods are not linked in the period tree.

add_note(object, /)#

Exception.add_note(note) – add a note to the exception

args#
with_traceback(object, /)#

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

compare(tree: dict, of)[source]#

How many periods make this period

dict_signature(d)[source]#

Hashable signature of a dict for grouping.

merge_tree_levels(d)[source]#

Convert nested dict to level-wise compact structure, merging repeated keys at all levels.