dict module

class toolbox.dict.DotDict

Bases: dict

A dictionary that supports attribute-style access (dot notation).

Example:

d = DotDict(a=1) print(d.a) # 1

toolbox.dict.deep_copy_dict(d: Dict[Any, Any]) Dict[Any, Any]

Create a deep copy of a nested dictionary.

Args:

d (Dict[Any, Any]): The dictionary to copy.

Returns:

Dict[Any, Any]: A deep copy of the dictionary.

toolbox.dict.filter_dict(d: Dict[Any, Any], predicate: callable) Dict[Any, Any]

Filter a dictionary based on a predicate function.

Args:

d (Dict[Any, Any]): The dictionary to filter. predicate (callable): Function that takes (key, value) and returns bool.

Returns:

Dict[Any, Any]: Filtered dictionary.

toolbox.dict.flatten_dict(d: Dict[Any, Any], parent_key: str = '', sep: str = '.') Dict[str, Any]

Flatten a nested dictionary, joining keys with a separator.

Args:

d (Dict[Any, Any]): The dictionary to flatten. parent_key (str, optional): The base key for recursion. Defaults to ‘’. sep (str, optional): Separator between keys. Defaults to ‘.’.

Returns:

Dict[str, Any]: A flat dictionary with joined key paths.

toolbox.dict.get_nested(d: Dict[Any, Any], path: str, default: Any = None, sep: str = '.') Any

Get a value from a nested dictionary using a dotted path.

Args:

d (Dict[Any, Any]): The nested dictionary. path (str): The dotted path to the value (e.g., ‘a.b.c’). default (Any, optional): Default value if path not found. Defaults to None. sep (str, optional): Path separator. Defaults to ‘.’.

Returns:

Any: The value at the path or the default.

toolbox.dict.invert_dict(d: Dict[Any, Any]) Dict[Any, Any]

Invert a dictionary (swap keys and values).

Args:

d (Dict[Any, Any]): The dictionary to invert.

Returns:

Dict[Any, Any]: Inverted dictionary with values as keys.

toolbox.dict.merge_dicts(dict1: Dict[Any, Any], dict2: Dict[Any, Any]) Dict[Any, Any]

Recursively merge two dictionaries.

Args:

dict1 (Dict[Any, Any]): The base dictionary. dict2 (Dict[Any, Any]): The dictionary to merge into the base.

Returns:

Dict[Any, Any]: The merged dictionary.

toolbox.dict.set_nested(d: Dict[Any, Any], path: str, value: Any, sep: str = '.') None

Set a value in a nested dictionary using a dotted path.

Args:

d (Dict[Any, Any]): The nested dictionary to modify. path (str): The dotted path to set (e.g., ‘a.b.c’). value (Any): The value to set. sep (str, optional): Path separator. Defaults to ‘.’.

toolbox.dict.unflatten_dict(d: Dict[str, Any], sep: str = '.') Dict[Any, Any]

Unflatten a dictionary with joined keys back into a nested structure.

Args:

d (Dict[str, Any]): The flattened dictionary. sep (str, optional): Separator used in keys. Defaults to ‘.’.

Returns:

Dict[Any, Any]: A nested dictionary.