Skip to content

Instantly share code, notes, and snippets.

@gtalarico
Created July 18, 2016 06:34
Show Gist options
  • Save gtalarico/50b6eea650d359fb99aac813853e44d5 to your computer and use it in GitHub Desktop.
Save gtalarico/50b6eea650d359fb99aac813853e44d5 to your computer and use it in GitHub Desktop.
json like python dict
def tree():
return defaultdict(tree)
Adding a simple subclass allows attribute like selection:
class mydefaultdict(defaultdict):
def __getattr__(self, attr):
return self[attr]
def __setattr__(self, attr, val):
self[attr] = val
def tree(): return mydefaultdict(tree)
will turn:
taxonomy['Animalia']['Chordata']['Mammalia']['Carnivora']['Felidae']['Felis']['cat']
into:
taxonomy.Animalia.Chordata.Mammalia.Carnivora.Felidae.Felis.cat
@gtalarico
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment