Created
December 12, 2012 18:01
-
-
Save xfenix/4270083 to your computer and use it in GitHub Desktop.
Create object from dict
This file contains hidden or 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
class DictToObject: | |
def __init__(self, data): | |
for name, value in data.iteritems(): | |
setattr(self, name, self._wrap(value)) | |
def _wrap(self, value): | |
if isinstance(value, (tuple, list, set, frozenset)): | |
return type(value)([self._wrap(v) for v in value]) | |
else: | |
return Struct(value) if isinstance(value, dict) else value |
This file contains hidden or 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
simple_object = DictToObject({'title': 'Roger says: "hiii!"', 'content': 'American Dad'}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment