Portable readable dump format

Discussion in 'Python' started by Chris Stiles, Oct 26, 2003.

  1. Chris Stiles

    Chris Stiles Guest

    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.

    --
    regards, chris
     
    Chris Stiles, Oct 26, 2003
    #1
    1. Advertising

  2. Donald 'Paddy' McCarthy, Oct 26, 2003
    #2
    1. Advertising

  3. Chris Stiles

    Chris Stiles Guest

    Chris Stiles, Oct 26, 2003
    #3
  4. Chris Stiles

    David Mertz Guest

    Chris Stiles <> wrote previously:
    |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:

    >>> import gnosis.xml.pickle
    >>> import yaml
    >>> class Something:

    ... def __init__(self):
    ... self.tup = (1,2,3)
    ... self.dct = {1.4:5.6,7.8:9.0}
    ... self.str = "Spam and eggs"
    ...
    >>> o = Something()
    >>> print gnosis.xml.pickle.dumps(o)

    <?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>
    <attr name="str" type="string" value="Spam and eggs" />
    </PyObject>
    >>> print yaml.dump(o)

    --- !!__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:

    >>> class NewSomething(object):

    ... def __init__(self):
    ... self.tup = (1,2,3)
    ... self.dct = {1.4:5.6,7.8:9.0}
    ... self.str = "Spam and eggs"
    ...
    >>> o2 = NewSomething()
    >>> print yaml.dump(o2)

    --- <__main__.NewSomething object at 0x2abbcc>

    Gnosis does fine here:

    >>> print gnosis.xml.pickle.dumps(o2)

    <?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:

    >>> class ListandTuple:

    ... def __init__(self):
    ... self.tup = (1,2,3)
    ... self.lst = [1,2,3]
    ...
    >>> lt = ListandTuple()
    >>> print yaml.dump(lt)

    --- !!__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!
    -------------------------------------------------------------------------
     
    David Mertz, Oct 26, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. halfdog
    Replies:
    12
    Views:
    12,515
  2. James Gan
    Replies:
    11
    Views:
    583
    Kent Johnson
    Oct 19, 2005
  3. reuven
    Replies:
    1
    Views:
    1,462
    Mike Treseler
    Jul 27, 2006
  4. Satish

    Julian timestamp to readable format

    Satish, Oct 5, 2004, in forum: C Programming
    Replies:
    2
    Views:
    2,879
    Satish
    Oct 11, 2004
  5. Wesley
    Replies:
    1
    Views:
    155
    Wesley
    Apr 15, 2014
Loading...

Share This Page