Trait notifications not working on lists?

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Trait notifications not working on lists?

Jaidev Deshpande
Hello,

Does the standard _trait_changed(self, ...) method not work on lists
in some cases?

Say I have a class like this:

from traits.api import HasTraits, List

class SimpleList(HasTraits):
    mylist = List

    def __init__(self):
        self.mylist = []

    def _mylist_changed(self, new):
        print new

And then if I try this:

sl = SimpleList()

sl.mylist.append(1)

The last step simply appends 1 to sl.mylist, but does not execute the
_mylist_changed() method. Where am I going wrong?

Thanks
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Trait notifications not working on lists?

Chris Colbert
you need to use an 'items' event for that:

In [1]: from traits.api import *

In [2]: class Foo(HasTraits):
   ...:     a = List
   ...:     def _a_items_changed(self, evt):
   ...:         print 'heard'
   ...:         

In [3]: f = Foo()

In [4]: f.a
Out[4]: []

In [5]: f.a.append(1)
heard

If you haven't yet, you should read this doc which covers the extended trait change syntax:


On Wed, Aug 8, 2012 at 8:30 PM, Jaidev Deshpande <[hidden email]> wrote:
Hello,

Does the standard _trait_changed(self, ...) method not work on lists
in some cases?

Say I have a class like this:

from traits.api import HasTraits, List

class SimpleList(HasTraits):
    mylist = List

    def __init__(self):
        self.mylist = []

    def _mylist_changed(self, new):
        print new

And then if I try this:

sl = SimpleList()

sl.mylist.append(1)

The last step simply appends 1 to sl.mylist, but does not execute the
_mylist_changed() method. Where am I going wrong?

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


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Trait notifications not working on lists?

Jaidev Deshpande
On Thu, Aug 9, 2012 at 7:36 AM, Chris Colbert <[hidden email]> wrote:
> you need to use an 'items' event for that:

Okay, that works.

> In [1]: from traits.api import *
>
> In [2]: class Foo(HasTraits):
>    ...:     a = List
>    ...:     def _a_items_changed(self, evt):
>    ...:         print 'heard'
>    ...:
>
> In [3]: f = Foo()
>
> In [4]: f.a
> Out[4]: []
>
> In [5]: f.a.append(1)
> heard
>
> If you haven't yet, you should read this doc which covers the extended trait
> change syntax:
> http://code.enthought.com/projects/traits/docs/html/traits_user_manual/notification.html

Thanks

>
> On Wed, Aug 8, 2012 at 8:30 PM, Jaidev Deshpande
> <[hidden email]> wrote:
>>
>> Hello,
>>
>> Does the standard _trait_changed(self, ...) method not work on lists
>> in some cases?
>>
>> Say I have a class like this:
>>
>> from traits.api import HasTraits, List
>>
>> class SimpleList(HasTraits):
>>     mylist = List
>>
>>     def __init__(self):
>>         self.mylist = []
>>
>>     def _mylist_changed(self, new):
>>         print new
>>
>> And then if I try this:
>>
>> sl = SimpleList()
>>
>> sl.mylist.append(1)
>>
>> The last step simply appends 1 to sl.mylist, but does not execute the
>> _mylist_changed() method. Where am I going wrong?
>>
>> Thanks
>> _______________________________________________
>> Enthought-Dev mailing list
>> [hidden email]
>> https://mail.enthought.com/mailman/listinfo/enthought-dev
>
>
>
> _______________________________________________
> Enthought-Dev mailing list
> [hidden email]
> https://mail.enthought.com/mailman/listinfo/enthought-dev
>
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Trait notifications not working on lists?

Robert Kern
In reply to this post by Jaidev Deshpande
On Thu, Aug 9, 2012 at 1:30 AM, Jaidev Deshpande
<[hidden email]> wrote:

> Hello,
>
> Does the standard _trait_changed(self, ...) method not work on lists
> in some cases?
>
> Say I have a class like this:
>
> from traits.api import HasTraits, List
>
> class SimpleList(HasTraits):
>     mylist = List
>
>     def __init__(self):
>         self.mylist = []

Also, if you override __init__() you must call super(SimpleList,
self).__init__(). Some Traits notifications will work, but some won't.

--
Robert Kern
Enthought
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Trait notifications not working on lists?

