title: Recursively dir() a python object to find values of a certain type or with a certain value
body: <p>I have a complex Python data structure (if it matters, it's a large music21 Score object) which will not pickle due to the presence of a weakref somewhere deep inside the object structure.  I've debugged such problems with the stack trace and python debugger before, but it's always a big pain.  Is there a tool which runs dir() recursively on all attributes of an object, finding objects hidden in lists, tuples, dicts, etc., and returns those that match a certain value (a lambda function or something like that).  A big problem is recursive references, so some sort of memo function (like copy.deepcopy uses) is needed. I tried:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I can probably continue plugging holes in this (the <strong>iter</strong> isn't what I'd want for dicts, for instance), but before I throw more time into it, wondering if someone knows an easier answer.  It could be a pretty useful general tool.</p>