Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
deepdict: quickly create highly nested dictionary structures without lots of initialization steps.
'''deepdict: quickly create highly nested dictionary structures without lots
of initialization steps.'''
from collections import defaultdict
class deepdict(defaultdict):
def __init__(self, *a, **kw):
defaultdict.__init__(self, deepdict, *a, **kw)
def __add__(self, num):
return num
def __str__(self):
return str(self.shallow)
@property
def shallow(self):
out = {}
for k, v in self.items():
if isinstance(v, deepdict):
v = v.shallow
out[k] = v
return out
def main():
foo = deepdict()
foo['bar']['baz']['qux'] += 5
print foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.