473,406 Members | 2,849 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,406 software developers and data experts.

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

Similar topics

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: Lemune | last post by:
Hello everyone. I'm creating windows service application to capture data from my PABX, and send the data to sql server. My question is how could my application know when that PABX is sending data...
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????
3
KevinADC
by: KevinADC | last post by:
If you are entirely unfamiliar with using Perl to sort data, read the "Sorting Data with Perl - Part One and Two" articles before reading this article. Beginning Perl coders may find this article...
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/
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.