traits: change notifications, delegates and persistence

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

traits: change notifications, delegates and persistence

Gregor Thalhammer
Hi all,

I experience some problems with traits: under some circumstances change notifications are issued twice for delegated trait attributes after calling __setstate__

Please take a look at this minimal example I came up with:

#---------------------------------

from traits.api import *

from traits.trait_notifiers import set_change_event_tracers
def tracer(obj, name, old, new, handler):
    print "tracing", name, old, new
#set_change_event_tracers(pre_tracer = tracer)
   
class Child(HasTraits):
    parent = Instance('Parent')
    age = Int(0)
    celebrate_birthday = Event

    @on_trait_change('age')
    def age_has_changed(self, obj, name, old, new):
        print 'age changed from %d to %d'%(old, new)

    def _celebrate_birthday_fired(self):
        self.age += 1


class Parent(HasTraits):

    child = Instance(Child, transient=True)
    child_age = DelegatesTo('child', prefix='age')

    @on_trait_change('child_age')
    def child_age_has_changed(self, object, name, old, new):
        print 'child_age changed from %d to %d'%(old, new)

    def set_state(self):
        state = dict(__traits_version__ = '4.3.0')
        self.__setstate__(state)

child = Child(age=0)
parent = Parent(child=child)
parent.child.celebrate_birthday = True

parent.set_state()
parent.child.celebrate_birthday = True
#child_age_has_changed is called two times

#---------------------------------

As a result I observe:

age changed from 0 to 1
child_age changed from 0 to 1
age changed from 1 to 2
child_age changed from 1 to 2
child_age changed from 1 to 2

So after calling __setstate__ on parent change notifications for child_age are issued twice (with identical arguments). I have no idea why this happens. Please, can someone help me solving this problem.

My example is a stripped down version of my experiment control system, 'child' is in my case a data source which I like to change during runtime, and data is further processed by 'parent'. I use persistence to load/save the state. Traits and traitsui are really amazing tools for this task.

Thanks for your help
Gregor

_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev