Skip to content

Commit

Permalink
add tag observing to HasTraits.observe
Browse files Browse the repository at this point in the history
  • Loading branch information
rmorshea committed Oct 21, 2015
1 parent d826e08 commit 1ae7d72
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions traitlets/traitlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,7 @@ def on_trait_change(self, handler=None, name=None, remove=False):
else:
self.observe(_callback_wrapper(handler), names=name)

def observe(self, handler, names=All, type='change'):
def observe(self, handler, names=All, tags=None, type='change'):
"""Setup a handler to be called when a trait changes.
This is used to setup dynamic notifications of trait changes.
Expand All @@ -1026,15 +1026,20 @@ def observe(self, handler, names=All, type='change'):
If names is All, the handler will apply to all traits. If a list
of str, handler will apply to all names in the list. If a
str, the handler will apply just to that name.
tags: dict
Applies the handler to all traits whose metadata matches the given
tags or selector functions (`HasTraits.traits` for selector info)
type : str, All (default: 'change')
The type of notification to filter by. If equal to All, then all
notifications are passed to the observe handler.
"""
names = parse_notifier_name(names)
if tags:
names.extend(self.trait_names(**tags))
for n in names:
self._add_notifiers(handler, n, type)

def unobserve(self, handler, names=All, type='change'):
def unobserve(self, handler, names=All, tags=None, type='change'):
"""Remove a trait change handler.
This is used to unregister handlers to trait change notificiations.
Expand All @@ -1047,11 +1052,16 @@ def unobserve(self, handler, names=All, type='change'):
The names of the traits for which the specified handler should be
uninstalled. If names is All, the specified handler is uninstalled
from the list of notifiers corresponding to all changes.
tags: dict
Removes the handler from all traits whose metadata matches the given
tags or selector functions (`HasTraits.traits` for selector info)
type : str or All (default: 'change')
The type of notification to filter by. If All, the specified handler
is uninstalled from the list of notifiers corresponding to all types.
"""
names = parse_notifier_name(names)
if tags:
names.extend(self.trait_names(**tags))
for n in names:
self._remove_notifiers(handler, n, type)

Expand Down

0 comments on commit 1ae7d72

Please sign in to comment.