Skip to content

Instantly share code, notes, and snippets.

@ultrafunkamsterdam
Last active January 26, 2021 11:06
Show Gist options
  • Save ultrafunkamsterdam/dcd27e502a40d60e5e20e2c82401d073 to your computer and use it in GitHub Desktop.
Save ultrafunkamsterdam/dcd27e502a40d60e5e20e2c82401d073 to your computer and use it in GitHub Desktop.
rdict
# accessible by dot ( instance.item ) notation or instance['item'] notation
# recursive dict object
# json serializable
# "pretty print" as stack
# i wouln't really recommend using this in production
class rdict(dict):
def __init__(self, *a, **k):
super(rdict, self).__init__(*a, **k)
self.__dict__ = self
for k in self.__dict__:
if isinstance(self.__dict__[k], dict):
self.__dict__[k] = rdict(self.__dict__[k])
elif isinstance(self.__dict__[k], list):
for i in range(len(self.__dict__[k])):
if isinstance(self.__dict__[k][i], dict):
self.__dict__[k][i] = rdict(self)
def __repr__(self):
return '<%s>' % '\n '.join('%s : %s' % (
k, '\t'+repr(v) if isinstance(v,dict) else repr(v)) for (k, v) in self.__dict__.items())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment