how to pickle unpicklable objects

Discussion in 'Python' started by Guy Lateur, Sep 23, 2005.

  1. Guy Lateur

    Guy Lateur Guest

    Hi all,

    I've been writing an application containing a lot of settings which can be
    changed by the user. I'm using wx.Config to read/write these settings (to
    the windows registry). This means I can only store strings, ints and floats.

    However, it would be very convenient if I could also store more general
    objects. It seems to work for wx.Colour, but not for wx.Font. It raises a
    "TypeError: can't pickle PySwigObject objects".

    Does anybody a way to get around this? Is there some other module that would
    allow writing general (small) objects to the registry? Some sort of
    serialiser or something?

    TIA,
    g
     
    Guy Lateur, Sep 23, 2005
    #1
    1. Advertising

  2. Guy Lateur

    Guest

    interesting.
    usually the [pickle | cpickle | marshal] modules should handle such
    things
     
    , Sep 23, 2005
    #2
    1. Advertising

  3. Guy Lateur schrieb:
    > Hi all,
    >
    > I've been writing an application containing a lot of settings which can be
    > changed by the user. I'm using wx.Config to read/write these settings (to
    > the windows registry). This means I can only store strings, ints and floats.
    >
    > However, it would be very convenient if I could also store more general
    > objects. It seems to work for wx.Colour, but not for wx.Font. It raises a
    > "TypeError: can't pickle PySwigObject objects".


    The object is wrapped by SWIG. So, python can not know anything about it
    and the object can not be pickled.

    As far as I see, there are two possibilities
    - define __getstate__ and __setstate__ in the c/c++-source or the .i
    file (used by swig). This is only possible if the source is available
    - use copy_reg (http://docs.python.org/lib/module-copyreg.html) to
    register a 'reduce' function (I never used that).

    I use the first option in the .i-File for a wrapped c++-class like this:

    %extend UMDMResult {
    %insert("python") %{
    def __getstate__(self):
    return (self.v,self.u,self.l,self.unit,self.Z0,self.Eta0,self.t)
    def __setstate__(self,tup):
    self.this = _umddevice.new_UMDMResult(tup[0],tup[1],tup[2],tup[3])
    self.thisown=1
    (self.Z0,self.Eta0,self.t)=[i for i in tup[4:]]
    %}
    }

    regards
    Hans Georg Krauthaeuser
     
    Hans Georg Krauthaeuser, Sep 23, 2005
    #3
  4. Guy Lateur

    Guy Lateur Guest

    how to use copy_reg/pickle (was Re: how to pickle unpicklable objects)

    Thanks for the swift reply, Hans, and sorry for my delayed reaction.

    I've been looking at the copy_reg module, but I can't seem to get it to
    work.

    wx.Font has a method called GetNativeFontInfo(), which returns a string
    description of the font. Here's what I had hoped would have worked.

    Code:
    def reducef(objf):
        return str(objf.GetNativeFontInfo())
    def constrf(strf):
        rv = wx.Font()
        rv.SetNativeFontInfo(wx.String(strf))
        return rv
    copy_reg.pickle(wx.Font, reducef, constrf)
    valf = wx.Font(10, wx.NORMAL, wx.NORMAL, wx.NORMAL, False, 'Arial')
    rvf = pickle.dumps(valf)
    
    Unfortunately, this raises the exception:
    pickle.PicklingError: Can't pickle <wx._gdi.Font; proxy of C++ wxFont
    instance at _c8bb6101_p_wxFont>: it's not found as
    wx._gdi.0;-13;0;0;0;400;0;0;0;1;0;0;0;32;Arial

    Like I said, this is how I hoped it would work. I've read in the pickle docs
    you should return the name of a global var containing the value to be
    pickled as usual. I've tried this (without really understanding it), too,
    but without any success.

    Could somebody please help me to modify this example so that it works?

    Best regards,
    g




    "Hans Georg Krauthaeuser" <-magdeburg.de> schreef in bericht
    news:dh10st$7kv$-magdeburg.de...
    > Guy Lateur schrieb:
    >> Hi all,
    >>
    >> I've been writing an application containing a lot of settings which can
    >> be
    >> changed by the user. I'm using wx.Config to read/write these settings (to
    >> the windows registry). This means I can only store strings, ints and
    >> floats.
    >>
    >> However, it would be very convenient if I could also store more general
    >> objects. It seems to work for wx.Colour, but not for wx.Font. It raises a
    >> "TypeError: can't pickle PySwigObject objects".

    >
    > The object is wrapped by SWIG. So, python can not know anything about it
    > and the object can not be pickled.
    >
    > As far as I see, there are two possibilities
    > - define __getstate__ and __setstate__ in the c/c++-source or the .i
    > file (used by swig). This is only possible if the source is available
    > - use copy_reg (http://docs.python.org/lib/module-copyreg.html) to
    > register a 'reduce' function (I never used that).
    >
    > I use the first option in the .i-File for a wrapped c++-class like this:
    >
    > %extend UMDMResult {
    > %insert("python") %{
    > def __getstate__(self):
    > return (self.v,self.u,self.l,self.unit,self.Z0,self.Eta0,self.t)
    > def __setstate__(self,tup):
    > self.this = _umddevice.new_UMDMResult(tup[0],tup[1],tup[2],tup[3])
    > self.thisown=1
    > (self.Z0,self.Eta0,self.t)=[i for i in tup[4:]]
    > %}
    > }
    >
    > regards
    > Hans Georg Krauthaeuser
     
    Guy Lateur, Sep 26, 2005
    #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. Edward C. Jones

    Pickle vs. eval for deeply nested objects

    Edward C. Jones, Feb 18, 2004, in forum: Python
    Replies:
    0
    Views:
    488
    Edward C. Jones
    Feb 18, 2004
  2. a pickle's pickle

    , Aug 2, 2005, in forum: Python
    Replies:
    4
    Views:
    387
  3. Jim Lewis

    can't pickle instancemethod objects

    Jim Lewis, Jul 9, 2006, in forum: Python
    Replies:
    6
    Views:
    1,294
    Jim Lewis
    Jul 9, 2006
  4. Steven Bethard

    pickle and instancemethod objects

    Steven Bethard, Sep 13, 2006, in forum: Python
    Replies:
    2
    Views:
    412
    Steven Bethard
    Sep 13, 2006
  5. Michele Simionato
    Replies:
    2
    Views:
    1,889
    Michele Simionato
    May 23, 2008
Loading...

Share This Page