David C. Morrill | 18 Jul 17:33

Re: Automatically Triggering post_settattr After Value Assign by _*_default Method

Good catch on this one! The problem has been fixed and checked in (you 
will need to rebuild the ctraits module also). My original comment that 
it should have been called was correct, but it turns out that it was 
only being called in some cases, but not the most common case (stupid 
oversight). Thanks for sending the test case and reporting it!

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
>
>   

Gmane