Created
April 22, 2015 15:26
-
-
Save rochacbruno/d4af29249d3d07f08432 to your computer and use it in GitHub Desktop.
Taxonomy Tree AutoVivification
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
from taxonomy_tree import TaxonomyTree | |
taxonomy = TaxonomyTree() | |
taxonomy.linguagens.script.python | |
taxonomy.linguagens.script.perl | |
taxonomy.linguagens.funcionais.elixir | |
taxonomy.linguagens.funcionais.f_sharp | |
taxonomy.databases.nosql.mongo | |
taxonomy.databases.nosql.cassandra | |
taxonomy.databases.sql.mysql | |
taxonomy.databases.sql.postgres | |
taxonomy['databases']['hybrid']['orient'] | |
print taxonomy.as_dict() | |
{ | |
"databases": { | |
"hybrid": { | |
"orient": {} | |
}, | |
"nosql": { | |
"cassandra": {}, | |
"mongo": {} | |
}, | |
"sql": { | |
"mysql": {}, | |
"postgres": {} | |
} | |
}, | |
"linguagens": { | |
"funcionais": { | |
"elixir": {}, | |
"f_sharp": {} | |
}, | |
"script": { | |
"perl": {}, | |
"python": {} | |
} | |
} | |
} |
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
__author__ = 'rochacbruno' | |
import collections | |
import json | |
class TaxonomyDict(collections.defaultdict): | |
""" | |
Mind Blowing Recursive Tree implementing AutoVivification for taxonomy Trees | |
""" | |
def __getattr__(self, attr): | |
return self[attr] | |
def __setattr__(self, attr, val): | |
self[attr] = val | |
def as_json(self, indent=4, sort_keys=True): | |
return json.dumps(self, indent=indent, sort_keys=sort_keys) | |
def __unicode__(self): | |
return self.as_json() | |
__str__ = __unicode__ | |
def as_dict(self): | |
return json.loads(self.as_json()) | |
TaxonomyTree = lambda: TaxonomyDict(TaxonomyTree) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment