codebox: Dictionary utilities¶
Dictionary utility tools
- codebox.dict_tools.get_nested(the_dict: dict, nested_key: str, default: Optional[Any] = None) Any [source]¶
Returns the value of a nested key in an dictorinary
- Parameters
Returns:
Examples
>>> t = {'a': {'b': {'c':'value_of_c'}}} >>> get_nested(t, 'b:c') 'value_of_c' >>> get_nested(t, 'b:c:d', 'not found') 'not found'
- codebox.dict_tools.merge(*dicts: dict) dict [source]¶
Deep merges the right most dictionaries into the left most one.
- Parameters
*dicts (dict) – dictionaries to be merged.
Examples
>>> a = {'a': 1, 'b': 2} >>> b = {'b': 3, 'c': {'c1': 4, 'c2': 3}} >>> c = {'a': 3, 'c': {'c1': 3 }} >>> print(merge(a,b,c)) {'c': {'c1': 3, 'c2': 3}, 'a': 3, 'b': 3}
- Returns
dict
- codebox.dict_tools.update_nested(the_dict: dict, nested_key: str, value: Any)[source]¶
Updates nested keys inside a dictionary
- Parameters
Examples
>>> t = {} >>> update_nested(t, 'a:b:c', 1) >>> t {'a': {'b': {'c': 1}}} >>> update_nested(t, 'a:b:d', 1) >>> t {'a': {'b': {'c': 1, 'd': 1}}} >>> update_nested(t, 'a:b:c', 2) >>> t {'a': {'b': {'c': 2, 'd': 1}}}