Attribute dictionary (attributedict)#

Classes:

AttributeDict([entries, recursive, ...])

Dictionary with recursive attribute like access.

class module_utilities.attributedict.AttributeDict(entries=None, recursive=True, allow_missing=True)[source]#

Bases: MutableMapping[str, str | NestedMap]

Dictionary with recursive attribute like access.

To be used in str.format calls, so can expand on fields like {name.property} in a nested manner.

Parameters:
  • entries (dict)

  • recursive (bool, default True) – If True, recursively return AttributeDict for nested dicts.

  • allow_missing (bool, default True) – If True, allow missing keys.

Example

>>> d = AttributeDict({"a": 1, "b": {"c": 2}})
>>> d.a
1
>>> d.b
AttributeDict({'c': 2})
>>> d.b.c
2

Methods:

from_dict(params[, max_level, recursive])

Create AttributeDict recursively for nested dictionaries.

classmethod from_dict(params, max_level=1, recursive=True)[source]#

Create AttributeDict recursively for nested dictionaries.

To be used in cases where need to apply AttributeDict to parameters passed with func(**params).

Parameters:
  • params (mapping) – Mapping to apply cls to.

  • max_level (int, default 1) – How deep to apply cls to.

  • recursive (bool, default True) – recursive parameter of resulting object.

Returns:

AttributeDict

Examples

>>> d = {"a": "p0", "b": {"c": {"d": "d0"}}}
>>> AttributeDict.from_dict(d, max_level=1)
AttributeDict({'a': 'p0', 'b': AttributeDict({'c': {'d': 'd0'}})})
>>> AttributeDict.from_dict(d, max_level=2)
AttributeDict({'a': 'p0', 'b': AttributeDict({'c': AttributeDict({'d': 'd0'})})})