Created
May 25, 2012 00:30
-
-
Save ksysctl/2785073 to your computer and use it in GitHub Desktop.
Convert <dict, list, tuple> 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
# -*- coding: utf-8 -*- | |
class CObject(object): | |
def __init__(self, map): | |
if isinstance(map, (list, tuple)): | |
map = dict(enumerate(map)) | |
if isinstance(map, dict): | |
for key, val in map.iteritems(): | |
key = str(key) | |
if isinstance(val, dict): | |
setattr(self, key, CObject(val)) | |
else: | |
setattr(self, key, val) | |
def __getitem__(self, val): | |
return self.__dict__[val] | |
# Sample: | |
# | |
# map = { | |
# 'attr_1': 'string', | |
# 'attr_2': [1, 2, 3], | |
# } | |
# | |
# o = CObject(map) | |
# print o.attr_1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment