Utils#

sigmaepsilon.deepdict.utils.dictparser(d: dict, *, dtype=<class 'dict'>, **_kw) Iterable[Tuple[List[Hashable], Any]]#

Iterates through all the values of a nested dictionary.

Notes

Returns all kinds of items, even nested discionaries themselves, along with their content.

Example

>>> from sigmaepsilon.deepdict import dictparser
>>> d = {
...     "a" : {"aa" : 1},
...     "b" : 2,
...     "c" : {"cc" : {"ccc" : 3}},
... }
>>> for address, value in dictparser(d):
...     print(f"Address: {address}, Value: {value}")
Address: ['a', 'aa'], Value: 1
Address: ['b'], Value: 2
Address: ['c', 'cc', 'ccc'], Value: 3
sigmaepsilon.deepdict.utils.parseaddress(d: dict, address: List[Hashable]) Any#

Returns a value specified with an address.

Example

>>> from sigmaepsilon.deepdict import parseaddress
>>> d = {
...     "a" : {"aa" : 1},
...     "b" : 2,
...     "c" : {"cc" : {"ccc" : 3}},
... }
>>> parseaddress(d, ['c', 'cc', 'ccc'])
3
sigmaepsilon.deepdict.utils.parsedicts(d: dict, *, inclusive: bool = True, dtype: ~typing.Any = <class 'dict'>, deep: bool = True) Iterable[dict]#

Returns all subdirectories of a dictionary.

Example

>>> from sigmaepsilon.deepdict import parsedicts
>>> d = {
...     "a" : {"aa" : 1},
...     "b" : 2,
...     "c" : {"cc" : {"ccc" : 3}},
... }
>>> for subd in parsedicts(d):
...     print(subd)
{'a': {'aa': 1}, 'b': 2, 'c': {'cc': {'ccc': 3}}}
{'aa': 1}
{'cc': {'ccc': 3}}
{'ccc': 3}
>>> for subd in parsedicts(d, inclusive=False):
...     print(subd)
{'aa': 1}
{'cc': {'ccc': 3}}
{'ccc': 3}
sigmaepsilon.deepdict.utils.parsedicts_addr(d: dict, *, inclusive: bool = True, dtype: ~typing.Any = <class 'dict'>, deep: bool = True, **_kw) Tuple[List[Hashable], Iterable[dict]]#

Returns all subdirectories of a dictionary and their addresses.

Example

>>> from sigmaepsilon.deepdict import parsedicts_addr
>>> d = {
...     "a" : {"aa" : 1},
...     "b" : 2,
...     "c" : {"cc" : {"ccc" : 3}},
... }
>>> for addr, subd in parsedicts_addr(d):
...     print(f"{subd} @ {addr}")
{'a': {'aa': 1}, 'b': 2, 'c': {'cc': {'ccc': 3}}} @ []
{'aa': 1} @ ['a']
{'cc': {'ccc': 3}} @ ['c']
{'ccc': 3} @ ['c', 'cc']
>>> for addr, subd in parsedicts_addr(d, inclusive=False):
...     print(f"{subd} @ {addr}")
{'aa': 1} @ ['a']
{'cc': {'ccc': 3}} @ ['c']
{'ccc': 3} @ ['c', 'cc']
sigmaepsilon.deepdict.utils.parseitems(d: dict, *, dtype: ~typing.Any = <class 'dict'>) Iterable[Tuple[Hashable, Any]]#

A generator function that yields all the items of a nested dictionary as (key, value) pairs.

Notes

Does not return nested dictionaries themselves, only their content.

Example

>>> from sigmaepsilon.deepdict import parseitems
>>> d = {
...     "a" : {"aa" : 1},
...     "b" : 2,
...     "c" : {"cc" : {"ccc" : 3}},
... }
>>> for key, value in parseitems(d):
...     print(f"Key: {key}, Value: {value}")
Key: aa, Value: 1
Key: b, Value: 2
Key: ccc, Value: 3