2012-09-26 12 views
5

Ho una struttura di dati Python complessa (se è importante, è un grande oggetto Music21 Score) che non si mette in crisi a causa della presenza di un elemento debole da qualche parte nel profondo della struttura dell'oggetto. Ho già eseguito il debug di questi problemi con lo stack trace e il debugger Python, ma è sempre un grosso problema. Esiste uno strumento che esegue dir() ricorsivamente su tutti gli attributi di un oggetto, trovando oggetti nascosti in liste, tuple, dicts, ecc. E restituisce quelli che corrispondono a un certo valore (una funzione lambda o qualcosa di simile). Un grosso problema sono i riferimenti ricorsivi, quindi è necessaria una sorta di funzione memo (come usa copy.deepcopy). Ho provato:Recursivamente dir() un oggetto python per trovare valori di un certo tipo o con un certo valore

import weakref 
def findWeakRef(streamObj, memo=None): 
    weakRefList = [] 
    if memo is None: 
     memo = {} 
    for x in dir(streamObj): 
     xValue = getattr(streamObj, x) 
     if id(xValue) in memo: 
      continue 
     else: 
      memo[id(xValue)] = True 
     if type(xValue) is weakref.ref: 
      weakRefList.append(x, xValue, streamObj) 
     if hasattr(xValue, "__iter__"): 
      for i in xValue: 
       if id(i) in memo: 
        pass 
       else: 
        memo[id(i)] = True 
        weakRefList.extend(findWeakRef(i), memo) 
     else: 
      weakRefList.extend(findWeakRef(xValue), memo) 
    return weakRefList 

posso probabilmente continuerà collegare buchi in questo (il iter non è quello che avrei voluto per dicts, per esempio), ma prima di gettare più tempo in esso, chiedendosi se qualcuno conosce una risposta più facile. Potrebbe essere uno strumento generale piuttosto utile.

+1

Non ho ancora visto una soluzione già pronta. Forse gc.get_referents invece di dir ti fa un po 'più lontano. – Pankrat

+0

Riduce il cruft (__eq__, ecc.) Anche se a un prezzo che il formato di ciò che viene restituito cambia in base al tipo di oggetto, quindi probabilmente sarebbe una soluzione più rapida, ma non più semplice. Anche a quanto pare non supporta la ricorsione. Grazie! –

+1

Hai considerato la sottoclasse della classe 'pickle.Pickler'? La fonte è inclusa in '.../Lib/pickle.py'. Ciò dovrebbe consentire di riutilizzare un sacco di codice e intrappolare 'PickleError's per fare ciò che descrivi - così come il piggyback del protocollo di pickling ben stabilito che Python ha già installato. – martineau

risposta

2

Questo sembra essere l'inizio di una risposta. Ho dovuto eseguire il backport di alcuni elementi da Python 3.2 inspect.getattr_static per farlo funzionare in modo che non chiamasse le proprietà che continuavano a generare nuovi oggetti. Ecco il codice mi si avvicinò con:

#------------------------------------------------------------------------------- 
# Name:   treeYield.py 
# Purpose:  traverse a complex datastructure and yield elements 
#    that fit a given criteria 
# 
# Authors:  Michael Scott Cuthbert 
# 
# Copyright: Copyright © 2012 Michael Scott Cuthbert 
# License:  CC-BY 
#------------------------------------------------------------------------------- 
import types 

