DeepDict - A nested dictionary class with a self replicating default factory#
- class nddict.DeepDict(*args, parent=None, root=None, locked=None, **kwargs)#
An nested dictionary class with a self-replicating default factory. It can be a drop-in replacement for the bulit-in dictionary type, but it’s more capable as it handles nested layouts.
Examples
Basic usage:
>>> from nddict import DeepDict >>> d = {'a' : {'aa' : {'aaa' : 0}}, 'b' : 1, 'c' : {'cc' : 2}} >>> dd = DeepDict(d) >>> list(dd.values(deep=True)) [0, 1, 2]
See the docs for more use cases!
- __init__(*args, parent=None, root=None, locked=None, **kwargs)#
Returns a DeepDict instance.
- Parameters
*args (tuple, Optional) – Extra positional arguments are forwarded to the dict class.
parent (DeepDict, Optional) – Parent DeepDict instance. Default is None.
root (DeepDict, Optional) – The top-level object. It is automatically set when creating nested layouts, but may be explicitly provided. Default is None.
locked (bool or NoneType, Optional) – If the object is locked, it reacts to missing keys as a regular dictionary would. If it is not, a new level and a new child is created (see the examples in the docs). A None value means that in terms of locking, the state of the object is inherited from its parent. Default is None.
**kwargs (tuple, Optional) – Extra keyword arguments are forwarded to the dict class.
- containers(*args, inclusive=False, deep=True, dtype=None, **kwargs)#
Returns all the containers in a nested layout. A dictionary in a nested layout is called a container, only if it contains other containers (it is a parent).
- Parameters
inclusive (bool, Optional) – If True, the object the call is made upon also gets returned. This can be important if you make the call on the root object, which most of the time does not hold onto relevant data directly. Default is False.
deep (bool, Optional) – If True the parser goes into nested dictionaries. Default is True
dtype (Any, Optional) – Constrains the type of the returned objects. Default is None, which means no restriction.
- Returns
Returns a generator object.
- Return type
generator
Examples
A simple example:
>>> from nddict import DeepDict >>> data = DeepDict() >>> data['a', 'b', 'c'] = 1 >>> [c.key for c in data.containers()] ['a', 'b']
We can see, that dictionaries ‘a’ and ‘b’ are returned as containers, but ‘c’ isn’t, because it is not a parent, there are no deeper levels.
>>> [c.key for c in data.containers(inclusive=True, deep=True)] [None, 'a', 'b']
>>> [c.key for c in data.containers(inclusive=True, deep=False)] [None, 'a']
>>> [c.key for c in data.containers(inclusive=False, deep=True)] ['a', 'b']
>>> [c.key for c in data.containers(inclusive=False, deep=False)] ['a']
- property depth: int#
Retuns the depth of the actual instance in a layout, starting from 0..
- is_root() bool#
Returns True, if the object is the root.
- property key: Optional[Hashable]#
Returns the key of the dictionary in its parent, or None if the object is the root.
- lock()#
Locks the layout of the dictionary. If a DeepDict is locked, missing keys are handled the same way as they would’ve been handled if it was a ´dict´.
Equivalent to
self.locked = True.
- property locked: bool#
Returns True if the object is locked. The property is equpped with a setter.
- root()#
Returns the top-level object in a nested layout.
- unlock()#
Releases the layout of the dictionary. If a DeepDict is not locked, a missing key creates a new level in the layout.
Equivalent to
self.locked = False.