Package: pydictobject
dictobjectThere are a number of excellent packages that extend the python dictionary to all object-like access (e.g. dict.key).This aims to add some additional features to those projects by creating a python dictionary that is accessible (like an object) through attributes and allows default value and warningspydictobject adds to the python dict by:Allowing access through the dict.key notationSetting new items with dict.key = value notationAllowing a default value to be set, which is returned if the key is not in the dictionaryOptionally warning if the key is not found and default value is being returnedRecursively changing any nested dicts to DictObjects and back againAdditionally, deepcopy has been implemented to create new copies of any nested DictObjects.Installationpip install pydictobjectImportfrompydictobjectimportDictObjectInitialisedob=DictObject({'foo':1,'bar':2})Parameters:input_dict(dict): Optional. If not set, an empty dictionary is initialised.default_to: Optional. If set, this is the value that is returned if a key is not in the dictionary.warn_key_not_found (bool): Optional prints a warning if a key is not found and default_to is returned. Defaults to False.Example usagedob=DictObject({'foo':1,'bar':2})>>>dob.foo1>>>dob['bar']2>>>dob.baz=3# Set new value with dot notation>>>dob.baz3Return default value if key not found>>>dob=DictObject({'foo':1,'bar':2},default_to='Key Not Set')>>>dob.baz'Key Not Set'>>>dob.baz=3>>>dob.baz3# set default_to after instantiation>>>dob=DictObject({'foo':1,'bar':2})>>>dob.bootle# Raises KeyError>>>dob.set_default_to['Key not set']>>>dob.bootle'Key not set'Nesting(See later for converting nested dictionaries)dob=DictObject({'foo':1,'bar':2})dob.baz=DictObject({'a':3,'b':4})dob# Note: Display indenting added for clarityDictObject({'foo':1,'bar':2,'baz':DictObject({'a':3,'b':4})})>>>dob.baz.a3# Set new value within nested tree>>>dob.baz.c=5>>>dobDictObject({'foo':1,'bar':2,'baz':DictObject({'a':3,'b':4,'c':5})})Optional: warn if key not found but do not stop flowWarn if a key is not found and default_to is being returned.
Particularly useful if default_to is set toNone>>>dob=DictObject({'foo':1,'bar':2},default_to=None,warn_key_not_found=True)>>>dob.not_a_keyUserWarning:Keynot_a_keynotfoundinDictTuple.ReturningNone# Returns None# Set turn warnings on or off after instantiation>>>dob.warn_key_not_found()# turns warnings on>>>dob.warn_key_not_found(False)# turns warnings offConverting to / from a dictConvert any nested dictionaries to DictObject>>>dob=DictObject({'foo':1,'bar':2,'baz':{'c':3,'d':4},convert_nested=True)>>>dobDictObject({'foo':1,'bar':2,'baz':DictObject{'c':3,'d':4}})# Convert nested objects after initialisation>>>dob=DictObject({{'foo':1,'bar':2,'zonk':{'a':'zonk','b':'zonky''c':{'d':3,'e':4}}})dobDictObject({'foo':1,'bar':2,'zonk':{'a':'zonk','b':'zonky''c':{'d':3,'e':4}}})>>>dob.convert_dicts(recursive=True)>>>dobDictObject({'foo':1,'bar':2,'zonk':DictObject{'a':'zonk','b':'zonky','c':DictObject{'d':3,'e':4}}})# Access nested members>>>dob.zonk.c.d3# Convert back to a dict, including any nested objects>>>dob.todict()# Defaults to recursive{'foo':1,'bar':2,'zonk':{'a':'zonk','b':'zonky''c':{'d':3,'e':4}}}>>>dob.todict(False)# Nested DictObjects not converted to dict{'foo':1,'bar':2,'zonk':DictObject{'a':'zonk','b':'zonky','c':DictObject{'d':3,'e':4}}}All standard python dict functions work>>>dob=DictObject({'foo':1,'bar':2},default_to='Not Set')>>>dob.update({'bar':'Two','baz':3})>>>dobDictObject{'foo':1,'bar':'Two','baz':3}>>>dob['bar']='II'>>>dobDictObject{'foo':1,'bar':'II','baz':3}>>>dob.keys()dict_keys(['foo','bar','baz'])>>>forkeyindob:print(f'{key}={dob[key]}')foo=1bar=IIbaz=3>>>'bazz'indobFalse>>>dob=DictObject({'foo':1,'bar':2})# default_to is not set>>>dob.baz# Raises KeyError>>>dob['baz']# Raises KeyError>>>dob.get('baz',3)3CopyingDeepcopyDeep copies create new copies of any nested objects.>>>fromcopyimportdeepcopy>>>dob=DictObject({'foo':1,'bar':2},warn_key_not_found=True,default_to=None)>>>dob.baz={'a':1,'b':{'c':3,'d':4}}>>>dob.convert_dicts()>>>dobDictObject{'foo':1,'bar':2,'baz':DictObject{'a':1,'b':{'c':3,'d':4}}}>>>dob2=deepcopy(dob)>>>dob.baz.a=3>>>dobDictObject{'foo':1,'bar':2,'baz':DictObject{'a':3,'b':{'c':3,'d':4}}}dob2DictObject{'foo':1,'bar':2,'baz':DictObject{'a':1,'b':{'c':3,'d':4}}}Shallow copyIn the example above,
if dob2 is created with using>>>dob2=dob.copy()setting dob.baz.a = 3 would also change dob2.baz.a to 3