chunklet.common.dotdict
DotDict/DotList: dict and list subclasses with dot-notation access.
This module embeds code originally from dotdict3 (MIT License).
Co-authored by:
- speedyk-005
- Patrick Elmer patrick@elmer.ws (https://github.com/dotdict/dotdict3)
Classes:
-
DotDict–A dict subclass with dot-notation access and automatic nested conversion.
-
DotList–A list subclass with automatic nested dict/list conversion.
DotDict
Bases: dict
A dict subclass with dot-notation access and automatic nested conversion.
Basic usage::
>>> d = DotDict({"name": "John", "age": 30})
>>> d.name
'John'
>>> d.age
30
Nested dicts auto-convert::
>>> d = DotDict({"user": {"name": "Bob", "age": 25}})
>>> d.user.name
'Bob'
>>> d.user.age
25
Lists auto-convert to DotList::
>>> d = DotDict({"users": [{"name": "Alice"}, {"name": "Bob"}]})
>>> d.users[0].name
'Alice'
>>> isinstance(d.users, DotList)
True
Dot notation assignment::
>>> d = DotDict()
>>> d.name = "Alice"
>>> d.name
'Alice'
Methods:
-
to_std_dict–Recursively convert back to plain
dict/listcontainers.
Source code in src/chunklet/common/dotdict.py
to_std_dict
Recursively convert back to plain dict/list containers.
DotDict is itself a dict, so this is not needed for equality or
for json. It is needed when a consumer requires exact standard
types, e.g. yaml.dump emits !!python/object tags for dict
subclasses.
d = DotDict({"a": {"b": 1}, "c": [{"d": 2}]}) d.to_std_dict() {'a': {'b': 1}, 'c': [{'d': 2}]}
Source code in src/chunklet/common/dotdict.py
DotList
Bases: list
A list subclass with automatic nested dict/list conversion.
l = DotList([{"a": 1}, {"b": 2}]) l[0].a 1 l[1].b 2
Methods:
-
to_std_dict–Recursively convert back to plain
listcontainers.
Source code in src/chunklet/common/dotdict.py
to_std_dict
Recursively convert back to plain list containers.
DotList([DotDict({"a": 1}), DotDict({"b": 2})]).to_std_dict() [{'a': 1}, {'b': 2}]