Last active
January 26, 2021 11:06
-
-
Save ultrafunkamsterdam/dcd27e502a40d60e5e20e2c82401d073 to your computer and use it in GitHub Desktop.
rdict
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
# 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