4.7.11 pde.tools.nested_dict module
Provides a nested dictionary that stores hierarchical mappings.
Stores hierarchical mappings with string paths as keys. |
- class NestedDict(data=None)[source]
Bases:
MutableMapping[str,TValue|NestedDict[TValue]],Generic[TValue]Stores hierarchical mappings with string paths as keys.
NestedDict wraps nested mappings and supports reading and writing nested values using a separator-based key syntax (for example
"a.b.c"). It can convert between flat and nested representations and recursively traverses children when requested.Note
Equivalent entries can overwrite each other during initialization. For instance,
NestedDict({'a.b': 1, 'a': {'b': 2}})stores only one final value fora.b.Initializes a nested dictionary from an optional mapping.
- Parameters:
data (MutableMapping[str, Any] | None) – Optional mapping used to populate the instance. Nested plain dictionaries are converted into NestedDict children.
- copy()[source]
Creates a structural copy with copied nested mappings.
Child dictionaries and child NestedDict instances are copied, while non-mapping leaf values are reused by reference.
- Returns:
New instance containing copied nested structure.
- Return type:
- create_node(key)[source]
Create an empty node at the given location.
Creates all necessary parent nodes recursively. Skips nodes that already exist.
- data: MutableMapping[str, TValue | NestedDict[TValue]]
Internal mapping storing top-level keys and values for this instance.
- Type:
- items(*, flatten: Literal[False] = False) Iterator[tuple[str, TValue | NestedDict[TValue]]][source]
- items(*, flatten: Literal[True]) Iterator[tuple[str, TValue]]
Iterates over key-value pairs, optionally flattening nested paths.
- pprint(*args, flatten=False, **kwargs)[source]
Pretty-prints the current data as nested plain dictionaries.
- Parameters:
*args (Any) – Positional arguments forwarded to pprint.pprint.
flatten (bool) – If True, returns a flat mapping with separator-joined paths as keys. If False, returns nested dictionaries.
**kwargs (Any) – Keyword arguments forwarded to pprint.pprint.
- Return type:
None
- to_dict(*, flatten: Literal[False] = False) dict[str, TValue | TDictTree][source]
- to_dict(*, flatten: Literal[True]) dict[str, TValue]
Converts this object to a plain dictionary representation.
- update(other)[source]
Update this mapping from another mapping recursively.
This method implements
collections.abc.MutableMappingupdate semantics for mapping-like inputs and forwards the actual merge toupdate_recursive().- Parameters:
other – Mapping containing keys and values to merge into this instance.
- Raises:
TypeError – If other is not a mutable mapping.
- Return type:
None
- update_recursive(other)[source]
Recursively merges another mapping into this instance.
- Parameters:
other (MutableMapping[str, Any]) – Mapping whose entries are merged into this object. If both sides contain nested mappings at a key, values are merged recursively.
- Return type:
None
- values(*, flatten: Literal[False] = False) Iterator[TValue | NestedDict[TValue]][source]
- values(*, flatten: Literal[True]) Iterator[TValue]
Iterates over values, optionally recursing into nested children.
- Parameters:
flatten (bool) – If True, yields values from all descendant NestedDict instances. If False, yields only top-level values.
- Returns:
Iterator over values according to flatten.
- Return type:
Iterator[Any]