473,804 Members | 5,054 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7145
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***@lackingt alent.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***@lackingt alent.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***@activest ate.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_r epresentation(o bj)).

- Josiah

Jul 18 '05 #7
On Fri, 29 Oct 2004 00:34:31 -0700,
Josiah Carlson <jc******@uci.e du> 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: <HydraControlle r at 402ee86c>
_instances: <dictionary at 0x4071fa44>: {}
_links: None
_mapping_file_a pplied: <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
1877
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; my %tmp ;
0
5300
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 though the installation is apparently successful. Here is the output: CPAN: Storable loaded ok
14
3341
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 arbitrary data structure. I'd like to do the same with javascript. print Data::Dumper(document)
1
3430
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 solutions for: 1) Perl's Data::Dumper It dumps any perl variable to the stdout in a "readable" way.
0
1100
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 and not? I get really confused here. Please check my code and give me some idea or clue or help please :). Thanks in advance. My code is like this: Dim DBConnection As SqlConnection Dim WithEvents SPConnection As SerialPort = New...
1
2206
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' => { 'dow:ComprehensiveIncomeloss' => , 'dow:DistributionsFromNonconsolidatedAffiliates' =>
1
1648
crazy4perl
by: crazy4perl | last post by:
Hi, Is it possible to use data dumper to dump data into a file????
3
7348
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 uses unfamiliar terms and syntax. Intermediate and advanced Perl coders should find this article useful. The object of the article is to inform the reader, it is not about how to code Perl or how to write good Perl code, but to teach the Schwartzian...
1
3541
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
10577
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10320
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10077
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9150
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7620
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.