AttrsDictΒΆ
- class dbetto.attrsdict.AttrsDict(*_, **__)
Access dictionary items as attributes.
Examples
>>> d = AttrsDict({"key1": {"key2": 1}}) >>> d.key1.key2 1 >>> d1 = AttrsDict() >>> d1["a"] = 1 >>> d1.a 1
- clear()
- Return type:
- 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.
- pop(*args)
If the key is not found, return the default if given; otherwise, raise a KeyError.
- popitem()
Remove and return a (key, value) pair as a 2-tuple.
Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.
- Return type:
- reset()
Reset this instance by removing all cached data.
- Return type:
- setdefault(key, default=None)
Insert key with a value of default if key is not in the dictionary.
Return the value for key if key is in the dictionary, else default.
- to_dict()
Return a plain
dictrepresentation of the object.Nested
AttrsDictinstances and lists are recursively converted to built-in containers to ensure the result is fully serialisable by callers expecting standard dictionaries.- Return type:
- update(*args, **kwargs)
If E is present and has a .keys() method, then does: for k in E.keys(): D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]