Adam Hughes
In reply to this post by Jaidev Deshpande
Is there any difference between using static methods and decorators in the following manner:

def _a_items_changed(self, evt):
   print 'heard'


@on_trait_change(a.items)
def update(self):
    print 'heard'

It seems like the static listener method is also using the extended trait name syntax with underscores separating attribue levels rather than periods.  Or is the "_items" call special to the List class?

On Thu, Aug 9, 2012 at 4:52 PM, Jaidev Deshpande <[hidden email]> wrote:
On Thu, Aug 9, 2012 at 7:36 AM, Chris Colbert <[hidden email]> wrote:
> you need to use an 'items' event for that:

Okay, that works.

> In [1]: from traits.api import *
>
> In [2]: class Foo(HasTraits):
>    ...:     a = List
>    ...:     def _a_items_changed(self, evt):
>    ...:         print 'heard'
>    ...:
>
> In [3]: f = Foo()
>
> In [4]: f.a
> Out[4]: []
>
> In [5]: f.a.append(1)
> heard
>
> If you haven't yet, you should read this doc which covers the extended trait
> change syntax:
> http://code.enthought.com/projects/traits/docs/html/traits_user_manual/notification.html

Thanks

>
> On Wed, Aug 8, 2012 at 8:30 PM, Jaidev Deshpande
> <[hidden email]> wrote:
>>
>> Hello,
>>
>> Does the standard _trait_changed(self, ...) method not work on lists
>> in some cases?
>>
>> Say I have a class like this:
>>
>> from traits.api import HasTraits, List
>>
>> class SimpleList(HasTraits):
>>     mylist = List
>>
>>     def __init__(self):
>>         self.mylist = []
>>
>>     def _mylist_changed(self, new):
>>         print new
>>
>> And then if I try this:
>>
>> sl = SimpleList()
>>
>> sl.mylist.append(1)
>>
>> The last step simply appends 1 to sl.mylist, but does not execute the
>> _mylist_changed() method. Where am I going wrong?
>>
>> Thanks
>> _______________________________________________
>> Enthought-Dev mailing list
>> [hidden email]
>> https://mail.enthought.com/mailman/listinfo/enthought-dev
>
>
>
> _______________________________________________
> Enthought-Dev mailing list
> [hidden email]
> https://mail.enthought.com/mailman/listinfo/enthought-dev
>
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Trait notifications not working on lists?

Chris Colbert
There are some subtle behavioral differences concerning when the handler is run during initialization.

Handlers decorated with @on_trait_change will not fire until HasTraits.__init__ is called. The static notifiers, however, will.

On Thu, Aug 9, 2012 at 7:27 PM, Adam Hughes <[hidden email]> wrote:
Is there any difference between using static methods and decorators in the following manner:

def _a_items_changed(self, evt):
   print 'heard'


@on_trait_change(a.items)
def update(self):
    print 'heard'

It seems like the static listener method is also using the extended trait name syntax with underscores separating attribue levels rather than periods.  Or is the "_items" call special to the List class?


On Thu, Aug 9, 2012 at 4:52 PM, Jaidev Deshpande <[hidden email]> wrote:
On Thu, Aug 9, 2012 at 7:36 AM, Chris Colbert <[hidden email]> wrote:
> you need to use an 'items' event for that:

Okay, that works.

> In [1]: from traits.api import *
>
> In [2]: class Foo(HasTraits):
>    ...:     a = List
>    ...:     def _a_items_changed(self, evt):
>    ...:         print 'heard'
>    ...:
>
> In [3]: f = Foo()
>
> In [4]: f.a
> Out[4]: []
>
> In [5]: f.a.append(1)
> heard
>
> If you haven't yet, you should read this doc which covers the extended trait
> change syntax:
> http://code.enthought.com/projects/traits/docs/html/traits_user_manual/notification.html

Thanks

