gwadama.dictools#

dictools.py

Collection of utility functions related to nested Python dictionaries.

gwadama.dictools.dict_to_stacked_array(dict_: dict, target_length: int = None) tuple[ndarray, list][source]#

Stack the arrays inside a dict() to a 2d-array.

Given a NON-nested dict whose values are flat numpy arrays, with potentially different lengths, stacks them in a homogeneous 2d-array aligned to the left, zero-padding the remaining space.

Parameters:
dict_dict[str: np.ndarray]

NON-nested Python dictionary containing numpy 1d-arrays.

target_lengthint, optional

If given, defines the size of the second axis of the returned 2d-array. If omitted, the size will be equal to the longest array inside ‘dict_’. Must be larger or equal than the longest array inside ‘dict_’.

Returns:
stacked_arrays2d-array

Stacked arrays, with right zero-padding those original strains whose length were shorter.

lengthslist

Original length of each input array, following the same order as the first axis of ‘stacked_arrays’.

gwadama.dictools.fill(dict_: dict, value, keys=None, deepcopy=False)[source]#

Fill an arbitrarily-depth nested dictionary with a value.

Fill an arbitrarily-depth nested dictionary below the coordinates ‘keys’ with the value ‘value’.

The filling is performed inplace.

Parameters:
dict_: dict

Nested dictionary.

value: Any
keys: iterable, optional

Starting layers from where to fill the dictionary, if only a subset of the whole ‘dict_’ is desired to be filled.

deepcopy: bool

If True, each instance of ‘value’ will be a copy. By default all elements reference the same ‘value’.

gwadama.dictools.filter_nested_dict(dict_, condition, layer) dict[source]#

Filter a layer of a nested dictionary.

Filter a nested dictionary based on a condition applied to the keys of the specified layer.

NOTE: Layer numbering begins with 0, as array-likes do; as God commands.

Parameters:
dict_dict

The nested dictionary to be filtered.

conditioncallable

The condition function to apply. Should take a single argument, the key, and return a boolean indicating wether to include its related value.

layerint

The layer at which to apply the condition. 1 corresponds to the top level, 2 to the second level, and so on. Default is 1.

Returns:
: dict

Filtered version of the nested dictionary.

gwadama.dictools.flatten_nested_dict(dict_: dict) dict[source]#

Turn any nested dictionary into a shallow (single level) one.

Flatten a nested dictionary into a single level dictionary, keeping their keys as tuples.

gwadama.dictools.get_depth(dict_: dict) int[source]#

Return the depth of the input nested dictionary.

A simple (non-nested) dictionary has a depth of 1. Assumes a homogeneous nested dictionary, and only looks for the first element at each layer.

gwadama.dictools.get_next_item(dict_)[source]#

Get the next item in a nested dictionary.

Returns:
valueAny

Value of the next item in the dictionary.

gwadama.dictools.get_number_of_elements(dict_)[source]#

Get the number of elements in a nested dictionary.

Parameters:
dict_dict

Nested dictionary.

Returns:
numberint

Number of elements in the nested dictionary.

gwadama.dictools.get_value_from_nested_dict(dict_, keys: list)[source]#

Get a value from an arbitrarily-depth nested dictionary.

Parameters:
dict_: dict

Nested dictionary.

keys: list

Sequence of keys necessary to get to the element inside the nested dictionary.

Returns:
: Any

Value of the element inside the nested dictionary.

gwadama.dictools.set_value_to_nested_dict(dict_, keys, value, add_missing_keys=False)[source]#

Set a value to an arbitrarily-depth nested dictionary.

Parameters:
dict_: dict

Nested dictionary.

keys: iterable

Sequence of keys necessary to get to the element inside the nested dictionary.

value: Any
add_missing_keys: bool

If True, missing keys (layers) will be added to the nested dictionary.

CAUTION: if add_missing_keys=True, no KeyError will be raised.

gwadama.dictools.unroll_nested_dictionary_keys(dict_: dict, max_depth: int = None) list[source]#

Returns a list of all combinations of keys inside a nested dictionary.

Useful to iterate over all keys of a nested dictionary without having to use multiple loops.

Parameters:
dictionary: dict

Nested dictionary.

max_depth: int, optional

If specified, it is the number of layers to dig in to at most in the nested dictionary. If only the first layer is desired (no recursion at all), max_depth=1.

Returns:
: list

Unrolled combinations of all keys of the nested dictionary.