Discussion:
Data::Dumper for Python
Jeremy Bowers
2004-10-28 18:50:12 UTC
Permalink
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
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.
Gisle Aas
2004-10-29 07:09:37 UTC
Permalink
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
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).
Josiah Carlson
2004-10-29 07:34:31 UTC
Permalink
Post by Gisle Aas
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
A.M. Kuchling
2004-10-29 12:20:53 UTC
Permalink
On Fri, 29 Oct 2004 00:34:31 -0700,
Post by Josiah Carlson
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,
Post by Josiah Carlson
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

Edward wijaya
2004-10-28 18:18:38 UTC
Permalink
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
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
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
2004-10-28 20:08:04 UTC
Permalink
Post by Jeremy Bowers
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
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."
Dave Benjamin
2004-10-28 18:06:12 UTC
Permalink
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
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."
Edward wijaya
2004-10-28 07:25:14 UTC
Permalink
Hi,

Is there any equivalent of it
in Python?

Thanks so much for your time.

Regards,
Edward WIJAYA
Continue reading on narkive:
Loading...