| Index | Module Index | Search Page |
qnd.adict module¶
An items-as-attributes dict.
-
class
qnd.adict.ADict(*args, **kwargs)[source]¶ Bases:
qnd.adict.ItemsAreAttrs,dictSubclass of dict permitting access to items as if they were attributes.
For a ADict ad,
ad.xis equivalent toad['x']for getting, setting, or deleting items. The exceptions are dict method names, like keys or items, syntactically illegal names, like class or yield, and any name beginning with __.Additionally, as a work around for some of these exceptions, ADict will remove a single trailing underscore from an attribute name, so
ad.x_is also equivalent toad['x'], and you needad.x__to getad['x_'](a convention inspired by the similar PEP8 recommendation for syntatically illegal variable names). The trailing underscore removal does not apply to names beginning with __.The trailing underscore removal convention applies to keywords passed to the constructor or to the update method as well.
Use subscript syntax when a variable or expression holds an item name; use attribute syntax when you know the item name at parse time:
ad[variable] = value # value of variable is the item name ad.fixed = value # 'fixed' is the item name value = ad.get('fixed', default) # except to avoid KeyError
See also
redict- recursively toggle between dict and ADict
ItemsAreAttrs- mixin base class to provide this for any class
-
class
qnd.adict.ItemsAreAttrs[source]¶ Bases:
objectMix-in class for QArray, QGroup, or QList, and also ADict.
-
qnd.adict.redict(d, cls=None)[source]¶ Recursively convert a nested dict to a nested ADict and vice versa.
Parameters: - d (dict or ADict instance) – A dict, possibly nested, to be converted.
- cls (dict or subclass of dict, optional) – The dict-like cls to recursively convert d and any sub-dicts into. By default, if d is a ADict, cls is dict, otherwise cls is ADict, so repeated calls to redict toggle between dict and ADict.
Returns: dnew – A copy of d whose class is cls. Any items which are dict instances are similarly copied to be cls instances. Non-dict items are not copied unless assignment makes copies.
Return type: dict or ADict