How to serialize (or even pickle) event-objects?

Hi there!

Has anybody a good idea on how to serialize (or pickle) event-objects?
There's no possibility to pickle SWIG-objects, as far as I can see.

I wonder if there's a more easy way to make the attributes of an event-
object (or even the whole object) persistent than to ask the object
for every attribute (for example in the app's FilterEvent()-method).

A deepcopy of event-objects isn't possible, too.

TIA for answers!
Benjamin

Benjamin Herwig wrote:

Hi there!

Has anybody a good idea on how to serialize (or pickle) event-objects?
There's no possibility to pickle SWIG-objects, as far as I can see.

I wonder if there's a more easy way to make the attributes of an event-
object (or even the whole object) persistent than to ask the object
for every attribute (for example in the app's FilterEvent()-method).

A deepcopy of event-objects isn't possible, too.

TIA for answers!
Benjamin

I would just take the data returned by the event object's methods (mainly strings and ints), and stick them in a list/dictionary and pickle that.

···

--
Steven Sproat, BSc
http://www.basicrpg.com/

Hi Steven

Thank you. Sure, that'd be the "easiest" method. But I'm looking for a
more nifty way to make m objects and their data persistent.

Maybe someone else has any idea. If not: Steven's idea is the way to
go. :slight_smile:

···

On 28 Jan., 16:01, Steven Sproat <spro...@gmail.com> wrote:

Benjamin Herwig wrote:
> Hi there!

> Has anybody a good idea on how to serialize (or pickle) event-objects?
> There's no possibility to pickle SWIG-objects, as far as I can see.

> I wonder if there's a more easy way to make the attributes of an event-
> object (or even the whole object) persistent than to ask the object
> for every attribute (for example in the app's FilterEvent()-method).

> A deepcopy of event-objects isn't possible, too.

> TIA for answers!
> Benjamin

I would just take the data returned by the event object's methods
(mainly strings and ints), and stick them in a list/dictionary and
pickle that.

--
Steven Sproat, BSchttp://www.basicrpg.com/

Maybe you can use Andrea's PersistentControls script:

http://groups.google.com/group/wxpython-users/browse_frm/thread/e324699bea201f31#

···

On Jan 28, 12:40 pm, Benjamin Herwig <bher...@gmx.com> wrote:

Hi Steven

Thank you. Sure, that'd be the "easiest" method. But I'm looking for a
more nifty way to make m objects and their data persistent.

Maybe someone else has any idea. If not: Steven's idea is the way to
go. :slight_smile:

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

PyCon 2010 Atlanta Feb 19-21 http://us.pycon.org/

Benjamin Herwig escribi�:

Hi Steven

Thank you. Sure, that'd be the "easiest" method. But I'm looking for a
more nifty way to make m objects and their data persistent.

Maybe someone else has any idea. If not: Steven's idea is the way to
go. :slight_smile:

Benjamin, you can pickle swig objects making a simple hack to wx, this is, replacing the __reduce__ method.

I'm doing it for pickle wx.Bitmap, wx.Image, wx.Brush, etc..
I think that you can use the same technique to implementing new __reduce__ or __method in an event object

Example: pickling a wx.Font object:

>>> # Create a new reduce function:
>>> def _reduce_Font(self):
... if not self.IsOk():
... # invalid font, need a valid font to pickle
... return (self.__class__,
... (-1, -1, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL))
... else:
... return (self.__class__,
... (self.GetPointSize(),
... self.GetFamily(),
... self.GetStyle(),
... self.GetWeight(),
... self.GetUnderlined(),
... self.GetFaceName(),
... self.GetDefaultEncoding()))
...
>>> # Replace the original Font.__reduce__ method:
>>> wx.Font.__reduce__ = _reduce_Font
>>>
>>> # Now create the font and pickle it
>>> f = wx.Font(16, wx.FONTFAMILY_ROMAN, wx.FONTSTYLE_ITALIC, wx.FONTWEIGHT_BOLD)
>>> pickledFont = pickle.dumps(f)
>>>
>>> type(pickledFont)
<type 'str'>
>>>
>>> # Create new font using the pickled font
>>>
>>> unpickledFont = pickle.loads(pickedFont)
>>>
>>> type(unpickledFont)
<class 'wx._gdi.Font'>
>>>

If yours think that these technique may be interesting, I can wrote a new wiki article to show it with some wx objects.

Regards

···

--
*****************************************
Oswaldo Hern�ndez
oswaldo (@) soft-com (.) es
*****************************************
PD:
Antes de imprimir este mensaje, aseg�rese de que es necesario.
El medio ambiente est� en nuestra mano.

Hi!

Thank you all very much for your answers!

Mike and Oswaldo:
I'm afraid your (or Andrea's) solutions are a little bit to "heavy"
for my needs (and my skills).
I need a solution with as few LOC as possible.

Well, I decided to stop the propagation of the event and then resume
it after the event had been passed to a method where I read the
event's attributes as Steven suggested.
The main problem was (is), that I need many things to be done with the
event outside of the app's FilterEvent and whenever I passed the event
it got out-of-date and the event's data was just confusing.

Hopefully I can show you what I've done when my program works one
day. :wink:

@Oswaldo: Wiki-articles are always appreciated. :slight_smile:

Benjamin

···

On 28 Jan., 21:41, Oswaldo Hernández <lis...@soft-com.es> wrote:

Benjamin Herwig escribi :

> Hi Steven

> Thank you. Sure, that'd be the "easiest" method. But I'm looking for a
> more nifty way to make m objects and their data persistent.

> Maybe someone else has any idea. If not: Steven's idea is the way to
> go. :slight_smile:

Benjamin, you can pickle swig objects making a simple hack to wx, this
is, replacing the __reduce__ method.

I'm doing it for pickle wx.Bitmap, wx.Image, wx.Brush, etc..
I think that you can use the same technique to implementing new
__reduce__ or __method in an event object

Example: pickling a wx.Font object:

>>> # Create a new reduce function:
>>> def _reduce_Font(self):
... if not self.IsOk():
... # invalid font, need a valid font to pickle
... return (self.__class__,
... (-1, -1, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL))
... else:
... return (self.__class__,
... (self.GetPointSize(),
... self.GetFamily(),
... self.GetStyle(),
... self.GetWeight(),
... self.GetUnderlined(),
... self.GetFaceName(),
... self.GetDefaultEncoding()))
...
>>> # Replace the original Font.__reduce__ method:
>>> wx.Font.__reduce__ = _reduce_Font
>>>
>>> # Now create the font and pickle it
>>> f = wx.Font(16, wx.FONTFAMILY_ROMAN, wx.FONTSTYLE_ITALIC,
wx.FONTWEIGHT_BOLD)
>>> pickledFont = pickle.dumps(f)
>>>
>>> type(pickledFont)
<type 'str'>
>>>
>>> # Create new font using the pickled font
>>>
>>> unpickledFont = pickle.loads(pickedFont)
>>>
>>> type(unpickledFont)
<class 'wx._gdi.Font'>
>>>

If yours think that these technique may be interesting, I can wrote a
new wiki article to show it with some wx objects.

Regards

--
*****************************************
Oswaldo Hern ndez
oswaldo (@) soft-com (.) es
*****************************************
PD:
Antes de imprimir este mensaje, aseg rese de que es necesario.
El medio ambiente est en nuestra mano.

With __reduce__ you are just saving the parameters needed to be passed to the class' __init__ in order to recreate the object. While that works great for many kinds of objects, it won't work with many of the event classes because they have other attributes that are not set by the constructors.

···

On 1/28/10 12:41 PM, Oswaldo Hern�ndez wrote:

Benjamin Herwig escribi�:

Hi Steven

Thank you. Sure, that'd be the "easiest" method. But I'm looking for a
more nifty way to make m objects and their data persistent.

Maybe someone else has any idea. If not: Steven's idea is the way to
go. :slight_smile:

Benjamin, you can pickle swig objects making a simple hack to wx, this
is, replacing the __reduce__ method.

I'm doing it for pickle wx.Bitmap, wx.Image, wx.Brush, etc..
I think that you can use the same technique to implementing new
__reduce__ or __method in an event object

--
Robin Dunn
Software Craftsman