Change of dictionary item's value.

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

Change of dictionary item's value.

Jordan Ilott
Is it possible to listen for a change to the value of a dictionary item? I know I can listen for a change to any of items, but I would only like to react to specific the value associated with specific keys being changed.

Something along the lines of the following:

data = Dict()

data['key1']='value 1'
data['key2']='value 2'

on_trait_change(some_func, appropriate string listen to changes to 'key1' only)\

so that some_func is called in the following case:

data['key1'] = 'new value 1'

but not for:

data['key2'] = 'new value 2'

Thanks.

Jordan

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

Re: Change of dictionary item's value.

Robert Kern
On Fri, Apr 19, 2013 at 10:34 PM, Jordan Ilott <[hidden email]> wrote:

> Is it possible to listen for a change to the value of a dictionary item? I
> know I can listen for a change to any of items, but I would only like to
> react to specific the value associated with specific keys being changed.
>
> Something along the lines of the following:
>
> data = Dict()
>
> data['key1']='value 1'
> data['key2']='value 2'
>
> on_trait_change(some_func, appropriate string listen to changes to 'key1'
> only)\
>
> so that some_func is called in the following case:
>
> data['key1'] = 'new value 1'
>
> but not for:
>
> data['key2'] = 'new value 2'

The handler will always be called for every key; there is no way to
specify a filter at that level. The object that the handler gets,
though, holds the information about what key was changed, and you can
conditionally do different things based on that.

https://github.com/enthought/traits/blob/master/traits/trait_handlers.py#L2945


class Model(HasTraits):
    data = Dict()

    def _data_items_changed(self, event):
        keys_changed = set(event.added) | set(event.removed) |
set(event.changed)
        if 'key1' in keys_changed:
            ...

--
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: Change of dictionary item's value.

Jordan Ilott-2
Thanks Robert. I suspected this might be the case, but I thought I might be missing something.

Jordan

On Fri, Apr 19, 2013 at 12:16 PM, Robert Kern <[hidden email]> wrote:
On Fri, Apr 19, 2013 at 10:34 PM, Jordan Ilott <[hidden email]> wrote:
> Is it possible to listen for a change to the value of a dictionary item? I
> know I can listen for a change to any of items, but I would only like to
> react to specific the value associated with specific keys being changed.
>
> Something along the lines of the following:
>
> data = Dict()
>
> data['key1']='value 1'
> data['key2']='value 2'
>
> on_trait_change(some_func, appropriate string listen to changes to 'key1'
> only)\
>
> so that some_func is called in the following case:
>
> data['key1'] = 'new value 1'
>
> but not for:
>
> data['key2'] = 'new value 2'

The handler will always be called for every key; there is no way to
specify a filter at that level. The object that the handler gets,
though, holds the information about what key was changed, and you can
conditionally do different things based on that.

https://github.com/enthought/traits/blob/master/traits/trait_handlers.py#L2945


class Model(HasTraits):
    data = Dict()

    def _data_items_changed(self, event):
        keys_changed = set(event.added) | set(event.removed) |
set(event.changed)
        if 'key1' in keys_changed:
            ...

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