473,387 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 2720
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Edward wijaya | last post by:
Hi, Is there any equivalent of it in Python? Thanks so much for your time. Regards, Edward WIJAYA
0
by: kamal | last post by:
I am trying to dump a hash using Data::Dumper. I need to quote the keys while dumping. My progarm looks like this use Data::Dumper; $Data::Dumper::Quotekeys = 1; $Data::Dumper::Useqq = 1; ...
0
by: Eric | last post by:
I've got a weird problem, regardless of how often I enter: perl -MCPAN -e 'install "Data::Dumper"' I never get a message telling me that it is up-to-date. It will always try to reinstall even...
14
by: horos | last post by:
hey all, I'm a heavy perl user, not so much a java script user, and was wondering... perl has an extremely nice utility called Data::Dumper, which allows you to dump out the contents of an...
1
by: Miguel Manso | last post by:
Hi there, I'm a Perl programmer trying to get into Python. I've been reading some documentation and I've choosed Python has being the "next step" to give. Can you point me out to Python...
0
by: tom arnall | last post by:
> George, did you ever put your object dumper on the internet? tom arnall north spit, ca
1
by: rhaas | last post by:
Howdy. I've been trying to parse the return of XML::TreePP from an xBRL file using perl and Data::Dumper. The resulting Data::Dumper output looks like this: $VAR1 = { 'xbrl' =>...
1
crazy4perl
by: crazy4perl | last post by:
Hi, Is it possible to use data dumper to dump data into a file????
0
by: reco | last post by:
Hi, I am only new to development and have been given a task to convert a Perl script to a Windows Service. The issue I am facing is that the current Perl Script uses a SOAP call to an Apache Web...
1
by: srinivasan srinivas | last post by:
Thanks, Srini Bollywood, fun, friendship, sports and more. You name it, we have it on http://in.promos.yahoo.com/groups/bestofyahoo/
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.