Portable readable dump format

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Stiles

    Portable readable dump format


    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
  • Donald 'Paddy' McCarthy

    #2
    Re: Portable readable dump format



    Chris Stiles wrote:[color=blue]
    > 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.
    >[/color]

    There is YAML http://yaml.org/

    and http://python.yaml.org/, http://yaml.freepan.org/index.cgi?LibSyck

    Comment

    • Chris Stiles

      #3
      Re: Portable readable dump format

      Donald 'Paddy' McCarthy <paddy3118@nets cape.netNOTthis BIT> writes:[color=blue]
      > There is YAML http://yaml.org/
      >
      > and http://python.yaml.org/, http://yaml.freepan.org/index.cgi?LibSyck[/color]

      Thank you. That was exactly the library I was after.

      --
      regards, chris

      Comment

      • David Mertz

        #4
        Re: Portable readable dump format

        Chris Stiles <chris@example. org> 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.pick le in Gnosis Utilities.

        Some comparisons:
        [color=blue][color=green][color=darkred]
        >>> import gnosis.xml.pick le
        >>> import yaml
        >>> class Something:[/color][/color][/color]
        ... def __init__(self):
        ... self.tup = (1,2,3)
        ... self.dct = {1.4:5.6,7.8:9. 0}
        ... self.str = "Spam and eggs"
        ...[color=blue][color=green][color=darkred]
        >>> o = Something()
        >>> print gnosis.xml.pick le.dumps(o)[/color][/color][/color]
        <?xml version="1.0"?>
        <!DOCTYPE PyObject SYSTEM "PyObjects.dtd" >
        <PyObject module="__main_ _" class="Somethin g" 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.399999 9999999999" />
        <val type="numeric" value="5.599999 9999999996" />
        </entry>
        <entry>
        <key type="numeric" value="7.799999 9999999998" />
        <val type="numeric" value="9." />
        </entry>
        </attr>
        <attr name="str" type="string" value="Spam and eggs" />
        </PyObject>[color=blue][color=green][color=darkred]
        >>> print yaml.dump(o)[/color][/color][/color]
        --- !!__main__.Some thing
        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:
        [color=blue][color=green][color=darkred]
        >>> class NewSomething(ob ject):[/color][/color][/color]
        ... def __init__(self):
        ... self.tup = (1,2,3)
        ... self.dct = {1.4:5.6,7.8:9. 0}
        ... self.str = "Spam and eggs"
        ...[color=blue][color=green][color=darkred]
        >>> o2 = NewSomething()
        >>> print yaml.dump(o2)[/color][/color][/color]
        --- <__main__.NewSo mething object at 0x2abbcc>

        Gnosis does fine here:
        [color=blue][color=green][color=darkred]
        >>> print gnosis.xml.pick le.dumps(o2)[/color][/color][/color]
        <?xml version="1.0"?>
        <!DOCTYPE PyObject SYSTEM "PyObjects.dtd" >
        <PyObject module="__main_ _" class="NewSomet hing" 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.399999 9999999999" />
        <val type="numeric" value="5.599999 9999999996" />
        </entry>
        <entry>
        <key type="numeric" value="7.799999 9999999998" />
        <val type="numeric" value="9." />
        </entry>
        </attr>
        <attr name="str" type="string" value="Spam and eggs" />
        </PyObject>

        Moreover, notice this:
        [color=blue][color=green][color=darkred]
        >>> class ListandTuple:[/color][/color][/color]
        ... def __init__(self):
        ... self.tup = (1,2,3)
        ... self.lst = [1,2,3]
        ...[color=blue][color=green][color=darkred]
        >>> lt = ListandTuple()
        >>> print yaml.dump(lt)[/color][/color][/color]
        --- !!__main__.List andTuple
        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.pick le 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...idea s have nothing to lose but their chains. Unite
        | against "intellectu al property" and anti-privacy regimes!
        -------------------------------------------------------------------------


        Comment

        Working...