469,128 Members | 1,384 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,128 developers. It's quick & easy.

object data member dumper?

does anyone know of a utility to do a recursive dump of object data members?
--
Posted via a free Usenet account from http://www.teranews.com

Nov 7 '06 #1
9 2487
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 ?
Nov 7 '06 #2
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 ?
Basically it sounds like the OP wants to see the attribute values for an
object (and those objects' attribute values, ...). Presumably the
recursive descent could be stopped at the built-in types.

I'm not familiar with any such thing. The code of the standard library's
pprint module might be a good place to start (and the suggested
functionality might make a nice addition if we could work out exactly
what the requirement was).

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Nov 7 '06 #3
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

--
Posted via a free Usenet account from http://www.teranews.com

Nov 7 '06 #4
Steve Holden 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 ?

Basically it sounds like the OP wants to see the attribute values for an
object (and those objects' attribute values, ...). Presumably the
recursive descent could be stopped at the built-in types.
Yes, exactly. But what is 'OP'?
>
I'm not familiar with any such thing.
I'm amazed that there is no way to easily look at the fundamental data
structures of a python program. I'm new to python - is there something I
don't know about the territory in this regard?
>The code of the standard library's
pprint module might be a good place to start
I played around with this but it seems more targetted to looking at python
objects as opposed to user-defined.
>(and the suggested
functionality might make a nice addition if we could work out exactly
what the requirement was).
I would be glad if people would comment on my example as a step in this
direction.

tom arnall
north spit, ca
usa
--
Posted via a free Usenet account from http://www.teranews.com

Nov 7 '06 #5
tom arnall wrote:
Steve Holden 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 ?

Basically it sounds like the OP wants to see the attribute values for an
object (and those objects' attribute values, ...). Presumably the
recursive descent could be stopped at the built-in types.


Yes, exactly. But what is 'OP'?
"original poster" (you)
>
>>I'm not familiar with any such thing.


I'm amazed that there is no way to easily look at the fundamental data
structures of a python program. I'm new to python - is there something I
don't know about the territory in this regard?
Well, new enough to be unaware that Python normally doesn't need such
drastic measures for debugging. When you are building complex data
structures such a form of output could get big really quickly. I've
never come across anything like it in any other language that I have used.

The most that's usually required is a __repr__() method that dumps the
values of necessary data attributes.
>
>>The code of the standard library's
pprint module might be a good place to start


I played around with this but it seems more targetted to looking at python
objects as opposed to user-defined.
It is, but it could be used as a basis for something more to your liking.
>
>>(and the suggested
functionality might make a nice addition if we could work out exactly
what the requirement was).


I would be glad if people would comment on my example as a step in this
direction.
I'd suggest you instead consider a mixin object that will add recursive
data dump functionality to any such classes you require.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Nov 8 '06 #6
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

Nov 8 '06 #7
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:
Make it :
class A(object):
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
Ok, so this is for debugging purpose ?

A small question : how should this behave for:
- _implementation attributes
- __magic__ attributes
- class attributes
- callable attributes
- properties and other descriptors ?

In fact, the problem is that in Python, everything's an object, and an
object is mostly a composed namespace (ie a set of name-object mappings)
subject to some lookup rules. FWIW, the class of an object is an
attribute (__class__) referencing the class objet; superclasses are
class objects referenced by the __bases__ and __mro__ attributes of the
class object; method objects are usually attributes of the class object,
but it's possible to add/replace methods on a per-instance basis;
properties are class attributes that most of the time works on some
instance attribute. etc, etc, etc... Even the code of a function is an
attribute of the function object. And as an icing on top of the cake,
there's the __getattr__ magic method...

With all this in mind, writing a generic python object inspector is not
that trivial : either you'll end up with way too much informations or
you'll arbitrary skip informations that would be useful in a given
context or you'll need to pass so much args to the inspector that it'll
become too heavy for a quick, intuitive use... Well, that's at least my
own experience.

OTOH, "live" inspection of an object (either in the Python shell or in a
pdb session) works fine. The pprint and inspect modules may help too.

My 2 cents...
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Nov 8 '06 #8
Steve Holden wrote:
tom arnall wrote:
>Steve Holden 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 ?

Basically it sounds like the OP wants to see the attribute values for an
object (and those objects' attribute values, ...). Presumably the
recursive descent could be stopped at the built-in types.


Yes, exactly. But what is 'OP'?
"original poster" (you)
>>
>>>I'm not familiar with any such thing.


I'm amazed that there is no way to easily look at the fundamental data
structures of a python program. I'm new to python - is there something I
don't know about the territory in this regard?
Well, new enough to be unaware that Python normally doesn't need such
drastic measures for debugging. When you are building complex data
structures such a form of output could get big really quickly. I've
never come across anything like it in any other language that I have used.
never come across the complexity of data structures or across an object
displayer?
>
The most that's usually required is a __repr__() method that dumps the
values of necessary data attributes.
what about __dict__?

>>
>>>The code of the standard library's
pprint module might be a good place to start


I played around with this but it seems more targetted to looking at
python objects as opposed to user-defined.
It is, but it could be used as a basis for something more to your liking.
>>
>>>(and the suggested
functionality might make a nice addition if we could work out exactly
what the requirement was).


I would be glad if people would comment on my example as a step in this
direction.
I'd suggest you instead consider a mixin object that will add recursive
data dump functionality to any such classes you require.
have googled a fair amount on 'mixin' but still stumped on it.


--
Posted via a free Usenet account from http://www.teranews.com

Nov 10 '06 #9
At Thursday 9/11/2006 22:16, tom arnall wrote:
I'd suggest you instead consider a mixin object that will add recursive
data dump functionality to any such classes you require.

have googled a fair amount on 'mixin' but still stumped on it.
Try again using "mixin class"
--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
Nov 10 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Edward wijaya | last post: by
reply views Thread by kamal | last post: by
reply views Thread by Eric | last post: by
14 posts views Thread by horos | last post: by
1 post views Thread by Miguel Manso | last post: by
reply views Thread by tom arnall | last post: by
crazy4perl
1 post views Thread by crazy4perl | last post: by
1 post views Thread by srinivasan srinivas | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.