Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to pickle unpicklable objects

Reply
Thread Tools

how to pickle unpicklable objects

 
 
Guy Lateur
Guest
Posts: n/a
 
      09-23-2005
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


 
Reply With Quote
 
 
 
 
Ido.Yehieli@gmail.com
Guest
Posts: n/a
 
      09-23-2005
interesting.
usually the [pickle | cpickle | marshal] modules should handle such
things

 
Reply With Quote
 
 
 
 
Hans Georg Krauthaeuser
Guest
Posts: n/a
 
      09-23-2005
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
 
Reply With Quote
 
Guy Lateur
Guest
Posts: n/a
 
      09-26-2005
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" <(E-Mail Removed)-magdeburg.de> schreef in bericht
news:dh10st$7kv$(E-Mail Removed)-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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
pickle error: can't pickle instancemethod objects Michele Simionato Python 2 05-23-2008 08:29 AM
can't pickle instancemethod objects Jim Lewis Python 6 07-09-2006 09:07 PM
a pickle's pickle temposs@gmail.com Python 4 08-02-2005 07:20 PM
Pickle vs. eval for deeply nested objects Edward C. Jones Python 0 02-18-2004 10:19 PM



Advertisments