Robert Kern | 6 Sep 00:23
Picon
Gravatar

Re: [Traits] dirty trait metadata

On Fri, Sep 5, 2008 at 14:43, Prabhu Ramachandran
<prabhu@...> wrote:
> Hi,
>
> Consider the following:
>
> -----------------
> from numpy import *
> from enthought.traits.api import *
> class Data(HasTraits):
>     x = Array
>     def _x_changed(self, value):
>         print value
>
> x = linspace(0, 1)
> d = Data(x=x)
> -------------------
>
> Now I'd like to always call _x_changed when the x trait is set.  Thus,
> I'd like this to work:
>
> x *= 2
> d.x = x # Should call _x_changed
>
> Is there a simple way of doing this?  I know that I could achieve this
> if I implemented x as a Property(Array) but is there an easier way?

I don't think so. The comparison logic is in C
(ctraits.c:setattr_trait), and there are no hooks for this.

If you only need coarse-grained notifications, you might consider
using an updated Event. This:

  d.x[5:10] = 0.0
  d.updated = True

is more obvious to me than this:

  d.x[5:10] = 0.0
  d.x = d.x

Of course, if you have several of these arrays, and updates to
different ones should cause different things to happen, then you would
need one Event per array. Which wouldn't be so bad, all considered.

  d.x[5:10] = 0.0
  d.x_updated = True

--

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
 -- Umberto Eco

Gmane