Created
January 19, 2014 14:03
-
-
Save lachezar/8505413 to your computer and use it in GitHub Desktop.
Python dict to object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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