473,399 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

how to pickle unpicklable objects

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
Sep 23 '05 #1
3 11459
interesting.
usually the [pickle | cpickle | marshal] modules should handle such
things

Sep 23 '05 #2
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
Sep 23 '05 #3
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.

Expand|Select|Wrap|Line Numbers
  1. def reducef(objf):
  2. return str(objf.GetNativeFontInfo())
  3. def constrf(strf):
  4. rv = wx.Font()
  5. rv.SetNativeFontInfo(wx.String(strf))
  6. return rv
  7. copy_reg.pickle(wx.Font, reducef, constrf)
  8. valf = wx.Font(10, wx.NORMAL, wx.NORMAL, wx.NORMAL, False, 'Arial')
  9. rvf = pickle.dumps(valf)
  10.  
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" <hg*@et.uni-magdeburg.de> schreef in bericht
news:dh**********@fuerst.cs.uni-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

Sep 26 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Christian Tismer | last post by:
Martin v. Löwis wrote: > "Mark Hahn" <mark@hahnca.com> writes: > > >>I don't understand how this could happen with pickle. Isn't it supposed to >>stop when it runs into an object it has...
0
by: Jean-Luc | last post by:
Hi I am grafing pickling functionality on a big dictionary of object instances and I am having numerous problems. I think understand the notion of what can/can't be pickled and I know how to...
2
by: Boris Borcic | last post by:
Assuming that the items of my_stream share no content (they are dumps of db cursor fetches), is there a simple way to do the equivalent of def pickles(my_stream) : from cPickle import...
6
by: Jim Lewis | last post by:
Pickling an instance of a class, gives "can't pickle instancemethod objects". What does this mean? How do I find the class method creating the problem?
2
by: Steven Bethard | last post by:
I'd like to be able to pickle instancemethod objects mainly because I want to be able to delay a call like ``foo(spam, badger)`` by dumping ``foo``, ``spam`` and ``badger`` to disk and loading them...
3
by: fizilla | last post by:
Hello all! I have the following weird problem and since I am new to Python I somehow cannot figure out an elegant solution. The problem reduces to the following question: How to pickle a...
2
by: Mario Ceresa | last post by:
Hello everybody: I'd like to use the pickle module to save the state of an object so to be able to restore it later. The problem is that it holds a list of other objects, say numbers, and if I...
2
by: Michele Simionato | last post by:
Can somebody explain what's happening with the following script? $ echo example.py import pickle class Example(object): def __init__(self, obj, registry): self._obj = obj self._registry =...
10
by: est | last post by:
>>import md5 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python25\lib\pickle.py", line 1366, in dumps Pickler(file, protocol).dump(obj) File...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.