473,383 Members | 1,877 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,383 software developers and data experts.

How to marshal objects to readable files?

Hi list:
I just want to marshal objects (instance of custom classes)to a human
*READABEL *file/string, and also, I want unmarshal it back. in xml
format or any other format.
Any advice? Which lib should I use?
Thanks a lot.
Sep 14 '08 #1
4 1913
On Sep 14, 10:28*am, nielinjie <nielin...@gmail.comwrote:
Hi list:
I just want to marshal objects (instance of custom classes)to a human
*READABEL *file/string, and also, I want unmarshal it back. in xml
format or any other format.
Any advice? Which lib should I use?
Thanks a lot.
Nielinjie,

There is no generic way; an object can contain a reference to another
object.

You can get started with ob.__dict__ as follows:
>>class A: pass
....
>>a= A()
a.b= 0
a.c= 'abc'
a.__dict__
{'c': 'abc', 'b': 0}

But the only way TMK (to my knowledge) to retrieve the contents is
eval, which is very hackish.

The PyYAML package produces the following (continued):
>>b= A()
b.d= 'efg'
b.e= 1.2
a.d= b
b.parent= a
print yaml.dump( a )
&id001 !!python/object:__main__.A
b: 0
c: abc
d: !!python/object:__main__.A
d: efg
e: 1.2
parent: *id001
>>print yaml.dump( b )
&id001 !!python/object:__main__.A
d: efg
e: 1.2
parent: !!python/object:__main__.A
b: 0
c: abc
d: *id001
>>yaml.load( yaml.dump( a ) )
<__main__.A instance at 0x00D098A0>
>>_.__dict__
{'c': 'abc', 'b': 0, 'd': <__main__.A instance at 0x00D09788>}

It caches IDs to reference circular references, and writes nested
objects in place. As you can see, you don't need to dump both 'a' and
'b'; 'a' contains 'b' already. You do need the types of the objects
already living in the same namespace as when you stored them.

If it's not human readable enough for your use, please write an
example of what you want your output to look like. Abstract is ok.

PyYAML is available at http://pyyaml.org/ . Download from
http://pyyaml.org/wiki/PyYAML . I've only skimmed it though.
Sep 14 '08 #2
En Sun, 14 Sep 2008 15:09:52 -0300, Aaron "Castironpi" Brady
<ca********@gmail.comescribió:
On Sep 14, 10:28*am, nielinjie <nielin...@gmail.comwrote:
>Hi list:
I just want to marshal objects (instance of custom classes)to a human
*READABEL *file/string, and also, I want unmarshal it back. in xml
format or any other format.
Any advice? Which lib should I use?

The PyYAML package produces the following (continued):
>>>print yaml.dump( a )
&id001 !!python/object:__main__.A
b: 0
c: abc
d: !!python/object:__main__.A
d: efg
e: 1.2
parent: *id001
JSON is another format, much simpler and probably more suited for human
reading. But it is somewhat limited on what it can represent. There are a
few Python implementations, maybe the most used is simplejson, which comes
bundled with Python 2.6 as the json module.

--
Gabriel Genellina

Sep 14 '08 #3
On Sep 14, 11:28 am, nielinjie <nielin...@gmail.comwrote:
Hi list:
I just want to marshal objects (instance of custom classes)to a human
*READABEL *file/string, and also, I want unmarshal it back. in xml
format or any other format.
Any advice? Which lib should I use?
Thanks a lot.
There is module pickle in the standard library. It's output is not all
that bad (in text mode, that is). If you don't like it, it would still
be a good starting point for a custom serializer - it does all the
tree walking for you, so you would only have to customize the strings
it writes and reads back in.

Sep 16 '08 #4
On Sep 14, 8:28*am, nielinjie <nielin...@gmail.comwrote:
Hi list:
I just want to marshal objects (instance of custom classes)to a human
*READABEL *file/string, and also, I want unmarshal it back. in xml
format or any other format.
Any advice? Which lib should I use?
Thanks a lot.
I think YAML is the solution most targeted for your needs. To quote
the website: "YAML is a human friendly data serialization standard for
all programming languages."

http://yaml.org/

If you're willing to sacrifice some readability for truly robust
serialization, try pickle, which isn't horribly cryptic.

If you want simplicity and AJAX interoperability, consider JSON.

If you consider Python itself to be human readable, and if your
security/performance situation does not preclude the use of eval(),
consider the PrettyPrinter module.

Sep 16 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mountain Bikn' Guy | last post by:
It is known that one cannot pass arguments as ref or out in a marshal-by-reference class. My problem is that I have a C DLL (and C# wrapper) that I need to isolate in an AppDomain and then I need...
3
by: Tom | last post by:
I think I'm still a little rough on the principle and understanding of Marshal by value and Marshal by reference after reading various materials. my understanding of Marshal by value is that the...
0
by: Johannes Unfried | last post by:
Problem Best practice needed to marshal STL data from managed code to unmanaged code & vice vers Details managed code is written in managed C++ & accesses the unmanaged code (i.e. lives in a...
4
by: Michael McGarry | last post by:
Hi, I am using the marshal module in python to save a data structure to a file. It does not appear to be portable. The data is saved on a Linux machine. Loading that same data on a Mac gives me...
2
by: jason | last post by:
hello everyone, i have had a suppot ticket open with microsoft for some time to investigate a memory leak issue we are experiencing on our production web servers. the web servers host both ASP...
0
by: metaperl | last post by:
A Comparison of Python Class Objects and Init Files for Program Configuration ============================================================================= Terrence Brannon bauhaus@metaperl.com...
5
by: Anurag | last post by:
I have been chasing a problem in my code since hours and it bolis down to this import marshal marshal.dumps(str(123)) != marshal.dumps(str("123")) Can someone please tell me why? when...
10
by: bkustel | last post by:
I'm stuck on a problem where I want to use marshal for serialization (yes, yes, I know (c)Pickle is normally recommended here). I favor marshal for speed for the types of data I use. However it...
0
by: xrxst32 | last post by:
Hello there, I have some doubts about the best practice for using COM automation, the Runtime Callable Wrapper (RCW) and Marshal.ReleaseComObject. So the question is/are: Do I need to release...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.