Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {'key0': {'attrs': {'entity': 'p', 'hash': '34nj3h43b4n3', 'id': '4130'},
- u'key1': {'attrs': {'entity': 'r',
- 'hash': '34njasd3h43b4n3',
- 'id': '4130-1'},
- u'key2': {'attrs': {'entity': 'c',
- 'hash': '34njasd3h43bdsfsd4n3',
- 'id': '4130-1-1'}}},
- u'key3': {'attrs': {'entity': 'r',
- 'hash': '34njasasasd3h43b4n3',
- 'id': '4130-2'},
- u'key4': {'attrs': {'entity': 'c',
- 'hash': '34njawersd3h43bdsfsd4n3',
- 'id': '4130-2-1'}},
- u'key5': {'attrs': {'entity': 'c',
- 'hash': '34njawersd3h43bdsfsd4n3',
- 'id': '4130-2-2'}}}},
- 'someohterthing': 'someothervalue',
- 'something': 'somevalue'}
- def walkDict( aDict, visitor, path=() ):
- for k in aDict:
- if k == 'attrs':
- visitor( path, aDict[k] )
- elif type(aDict[k]) != dict:
- pass
- else:
- walkDict( aDict[k], visitor, path+(k,) )
- def printMe( path, element ):
- print path, element
- def filterFor( path, element ):
- if element['id'] == '4130-2-2':
- print path, element
- walkDict( myDict, filterFor )
- for path, attrDict in walkDictIter( aDict ):
- # process attrDict...
- class ProperObject( object ):
- """A proper class definition for each "attr" dictionary."""
- def __init__( self, path, attrDict ):
- self.path= path
- self.__dict__.update( attrDict )
- def __str__( self ):
- return "path %r, entity %r, hash %r, id %r" % (
- self.path, self.entity, self.hash, self.id )
- masterDict= {}
- def builder( path, element ):
- masterDict[path]= ProperObject( path, element )
- # Use the Visitor to build ProperObjects for each "attr"
- walkDict( myDict, builder )
- # Now that we have a simple dictionary of Proper Objects, things are simple
- for k,v in masterDict.items():
- if v.id == '4130-2-2':
- print v
- # Create an "index" of your ProperObjects
- import collections
- byId= collections.defaultdict(list)
- for k in masterDict:
- byId[masterDict[k].id].append( masterDict[k] )
- # Look up a particular item in the index
- print map( str, byId['4130-2-2'] )
- def traverse_tree(dictionary, id=None):
- for key, value in dictionary.items():
- if key == 'id':
- if value == id:
- print dictionary
- else:
- traverse_tree(value, id)
- return
- >>> traverse_tree({1: {'id': 2}, 2: {'id': 3}}, id=2)
- {'id': 2}
- d = { u'somekey' : 'somevalue',
- u'someotherkey' : 'someothervalue',
- u'key1' : {
- 'attrs' : {
- 'entity' : 'Post',
- 'hash' : '63e1ca180b86850ce51a6eb13bd2654a',
- 'id' : '4130'
- },
- },
- u'key2' : {
- 'attrs' : {
- 'entity' : 'Rev',
- 'hash' : '34176d03995d23251a8ce2c45e11f4ae',
- 'id' : '4130-1'
- },
- },
- u'key3' : {
- 'attrs' : {
- 'entity' : 'Comment',
- 'hash' : '34176d03995d23251a8ce2c45e11f4er',
- 'id' : '4130-1-1'
- },
- },
- u'key4' : {
- 'attrs' : {
- 'entity' : 'Rev',
- 'hash' : '34176d03995d23251a8ce2c45e11f4qe',
- 'id' : '4130-2'
- },
- },
- u'key5' : {
- 'attrs' : {
- 'entity' : 'Comment',
- 'hash' : '34176d03995d23251a8ce2c45e11f4er',
- 'id' : '4130-2-1'
- },
- },
- u'key6' : {
- 'attrs' : {
- 'entity' : 'Comment',
- 'hash' : '34176d03995d23251a8ce2c4er1f4er',
- 'id' : '4130-2-2'
- },
- },
- }
Add Comment
Please, Sign In to add comment