Portable readable dump format

C

Chris Stiles

Is there a human readable dump format for objects ? As opposed to pickling ?
It doesn't necessarily have to be XML - in fact i'd prefer if it wasn't ;) -
but ISTR an XML dump library.
 
D

David Mertz

|Is there a human readable dump format for objects ? As opposed to
|pickling ? It doesn't necessarily have to be XML - in fact i'd prefer
|if it wasn't ;) - but ISTR an XML dump library.

Another poster pointed to YAML, which is a good format. If you want
XML, however, I have gnosis.xml.pickle in Gnosis Utilities.

Some comparisons:
... def __init__(self):
... self.tup = (1,2,3)
... self.dct = {1.4:5.6,7.8:9.0}
... self.str = "Spam and eggs"
... <?xml version="1.0"?>
<!DOCTYPE PyObject SYSTEM "PyObjects.dtd">
<PyObject module="__main__" class="Something" id="2283692">
<attr name="tup" type="tuple" id="2577828" >
<item type="numeric" value="1" />
<item type="numeric" value="2" />
<item type="numeric" value="3" />
</attr>
<attr name="dct" type="dict" id="2829140" >
<entry>
<key type="numeric" value="1.3999999999999999" />
<val type="numeric" value="5.5999999999999996" />
</entry>
<entry>
<key type="numeric" value="7.7999999999999998" />
<val type="numeric" value="9." />
</entry>
</attr>
--- !!__main__.Something
dct:
1.4: 5.6
7.8: 9.0
str: Spam and eggs
tup:
- 1
- 2
- 3

Certainly YAML is a lot less verbose (but also in a way less explicit).
The method 'yaml.dump()' is misnamed, however, it should have an 's' at
the end.

However, notice this:
... def __init__(self):
... self.tup = (1,2,3)
... self.dct = {1.4:5.6,7.8:9.0}
... self.str = "Spam and eggs"
... --- <__main__.NewSomething object at 0x2abbcc>

Gnosis does fine here:
<?xml version="1.0"?>
<!DOCTYPE PyObject SYSTEM "PyObjects.dtd">
<PyObject module="__main__" class="NewSomething" id="2800588">
<attr name="tup" type="tuple" id="2576628" >
<item type="numeric" value="1" />
<item type="numeric" value="2" />
<item type="numeric" value="3" />
</attr>
<attr name="dct" type="dict" id="2829820" >
<entry>
<key type="numeric" value="1.3999999999999999" />
<val type="numeric" value="5.5999999999999996" />
</entry>
<entry>
<key type="numeric" value="7.7999999999999998" />
<val type="numeric" value="9." />
</entry>
</attr>
<attr name="str" type="string" value="Spam and eggs" />
</PyObject>

Moreover, notice this:
... def __init__(self):
... self.tup = (1,2,3)
... self.lst = [1,2,3]
... --- !!__main__.ListandTuple
lst:
- 1
- 2
- 3
tup:
- 1
- 2
- 3

YAML is lossy in regard to Python types. Some other languages supported
by the YAML format do not have the list/tuple distinction, but since
Python does, usually you want to keep it (which gnosis.xml.pickle does).

Yours, David...

--
mertz@ | The specter of free information is haunting the `Net! All the
gnosis | powers of IP- and crypto-tyranny have entered into an unholy
..cx | alliance...ideas have nothing to lose but their chains. Unite
| against "intellectual property" and anti-privacy regimes!
-------------------------------------------------------------------------
 

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,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top