开发者

Is there a python module that provides deeper analysis of a data structure than pprint?

Is there anything in python that lets me dump out a random object in such a way as to see its underlying data representation?

I am coming from Perl where Data::Dumper does a reasonable job of letti开发者_如何转开发ng me see how a data structure is laid out. Is there anything that does the same thing in python?

Thanks!


Well Dumper in Perl gives you a representation of an object that can be eval'd by the interpreter to give you the original object. An object's repr in Python tries to do that, and sometimes it's possible. A dict's repr or a str's repr do this, and some classes like datetime and timedelta also do this. So repr is as much the equivalent of Dumper but it's not pretty and doesn't show you the internals of an object. For that you can use dir and roll your own printer.

Here's my shot at a printer that would not result in eval-able Python code and thus should be used to generate a string of the object instead:

def dump(obj):
  out = {}

  for attr in dir(obj):
    out[attr] = getattr(obj, attr)

  from pprint import pformat
  return pformat(out)

class myclass(object):
  foo = 'foo'

  def __init__(self):
    self.bar = 'bar'

  def __str__(self):
    return dump(self)

c = myclass()
print c

In the above example, I've overridden the object's default __str__ implementation. __str__ is what gets called when you try to represent the object as a string, or format it using a string formatting function.

BTW repr is what gets printed when you do print obj, it invokes the __repr__ method on that object. See the Python documentation of __repr__ for more information on how to control the formatting of objects.

# this would print the object's __repr__
print "%r" % c

# this would print the object's __str__
print "%s" % c

The output from the above code was

{'__class__': <class '__main__.myclass'>,
 '__delattr__': <method-wrapper '__delattr__' of myclass object at 0xb76deb0c>,
 '__dict__': {'bar': 'bar'},
 '__doc__': None,
 '__format__': <built-in method __format__ of myclass object at 0xb76deb0c>,
 '__getattribute__': <method-wrapper '__getattribute__' of myclass object at 0xb76deb0c>,
 '__hash__': <method-wrapper '__hash__' of myclass object at 0xb76deb0c>,
 '__init__': <bound method myclass.__init__ of <__main__.myclass object at 0xb76deb0c>>,
 '__module__': '__main__',
 '__new__': <built-in method __new__ of type object at 0x82358a0>,
 '__reduce__': <built-in method __reduce__ of myclass object at 0xb76deb0c>,
 '__reduce_ex__': <built-in method __reduce_ex__ of myclass object at 0xb76deb0c>,
 '__repr__': <method-wrapper '__repr__' of myclass object at 0xb76deb0c>,
 '__setattr__': <method-wrapper '__setattr__' of myclass object at 0xb76deb0c>,
 '__sizeof__': <built-in method __sizeof__ of myclass object at 0xb76deb0c>,
 '__str__': <bound method myclass.__str__ of <__main__.myclass object at 0xb76deb0c>>,
 '__subclasshook__': <built-in method __subclasshook__ of type object at 0x896ad34>,
 '__weakref__': None,
 'bar': 'bar',
 'foo': 'foo'}


After much searching about for this exactly myself, I came across this Dumper equivalent which I typically import now. https://salmon-protocol.googlecode.com/svn-history/r24/trunk/salmon-playground/dumper.py

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