469,317 Members | 2,039 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Data::Dumper for Python

Hi,

Is there any equivalent of it
in Python?

Thanks so much for your time.

Regards,
Edward WIJAYA
Jul 18 '05 #1
7 6793
In article <op**************@news.singnet.com.sg>, Edward wijaya wrote:
Is there any equivalent of it
in Python?


Take a look at the "pprint" module. Also, it's worth noting that the Python
interpreter prints representations of data structures all the time, no
library required:
x = [{'a': a, 'b': b} for a in range(2) for b in range(3)]
x

[{'a': 0, 'b': 0}, {'a': 0, 'b': 1}, {'a': 0, 'b': 2}, {'a': 1, 'b': 0},
{'a': 1, 'b': 1}, {'a': 1, 'b': 2}]

Read up on the __repr__ and __str__ methods to understand how this mechanism
works and how to extend it to your own objects.

--
.:[ dave benjamin: ramen/[sp00] -:- spoomusic.com -:- ramenfest.com ]:.
"talking about music is like dancing about architecture."
Jul 18 '05 #2
On Thu, 28 Oct 2004 18:06:12 +0000, Dave Benjamin wrote:
Take a look at the "pprint" module. Also, it's worth noting that the Python
interpreter prints representations of data structures all the time, no
library required:


However, Python tries ***much*** less hard to make those representations
actually evaluate back to equivalent objects. Based on my experiences but
with no particular knowledge of the history of the two languages in this
regard, this is because Python makes many manipulations easy that are hard
to borderline impossible in Perl, and it is much harder to create such
representations in general.

As a result, repr of any but the most base classes is often used more
for "debugging style" info, and str for a simple identification. It is not
safe in general to eval(repr(obj)) and expect anything but a Syntax Error.

Python shuffles that task off to the Pickle module, which is what you'd
want to look up in the docs. That splits human representation off from
computer-reproducable representation, and I believe overall this is
superior; the two are not the same. In addition, we then get the Pickle
protocol extensions which are frequently quite handy, especially when
dealing with weakrefs.
Jul 18 '05 #3
In article <pa****************************@jerf.org>, Jeremy Bowers wrote:
On Thu, 28 Oct 2004 18:06:12 +0000, Dave Benjamin wrote:
Take a look at the "pprint" module. Also, it's worth noting that the Python
interpreter prints representations of data structures all the time, no
library required:


However, Python tries ***much*** less hard to make those representations
actually evaluate back to equivalent objects. Based on my experiences but
with no particular knowledge of the history of the two languages in this
regard, this is because Python makes many manipulations easy that are hard
to borderline impossible in Perl, and it is much harder to create such
representations in general.

As a result, repr of any but the most base classes is often used more
for "debugging style" info, and str for a simple identification. It is not
safe in general to eval(repr(obj)) and expect anything but a Syntax Error.

Python shuffles that task off to the Pickle module, which is what you'd
want to look up in the docs. That splits human representation off from
computer-reproducable representation, and I believe overall this is
superior; the two are not the same. In addition, we then get the Pickle
protocol extensions which are frequently quite handy, especially when
dealing with weakrefs.


Very good points. My experience with Data::Dumper in Perl has only been with
debugging/pretty-printing; I've never used it as a serialization technique.
If you are the author of all of the classes in your data representation, you
could (in theory) design it such that eval(repr(obj)) always evaluates to
obj, but Pickle is more likely what you want anyway.

--
.:[ dave benjamin: ramen/[sp00] -:- spoomusic.com -:- ramenfest.com ]:.
"talking about music is like dancing about architecture."
Jul 18 '05 #4
Thanks for the informative reply, Dave.
I really appreciate that, for me as a new guy
in Python.

Regards,
Edward WIJAYA
On Thu, 28 Oct 2004 18:06:12 -0000, Dave Benjamin
<ra***@lackingtalent.com> wrote:
In article <op**************@news.singnet.com.sg>, Edward wijaya wrote:
Is there any equivalent of it
in Python?


Take a look at the "pprint" module. Also, it's worth noting that the
Python
interpreter prints representations of data structures all the time, no
library required:
x = [{'a': a, 'b': b} for a in range(2) for b in range(3)]
x

[{'a': 0, 'b': 0}, {'a': 0, 'b': 1}, {'a': 0, 'b': 2}, {'a': 1, 'b': 0},
{'a': 1, 'b': 1}, {'a': 1, 'b': 2}]

Read up on the __repr__ and __str__ methods to understand how this
mechanism
works and how to extend it to your own objects.

Jul 18 '05 #5
Dave Benjamin <ra***@lackingtalent.com> writes:
In article <op**************@news.singnet.com.sg>, Edward wijaya wrote:
Is there any equivalent of it
in Python?


Take a look at the "pprint" module. Also, it's worth noting that the Python
interpreter prints representations of data structures all the time, no
library required:
x = [{'a': a, 'b': b} for a in range(2) for b in range(3)]
x

[{'a': 0, 'b': 0}, {'a': 0, 'b': 1}, {'a': 0, 'b': 2}, {'a': 1, 'b': 0},
{'a': 1, 'b': 1}, {'a': 1, 'b': 2}]

Read up on the __repr__ and __str__ methods to understand how this mechanism
works and how to extend it to your own objects.


Data::Dumper will automatically show instance variables of your
objects and recurse down into their representation. pprint does not
do that. Because of this I find pprint quite useless (at least as a
replacement for Data::Dumper).
Jul 18 '05 #6

Gisle Aas <gi***@activestate.com> wrote:
Data::Dumper will automatically show instance variables of your
objects and recurse down into their representation. pprint does not
do that. Because of this I find pprint quite useless (at least as a
replacement for Data::Dumper).


Python's pprint is a fairly simple little module. A reasonably
experienced Python programmer could probably toss off a clone of it in a
weekend if given a description of it.

With that said, pprint only 'beautifies' lists, tuples and dictionaries.
If one wants something that does more, the source for pprint is readily
available in any Python distribution, to be customized to print
arbitrary class attributes as you (or someone else) finds necessary.

I would imagine that the reason why such an addition was not already
made is because textual representations of objects are not necessarily
round-tripable via eval(repr(obj)) (which has been mentioned before),
and a pprinted representation of an arbitrary user-class (as would be
presented by an altered pprint) is almost certainly not round-tripable
via eval(pprinted_representation(obj)).

- Josiah

Jul 18 '05 #7
On Fri, 29 Oct 2004 00:34:31 -0700,
Josiah Carlson <jc******@uci.edu> wrote:
Python's pprint is a fairly simple little module. A reasonably
experienced Python programmer could probably toss off a clone of it in a
weekend if given a description of it.


Such as, for example, dulcinea.dumper (part of Dulcinea,
http://www.mems-exchange.org/software/dulcinea/):
from dulcinea import dumper as d
from ASTi.model import Model
m=Model() # Create an object
d.dump(m) <Model at 402e180c: <Model object at '/home/amk/.mbv/loader-test'>>
_hc: <HydraController at 402ee86c>
_instances: <dictionary at 0x4071fa44>: {}
_links: None
_mapping_file_applied: <bool at 0x8130da0>: False
_model: <Model at 402e180c: <Model object at '/home/amk/.mbv/loader-test'>>
object already seen
_obj: <dictionary at 0x4071f68c>: {}
install_error: None
path: '/home/amk/.mbv/loader-test'
root_dir: '/home/amk/.mbv/loader-test'
services: <dictionary at 0x4071f79c>: {}


--amk
Jul 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

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
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
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.