beanbag.attrdict – Access dict members by attribute¶
AttrDict¶
This module provides the AttrDict
class, which allows you to
access dict members via attribute access, allowing similar syntax to
javascript objects. For example:
d = {"foo": 1, "bar": {"sub": {"subsub": 2}}}
ad = AttrDict(d)
assert ad["foo"] == ad["foo"]
assert ad.foo == 1
assert ad.bar.sub.subsub == 2
Note that AttrDict
simply provides a view on the native dict. That dict
can be obtained using the plus operator like so:
ad = AttrDict(d)
assert +ad is d
This allows use of native dict methods such as d.update()
or
d.items()
. Note that attribute access binds more tightly than plus,
so brackets will usually need to be used, eg: (+ad.bar).items()
.
An AttrDict
can also be directly used as an iterator (for key in
attrdict: ...
) and as a container (if key in attrdict: ...
).
-
class
beanbag.attrdict.
AttrDict
(base=None)¶ -
__delattr__
(attr)¶ del self.attr
-
__delitem__
(item)¶ del self[item]
-
__eq__
(other)¶ self == other
-
__getattr__
(attr)¶ self.attr
-
__getitem__
(item)¶ self[attr]
-
__init__
(base=None)¶ Provide an AttrDict view of a dictionary.
Parameters: base – dictionary/list to be viewed
-
__ne__
(other)¶ self != other
-
__pos__
()¶ View underlying dict object
-
__repr__
()¶ Human readable representation of object
-
__setattr__
(attr, val)¶ self.attr = val
-
__setitem__
(item, val)¶ self[item] = val
-
__str__
()¶ Returns path joined by dots
-