XML Pickle with PyGraphLib - Problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike P.

    #1

    XML Pickle with PyGraphLib - Problems

    Hi all,

    I'm working on a simulation (can be considered a game) in Python where I
    want to be able to dump the simulation state to a file and be able to load
    it up later. I have used the standard Python pickle module and it works fine
    pickling/unpickling from files.

    However, I want to be able to use a third party tool like an XML editor (or
    other custom tool) to setup the initial state of the simulation, so I have
    been playing around with the gnosis set of tools written by David Mertz. In
    particular I have been using the gnosis.xml.pick le module. Anyway, this
    seems to work fine for most data structures, however I have problems getting
    it to work with pygraphlib version 0.6.0.1 (from sourceforge). My simulation
    needs to store data in a set of graphs, and pygraphlib seems like a nice
    simple python graph library, that I found easy to use.

    Anyway, the problem is that I can successfully pickle to XML a data
    structure containing a pygraphlib graph, but I have trouble
    reloading/unpickling the very same data structure that was produced by the
    XML pickle module. I get an XMLUnpicklingEr ror. Anyone have any ideas?

    Here's some output from the interactive prompt of a small example which
    demonstrates the error:

    =============== =============== =============== =============== ==============
    C:\home\src>pyt hon
    ActivePython 2.4 Build 244 (ActiveState Corp.) based on
    Python 2.4 (#60, Feb 9 2005, 19:03:27) [MSC v.1310 32 bit (Intel)] on win32
    Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
    >>> from pygraphlib import pygraph
    >>> graph = pygraph.DGraph( )
    >>> graph.add_edge( 'Melbourne', 'Sydney')
    >>> graph.add_edge( 'Melbourne', 'Brisbane')
    >>> graph.add_edge( 'Melbourne', 'Adelaide')
    >>> graph.add_edge( 'Adelaide', 'Perth')
    >>> print graph[/color][/color][/color]
    DGraph: 5 nodes, 4 edges[color=blue][color=green][color=darkred]
    >>> graph[/color][/color][/color]
    DGraph: 5 nodes, 4 edges
    Melbourne -> ['Sydney', 'Brisbane', 'Adelaide']
    Brisbane -> []
    Perth -> []
    Sydney -> []
    Adelaide -> ['Perth'][color=blue][color=green][color=darkred]
    >>> import gnosis.xml.pick le
    >>> file = open('graph.xml ', 'w')
    >>> gnosis.xml.pick le.dump(graph, file)
    >>> file.close()
    >>> f2 = open('graph.xml ', 'r')
    >>> g2 = gnosis.xml.pick le.load(f2)[/color][/color][/color]
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "C:\Python24\Li b\site-packages\gnosis \xml\pickle\_pi ckle.py", line
    152, in load
    return parser(fh, paranoia=parano ia)
    File "C:\Python24\li b\site-packages\gnosis \xml\pickle\par sers\_dom.py",
    line 42, in thing_from_dom
    return _thing_from_dom (minidom.parse( fh),None,parano ia)
    File "C:\Python24\li b\site-packages\gnosis \xml\pickle\par sers\_dom.py",
    line 175, in _thing_from_dom
    container = unpickle_instan ce(node, paranoia)
    File "C:\Python24\li b\site-packages\gnosis \xml\pickle\par sers\_dom.py",
    line 59, in unpickle_instan ce
    raw = _thing_from_dom (node, _EmptyClass(), paranoia)
    File "C:\Python24\li b\site-packages\gnosis \xml\pickle\par sers\_dom.py",
    line 234, in _thing_from_dom
    node_val = unpickle_instan ce(node, paranoia)
    File "C:\Python24\li b\site-packages\gnosis \xml\pickle\par sers\_dom.py",
    line 95, in unpickle_instan ce
    raise XMLUnpicklingEr ror, \
    gnosis.xml.pick le.XMLUnpicklin gError: Non-DictType without setstate violates
    pickle protocol.(PARAN OIA setting may be too high)[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]
    =============== =============== =============== =============== ===============

    I find it strange that:
    a) the standard pickle/unpickle works
    b) the xml pickle dumps the file but the xml unpickle can't load it.

    I'm guessing the problem lies somewhere with implementing __setstate__ and
    __getstate__ for pygraphlib (I don't know much about this - haven't used
    them before). However, I am a bit reluctant to go in and start playing
    around with the internals pygraphlib, as I was hoping to just use it, and
    ignore the internal implementation (as you would with any library). Funny
    how the standard pickle module doesn't need to do this.

    Another thing I tried was the generic xml marshaller (xml.marshal.ge neric)
    that comes with PyXML 0.8.4 (for Python 2.4 on windows).
    This also fails but for different reasons. The marshaller doesn't support
    the boolean and set types which are part of Python 2.4 and are used in
    pygraphlib.
    I get errors of the form:

    AttributeError: Marshaller instance has no attribute 'tag_bool'
    AttributeError: Marshaller instance has no attribute 'm_Set'

    Again strange given that bool and sets should be supported in Python 2.4.

    Anyway, back to my question - does anyone have any suggestions as to where I
    could proceed next? I was hoping to be able to XML Pickle/Unpickle Python
    data structures containing graphs from pygraphlib fairly easily without
    having to stuff around in the internals of third party libraries.
    It would be nice if I could just concentrate on my application logic :-)

    Any ideas?

    Cheers.

    Mike P.



Working...