How to bind and event on the .update() of a dictionary ?

Robert VERGNES wrote:

Hello,
I have dictionary, let's say mydic and each time I use mydic.update(anotherdic)
I want that the update is done - as it should- but after that I want to trigger automatically the call to a function.
( because I want the GUI to refresh the dictionary).
What kind of event can I use to bind the to mydic and/or to .update ?

As you suggested deriving a new dict class would be most convenient. Then you
can either use callbacks, like:

class CallbackDict(dict):
    def __init__(self, callback, *args):
        self.callback = callback
        dict.__init__(self, *args)

    def update(self, data):
        dict.update(self, data)
        self.callback()

and intialise the dict with
d = CallbackDict(func_to_call_on_update, {'test':'data'})

or use custom wx events:

from wx.lib import newevent

UpdateEvent, EVT_UPDATE = newevent.NewCommandEvent()

class EventDict(dict):
    def __init__(self, notify, *args):
        self.notify = notify
        dict.__init__(self, *args)

    def update(self, data):
        dict.update(self, data)
        evt = UpdateEvent(self.notify.GetId())
        self.PostEvent(self.notify, evt)

and initialise the dict with

d = EventDict(wx_object_to_be_notified, {'test':'data})
wx_object_to_be_notified.Bind(wx.EVT_UPDATE, self.OnUpdate)

Christian

···

thanx
Robert
PS: of shall i go all the way to derivate a new kind of dictionary ?

---------------------------------
Découvrez une nouvelle façon d'obtenir des réponses à toutes vos questions ! Profitez des connaissances, des opinions et des expériences des internautes sur Yahoo! Questions/Réponses.