How to marshal objects to readable files?

N

nielinjie

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

Aaron \Castironpi\ Brady

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:
{'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):
&id001 !!python/object:__main__.A
b: 0
c: abc
d: !!python/object:__main__.A
d: efg
e: 1.2
parent: *id001
&id001 !!python/object:__main__.A
d: efg
e: 1.2
parent: !!python/object:__main__.A
b: 0
c: abc
d: *id001{'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.
 
G

Gabriel Genellina

En Sun, 14 Sep 2008 15:09:52 -0300, Aaron "Castironpi" Brady
The PyYAML package produces the following (continued):

&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.
 
M

Michael Palmer

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

showellshowell

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.
 

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

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top