Skip to content

Instantly share code, notes, and snippets.

@ksysctl
Created May 25, 2012 00:30
Show Gist options
  • Save ksysctl/2785073 to your computer and use it in GitHub Desktop.
Save ksysctl/2785073 to your computer and use it in GitHub Desktop.
Convert <dict, list, tuple> to object
# -*- 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