AttrsDictΒΆ
- class dbetto.attrsdict.AttrsDict(value=None)
Access dictionary items as attributes.
Examples
>>> d = AttrsDict({"key1": {"key2": 1}}) >>> d.key1.key2 1 >>> d1 = AttrsDict() >>> d1["a"] = 1 >>> d1.a 1
- group(label)
Group dictionary according to a label.
This is equivalent to
map()with unique set toFalse.- Parameters:
label (
str) β name (key) at which the new label can be found. If nested in dictionaries, use.to separate levels, e.g.level1.level2.label.- Return type:
Examples
>>> d = AttrsDict({ ... "a": { ... "type": "A", ... "data": 1 ... }, ... "b": { ... "type": "A", ... "data": 2 ... }, ... "c": { ... "type": "B", ... "data": 3 ... }, ... }) >>> d.group("type").keys() dict_keys(['A', 'B']) >>> d.group("type").A.values() dict_values([{'type': 'A', 'data': 1}, {'type': 'A', 'data': 2}]) >>> d.group("type").B.values() dict_values([{'type': 'B', 'data': 3}]) >>> d.group("type").A.map("data")[1] {'type': 'A', 'data': 1}
See also
- map(label, unique=True)
Remap dictionary according to an alternative unique label.
Loop over keys in the first level and search for key named label in their values. If label is found and its value newid is unique, create a mapping between newid and the first-level dictionary obj. If label is of the form
key.label,labelwill be searched in a dictionary keyed bykey. If the label is unique a dictionary of dictionaries will be returned, if not unique and unique is false, a dictionary will be returned where each entry is a dictionary of dictionaries keyed by an arbitrary integer.- Parameters:
- Return type:
Examples
>>> d = AttrsDict({ ... "a": { ... "id": 1, ... "group": { ... "id": 3, ... }, ... "data": "x" ... }, ... "b": { ... "id": 2, ... "group": { ... "id": 4, ... }, ... "data": "y" ... }, ... }) >>> d.map("id")[1].data == "x" True >>> d.map("group.id")[4].data == "y" True
Note
No copy is performed, the returned dictionary is made of references to the original objects.
Warning
The result is cached internally for fast access after the first call. If the dictionary is modified, the cache gets cleared.
- reset()
Reset this instance by removing all cached data.
- Return type: