18 Jul 17:17
Re: Automatically Triggering post_settattr After Value Assign by _*_default Method
From: David C. Morrill <dmorrill@...>
Subject: Re: Automatically Triggering post_settattr After Value Assign by _*_default Method
Newsgroups: gmane.comp.python.enthought.devel
Date: 2008-07-18 15:17:02 GMT
Subject: Re: Automatically Triggering post_settattr After Value Assign by _*_default Method
Newsgroups: gmane.comp.python.enthought.devel
Date: 2008-07-18 15:17:02 GMT
Hmm..OK, let me take a look at it. I'll let you know what I find... Dave Morrill Alexander Michael wrote: > On Thu, Jul 17, 2008 at 8:02 PM, David C. Morrill > <dmorrill@...> wrote: > >> Alexander Michael wrote: >> >>> If have a TraitType with a post_setattr method. Is there a way to >>> indicate that I would like the post_setattr to be called when the >>> value is set by a default method? >>> >> I don't think you need to indicate anything, it should just happen. I >> you observe otherwise, please let me know... >> > > Hmm. Here's my example: > > import enthought.traits.api as traits > > class MyData(traits.HasTraits): > pass > > class MyObject(traits.HasTraits): > data = traits.Dict(traits.Str, traits.Instance(MyData)) > > class MyObjectDataTrait(traits.BaseInstance): > def __init__(self, **metadata): > super(MyObjectDataTrait, self).__init__(MyData, > allow_none=True, > **metadata > ) > > def validate(self, object, name, value): > if (isinstance(object, MyObject) > and isinstance(value, MyData)): > return value > self.error(object, name, value) > > def post_setattr(self, object, name, value): > print 'post_setattr', object, name, value > object.data[name] = value > > class MyDerivedObject(MyObject): > my_data = MyObjectDataTrait > > def _my_data_default(self): > print '_my_data_default' > return MyData() > > obj = MyDerivedObject() > print obj.my_data # post_setattr isn't executed > #obj.my_data = MyData() # post_setattr is executed > > Should this work? My svn rev may be a few weeks old, I'll refresh it. > > Thanks, > Alex > > _______________________________________________ > Enthought-dev mailing list > Enthought-dev@... > https://mail.enthought.com/mailman/listinfo/enthought-dev > >
RSS Feed