Skip to content

Instantly share code, notes, and snippets.

@abits
Forked from lachezar/dict_to_object.py
Created February 22, 2016 16:05
Show Gist options
  • Save abits/6302e993a007958ef487 to your computer and use it in GitHub Desktop.
Save abits/6302e993a007958ef487 to your computer and use it in GitHub Desktop.
Python dict to object
config_dict = {
'group1': {
'server1': {
'apps': ('nginx', 'mysql'),
'cpus': 4
},
'maintenance': True
},
'firewall_version': '1.2.3',
'python2.7': True
}
class DictToObject(object):
def __init__(self, dictionary):
def _traverse(key, element):
if isinstance(element, dict):
return key, DictToObject(element)
else:
return key, element
object_dict = dict(_traverse(k, v) for k, v in dictionary.iteritems())
self.__dict__.update(object_dict)
if __name__ == '__main__':
config = DictToObject(config_dict)
assert config.group1.server1.apps == ('nginx', 'mysql')
assert config.group1.maintenance
assert config.firewall_version == '1.2.3'
assert config.__dict__['python2.7'] # or
assert config.__getattribute__('python2.7')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment