Skip to content

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

DotDict(data=None)

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/list containers.

Source code in src/chunklet/common/dotdict.py
def __init__(self, data=None):
    if data is not None:
        for key, value in data.items():
            self[key] = value

to_std_dict

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
def to_std_dict(self):
    """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}]}
    """
    return {k: _to_plain(v) for k, v in self.items()}

DotList

DotList(items=None)

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 list containers.

Source code in src/chunklet/common/dotdict.py
def __init__(self, items=None):
    if items is not None:
        for item in items:
            self.append(item)

to_std_dict

to_std_dict()

Recursively convert back to plain list containers.

DotList([DotDict({"a": 1}), DotDict({"b": 2})]).to_std_dict() [{'a': 1}, {'b': 2}]

Source code in src/chunklet/common/dotdict.py
def to_std_dict(self):
    """Recursively convert back to plain ``list`` containers.

    >>> DotList([DotDict({"a": 1}), DotDict({"b": 2})]).to_std_dict()
    [{'a': 1}, {'b': 2}]
    """
    return [_to_plain(v) for v in self]