sovabids.dicts#

Module with dictionary utilities.

Module Contents#

Functions#

deep_get(dictionary, keys[, default, sep])

Safe nested dictionary getter.

deep_merge_N(l)

Merge the list of dictionaries, such that the latest one has the greater precedence.

deep_merge(a, b)

Merge two values, with b taking precedence over a.

flatten(d[, parent_key, sep])

Flatten the nested dictionary structure using the given separator.

nested_notation_to_tree(key, value[, leaf])

Create a nested dictionary from the single (key,value) pair, with the key being branched by the leaf separator.

sovabids.dicts.deep_get(dictionary, keys, default=None, sep='.')[source]#

Safe nested dictionary getter.

Parameters
  • dictionary (dict) – The dictionary from which to get the value.

  • keys (str) – The nested keys using sep as separator. Ie: ‘person.name.lastname’ if `sep`=’.’

  • default (object) – The default value to return if the key is not found

  • sep (str, optional) – The separator to indicate nesting/branching/hierarchy.

Returns

The value of the required key. default if the key is not found.

Return type

object

Notes

Taken from https://stackoverflow.com/a/46890853/14068216

sovabids.dicts.deep_merge_N(l)[source]#

Merge the list of dictionaries, such that the latest one has the greater precedence.

Parameters

l (list of dict) – List containing the dictionaries to be merged, having precedence on the last ones.

Returns

The merged dictionary.

Return type

dict

sovabids.dicts.deep_merge(a, b)[source]#

Merge two values, with b taking precedence over a.

Semantics: - If either a or b is not a dictionary, a will be returned only if

b is None. Otherwise b will be returned.

  • If both values are dictionaries, they are merged as follows:
    • Each key that is found only in a or only in b will be included in the output collection with its value intact.

    • For any key in common between a and b, the corresponding values will be merged with the same semantics.

From David Schneider answer at https://stackoverflow.com/questions/7204805/how-to-merge-dictionaries-of-dictionaries/15836901#15836901

Parameters
  • a (object) –

  • b (object) –

Returns

Merged dictionary.

Return type

dict

sovabids.dicts.flatten(d, parent_key='', sep='.')[source]#

Flatten the nested dictionary structure using the given separator.

If parent_key is given, then that level is added at the start of the tree.

Parameters
  • d (dict) – The dictionary to flat.

  • parent_key (str, optional) – The optional top-level field of the dictionary.

  • sep (str, optional) – The separator to indicate nesting/branching/hierarchy.

Returns

A dictionary with only one level of fields.

Return type

dict

sovabids.dicts.nested_notation_to_tree(key, value, leaf='.')[source]#

Create a nested dictionary from the single (key,value) pair, with the key being branched by the leaf separator.

Parameters
  • key (str) – The key/field to be nested, assuming nesting is represented with the “leaf” parameters.

  • value (object) – The value that it will have at the last level of nesting.

  • leaf (str, optional) – The separator used to indicate nesting in “key” parameter.

Returns

Nested dictionary.

Return type

dict