tom arnall wrote: > >object data member dumper? > >George Sakkis george.sakkis at gmail.com > >Wed Nov 8 03:42:47 CET 2006 > > > tom arnall wrote: > > > > > Bruno Desthuilliers wrote: > > > > > > > tom arnall a écrit : > > > >> does anyone know of a utility to do a recursive dump of object data > > > >> members? > > > >> > > > > > > > > What are "object data members" ? (hint: in Python, everything is an > > > > object - even functions and methods). > > > > > > > > What is your real use case ? > > > > > > something like: > > > > > > class A: > > > def __init__(self, p1): > > > self.p1 = p1 > > > > > > class B: > > > def __init__(self,p1, p2): > > > self.a = A(p1) > > > self.p2 = p2 > > > self.v1 = '3' > > > > > > class C: > > > def __init__(self): > > > self.b = B(3,4) > > > self.p3 = 5 > > > > > > class D: > > > def __init__(self): > > > self.v2=2 > > > self.o1 = C() > > > self.o2 = B(11,12) > > > > > > > > > d = D() > > > objectDataDumper(d) > > > > > > > > > would produce something like: > > > > > > object of class D with: > > > o1(C)->b(B)->a(A)->p1=3 > > > o1(C)->b(B)->p2=4 > > > o1(C)->b(B)->v1=3 > > > o1(C)->p3=5 > > > o2(B)->a(A)->p1=11 > > > o2(B)->p2=12 > > > o2(B)->v1=3 > > > v2=2 > > > > > > > > > tom arnall > > > north spit, ca > > > usa > > > > At first I thought pickle would be what you're looking for, because > > that's exactly what it does; it dumps arbitrary objects, without > > choking on recursive references. Only problem is, it's not human > > readable (even in its ascii form).If you want it to be human readable, > > you may check the gnosis.xml.pickle module > > (http://cheeseshop.python.org/pypi/Gnosis_Utils/1.2.1-a). That's the > > output of gnosis.xml.pickle.XML_Pickler(d).dumps() on your example: > > > > <?xml version="1.0"?> > > <!DOCTYPE PyObject SYSTEM "PyObjects.dtd"> > > <PyObject module="__main__" class="D" id="-1210610740"> > > <attr name="v2" type="numeric" value="2" /> > > <attr name="o2" type="PyObject" id="-1211043316" module="__main__" > > class="B"> > > <attr name="a" type="PyObject" id="-1211043220" module="__main__" > > class="A"> > > <attr name="p1" type="numeric" value="11" /> > > </attr> > > <attr name="p2" type="numeric" value="12" /> > > <attr name="v1" type="string" value="3" /> > > </attr> > > <attr name="o1" type="PyObject" id="-1211199412" module="__main__" > > class="C"> > > <attr name="p3" type="numeric" value="5" /> > > <attr name="b" type="PyObject" id="-1211082644" module="__main__" > > class="B"> > > <attr name="a" type="PyObject" id="-1211067156" module="__main__" > > class="A"> > > <attr name="p1" type="numeric" value="3" /> > > </attr> > > <attr name="p2" type="numeric" value="4" /> > > <attr name="v1" type="string" value="3" /> > > </attr> > > </attr> > > </PyObject> > > > > > > I've also written a similar but less verbose xml dumper. The gnosis.xml > > package provides a bidirectional mapping between objects and xml > > (objects -> xml and xml->object) and therefore has to be precise; mine > > simply generates a nice xml dump of the object which isn't necessarily > > reversible. Here's the output for your example: > > > > <root type="__main__.D"> > > <o1 type="__main__.C"> > > <b type="__main__.B"> > > <a type="__main__.A"> > > <p1 type="int">3</p1> > > </a> > > <p2 type="int">4</p2> > > <v1 type="str">3</v1> > > </b> > > <p3 type="int">5</p3> > > </o1> > > <o2 type="__main__.B"> > > <a type="__main__.A"> > > <p1 type="int">11</p1> > > </a> > > <p2 type="int">12</p2> > > <v1 type="str">3</v1> > > </o2> > > <v2 type="int">2</v2> > > </root> > > > > > > If you find it suits you better, I'll try to make it available > > somewhere (probably in the cookbook). > > > > George > > > > > George, > > did you ever put up your object dumper on the net?
Here it is: http://rafb.net/paste/results/C0NHBp27.html Mostly undocumented but works for most common types. Most methods are overridable so you can customize the dumping for a specific type (or types) in a subclass. George -- http://mail.python.org/mailman/listinfo/python-list