Data::Dumper for Python

E

Edward wijaya

Hi,

Is there any equivalent of it
in Python?

Thanks so much for your time.

Regards,
Edward WIJAYA
 
D

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
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.
 
E

Edward wijaya

Thanks for the informative reply, Dave.
I really appreciate that, for me as a new guy
in Python.

Regards,
Edward WIJAYA
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.
 
J

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
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.
 
D

Dave Benjamin

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.
 
G

Gisle Aas

Dave Benjamin said:
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).
 
J

Josiah Carlson

Gisle Aas said:
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

A.M. Kuchling

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/):
<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'

--amk
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top