>
> On Wed, Aug 8, 2012 at 8:30 PM, Jaidev Deshpande
> <[hidden email]> wrote:
>>
>> Hello,
>>
>> Does the standard _trait_changed(self, ...) method not work on lists
>> in some cases?
>>
>> Say I have a class like this:
>>
>> from traits.api import HasTraits, List
>>
>> class SimpleList(HasTraits):
>>     mylist = List
>>
>>     def __init__(self):
>>         self.mylist = []
>>
>>     def _mylist_changed(self, new):
>>         print new
>>
>> And then if I try this:
>>
>> sl = SimpleList()
>>
>> sl.mylist.append(1)
>>
>> The last step simply appends 1 to sl.mylist, but does not execute the
>> _mylist_changed() method. Where am I going wrong?
>>
>> Thanks
>> _______________________________________________
>> Enthought-Dev mailing list
>> [hidden email]
>> https://mail.enthought.com/mailman/listinfo/enthought-dev
>
>
>
> _______________________________________________
> Enthought-Dev mailing list
> [hidden email]
> https://mail.enthought.com/mailman/listinfo/enthought-dev
>
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev


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



_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Trait notifications not working on lists?

Robert Kern
In reply to this post by Adam Hughes
On Fri, Aug 10, 2012 at 12:27 AM, Adam Hughes <[hidden email]> wrote:
> Is there any difference between using static methods and decorators in the
> following manner:
>
> def _a_items_changed(self, evt):
>    print 'heard'
>
>
> @on_trait_change(a.items)

This is actually spelled

@on_trait_change('a_items')

> def update(self):
>     print 'heard'
>
> It seems like the static listener method is also using the extended trait
> name syntax with underscores separating attribue levels rather than periods.
> Or is the "_items" call special to the List class?

List, Dict, and Set traits explicitly emit a notification with the
trait name 'a_items' when they are internally modified. This is a
single, non-extended trait name, just one that is not represented as
an attribute on the object; it only receives notifications. The
extended trait name 'a.items' would actually listen to changes on the
'items' trait of all of the objects in the 'a' List.

--
Robert Kern
Enthought
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Trait notifications not working on lists?

Adam Hughes
Thanks for the explanation.

On Fri, Aug 10, 2012 at 4:54 AM, Robert Kern <[hidden email]> wrote:
On Fri, Aug 10, 2012 at 12:27 AM, Adam Hughes <[hidden email]> wrote:
> Is there any difference between using static methods and decorators in the
> following manner:
>
> def _a_items_changed(self, evt):
>    print 'heard'
>
>
> @on_trait_change(a.items)

This is actually spelled

@on_trait_change('a_items')

> def update(self):
>     print 'heard'
>
> It seems like the static listener method is also using the extended trait
> name syntax with underscores separating attribue levels rather than periods.
> Or is the "_items" call special to the List class?

List, Dict, and Set traits explicitly emit a notification with the
trait name 'a_items' when they are internally modified. This is a
single, non-extended trait name, just one that is not represented as
an attribute on the object; it only receives notifications. The
extended trait name 'a.items' would actually listen to changes on the
'items' trait of all of the objects in the 'a' List.

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


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Trait notifications not working on lists?

Fred
Le 10/08/2012 16:44, Adam Hughes a écrit :
> Thanks for the explanation.

Useful, as usual ;-)

>
> On Fri, Aug 10, 2012 at 4:54 AM, Robert Kern <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     On Fri, Aug 10, 2012 at 12:27 AM, Adam Hughes
>     <[hidden email] <mailto:[hidden email]>> wrote:
>      > Is there any difference between using static methods and
>     decorators in the
>      > following manner:
>      >
>      > def _a_items_changed(self, evt):
>      >    print 'heard'
>      >
>      >
>      > @on_trait_change(a.items)
>
>     This is actually spelled
>
>     @on_trait_change('a_items')
>
>      > def update(self):
>      >     print 'heard'
>      >
>      > It seems like the static listener method is also using the
>     extended trait
>      > name syntax with underscores separating attribue levels rather
>     than periods.
>      > Or is the "_items" call special to the List class?
>
>     List, Dict, and Set traits explicitly emit a notification with the
>     trait name 'a_items' when they are internally modified. This is a
>     single, non-extended trait name, just one that is not represented as
>     an attribute on the object; it only receives notifications. The
>     extended trait name 'a.items' would actually listen to changes on the
>     'items' trait of all of the objects in the 'a' List.
>
>     --
>     Robert Kern
>     Enthought
>     _______________________________________________
>     Enthought-Dev mailing list
>     [hidden email]
>     <mailto:[hidden email]>
>     https://mail.enthought.com/mailman/listinfo/enthought-dev
>
>
>
>
> _______________________________________________
> Enthought-Dev mailing list
> [hidden email]
> https://mail.enthought.com/mailman/listinfo/enthought-dev


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