access dict elements with dot access dict elements with dot

The solution for “access dict elements with dot access dict elements with dot” can be found here. The following code will assist you in solving the problem.

class dotdict(dict):
“””dot.notation access to dictionary attributes”””
__getattr__ = dict.get
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__class Map(dict):
“””
Example:
m = Map({‘first_name’: ‘Eduardo’}, last_name=’Pool’, age=24, sports=[‘Soccer’])
“””
def __init__(self, *args, **kwargs):
super(Map, self).__init__(*args, **kwargs)
for arg in args:
if isinstance(arg, dict):
for k, v in arg.iteritems():
self[k] = v

if kwargs:
for k, v in kwargs.iteritems():
self[k] = v

def __getattr__(self, attr):
return self.get(attr)

def __setattr__(self, key, value):
self.__setitem__(key, value)

def __setitem__(self, key, value):
super(Map, self).__setitem__(key, value)
self.__dict__.update({key: value})

def __delattr__(self, item):
self.__delitem__(item)

def __delitem__(self, key):
super(Map, self).__delitem__(key)
del self.__dict__[key]

Thank you for using DeclareCode; We hope you were able to resolve the issue.

More questions on [categories-list]

0
inline scripts encapsulated in