class TreeYielder(object): 
    def __init__(self, yieldValue = None): 
     ''' 
     `yieldValue` should be a lambda function that 
     returns True/False or a function/method call that 
     will be passed the value of a current attribute 
     '''   
     self.currentStack = [] 
     self.yieldValue = yieldValue 
     self.stackVals = [] 
     t = types 
     self.nonIterables = [t.IntType, t.StringType, t.UnicodeType, t.LongType, 
          t.FloatType, t.NoneType, t.BooleanType] 

    def run(self, obj, memo = None): 
     ''' 
     traverse all attributes of an object looking 
     for subObjects that meet a certain criteria. 
     yield them. 

     `memo` is a dictionary to keep track of objects 
     that have already been seen 

     The original object is added to the memo and 
     also checked for yieldValue 
     ''' 
     if memo is None: 
      memo = {} 
     self.memo = memo 
     if id(obj) in self.memo: 
      self.memo[id(obj)] += 1 
      return 
     else: 
      self.memo[id(obj)] = 1 

     if self.yieldValue(obj) is True: 
      yield obj 


     ### now check for sub values... 
     self.currentStack.append(obj) 

     tObj = type(obj) 
     if tObj in self.nonIterables: 
      pass 
     elif tObj == types.DictType: 
      for keyX in obj: 
       dictTuple = ('dict', keyX) 
       self.stackVals.append(dictTuple) 
       x = obj[keyX] 
       for z in self.run(x, memo=memo): 
        yield z 
       self.stackVals.pop() 

     elif tObj in [types.ListType, types.TupleType]: 
      for i,x in enumerate(obj): 
       listTuple = ('listLike', i) 
       self.stackVals.append(listTuple) 
       for z in self.run(x, memo=memo): 
        yield z 
       self.stackVals.pop() 

     else: # objects or uncaught types... 
      ### from http://bugs.python.org/file18699/static.py 
      try: 
       instance_dict = object.__getattribute__(obj, "__dict__") 
      except AttributeError: 
       ## probably uncaught static object 
       return 

      for x in instance_dict: 
       try: 
        gotValue = object.__getattribute__(obj, x) 
       except: # ?? property that relies on something else being set. 
        continue 
       objTuple = ('getattr', x) 
       self.stackVals.append(objTuple) 
       try: 
        for z in self.run(gotValue, memo=memo): 
         yield z 
       except RuntimeError: 
        raise Exception("Maximum recursion on:\n%s" % self.currentLevel()) 
       self.stackVals.pop()     

     self.currentStack.pop() 

    def currentLevel(self): 
     currentStr = "" 
     for stackType, stackValue in self.stackVals: 
      if stackType == 'dict': 
       if isinstance(stackValue, str): 
        currentStr += "['" + stackValue + "']" 
       elif isinstance(stackValue, unicode): 
        currentStr += "[u'" + stackValue + "']" 
       else: # numeric key... 
        currentStr += "[" + str(stackValue) + "]" 
      elif stackType == 'listLike': 
       currentStr += "[" + str(stackValue) + "]" 
      elif stackType == 'getattr': 
       currentStr += ".__getattribute__('" + stackValue + "')" 
      else: 
       raise Exception("Cannot get attribute of type %s" % stackType) 
     return currentStr 

Questo codice consente di eseguire qualcosa di simile:

class Mock(object): 
    def __init__(self, mockThing, embedMock = True): 
     self.abby = 30 
     self.mocker = mockThing 
     self.mockList = [mockThing, mockThing, 40] 
     self.embeddedMock = None 
     if embedMock is True: 
      self.embeddedMock = Mock(mockThing, embedMock = False) 

mockType = lambda x: x.__class__.__name__ == 'Mock' 

subList = [100, 60, -2] 
myList = [5, 20, [5, 12, 17], 30, {'hello': 10, 'goodbye': 22, 'mock': Mock(subList)}, -20, Mock(subList)] 
myList.append(myList) 

ty = TreeYielder(mockType) 
for val in ty.run(myList): 
    print(val, ty.currentLevel()) 

E ottenere:

(<__main__.Mock object at 0x01DEBD10>, "[4]['mock']") 
(<__main__.Mock object at 0x01DEF370>, "[4]['mock'].__getattribute__('embeddedMock')") 
(<__main__.Mock object at 0x01DEF390>, '[6]') 
(<__main__.Mock object at 0x01DEF3B0>, "[6].__getattribute__('embeddedMock')") 

o eseguire:

high = lambda x: isinstance(x, (int, float)) and x > 10 
ty = TreeYielder(high) 
for val in ty.run(myList): 
    print(val, ty.currentLevel()) 

e ottieni:

(20, '[1]') 
(12, '[2][1]') 
(17, '[2][2]') 
(30, '[3]') 
(22, "[4]['goodbye']") 
(100, "[4]['mock'].__getattribute__('embeddedMock').__getattribute__('mocker')[0]") 
(60, "[4]['mock'].__getattribute__('embeddedMock').__getattribute__('mocker')[1]") 
(40, "[4]['mock'].__getattribute__('embeddedMock').__getattribute__('mockList')[2]") 

Sto ancora cercando di capire perché .abby non viene trovato, ma immagino valga la pena postare anche a questo punto, dal momento che è molto più sulla buona strada di quando ero partito.

Problemi correlati