From d674ae2e050b4a08da4158a1a512fe0abd305a78 2014-08-04 21:22:16 From: Jonathan Frederic Date: 2014-08-04 21:22:16 Subject: [PATCH] Added on_events convinience method --- diff --git a/IPython/utils/eventful.py b/IPython/utils/eventful.py index 332915a..a986560 100644 --- a/IPython/utils/eventful.py +++ b/IPython/utils/eventful.py @@ -15,8 +15,8 @@ class EventfulDict(dict): set `EventfulDict`s that wrap those `dict`s. Then you can wire the events to the same handlers if necessary. - See the on_add, on_set, and on_del methods for registering an event - handler.""" + See the on_events, on_add, on_set, and on_del methods for registering + event handlers.""" def __init__(self, *args, **kwargs): """Public constructor""" @@ -25,6 +25,20 @@ class EventfulDict(dict): self._set_callback = _void dict.__init__(self, *args, **kwargs) + def on_events(self, *callbacks): + """Register callbacks for add, set, and del actions. + + See the doctstrings for on_(add/set/del) for details about each + callback. + + add_callback: callback or None + set_callback: callback or None + del_callback: callback or None""" + registers = ['on_add', 'on_set', 'on_del'] + if len(callbacks) < len(registers): + raise ValueError('on_events takes {} callbacks'.format(len(registers))) + [getattr(self, n)(callbacks[i]) for i, n in enumerate(registers)] + def on_add(self, callback): """Register a callback for when an item is added to the dict. @@ -141,6 +155,22 @@ class EventfulList(list): self._reverse_callback = _void list.__init__(self, *pargs, **kwargs) + def on_events(self, *callbacks): + """Register callbacks for add, set, and del actions. + + See the doctstrings for on_(insert/set/del/reverse/sort) for details + about each callback. + + insert_callback: callback or None + set_callback: callback or None + del_callback: callback or None + reverse_callback: callback or None + sort_callback: callback or None""" + registers = ['on_insert', 'on_set', 'on_del', 'on_reverse', 'on_sort'] + if len(callbacks) < len(registers): + raise ValueError('on_events takes {} callbacks'.format(len(registers))) + [getattr(self, n)(callbacks[i]) for i, n in enumerate(registers)] + def on_insert(self, callback): """Register a callback for when an item is inserted into the list. diff --git a/IPython/utils/tests/test_traitlets.py b/IPython/utils/tests/test_traitlets.py index 760e22e..4acd3ec 100644 --- a/IPython/utils/tests/test_traitlets.py +++ b/IPython/utils/tests/test_traitlets.py @@ -1181,22 +1181,11 @@ class TestEventful(TestCase): class A(HasTraits): x = EventfulList([c for c in 'abc']) a = A() - - def handle_insert(index, value): - event_cache.append('insert') - def handle_del(index): - event_cache.append('del') - def handle_set(index, value): - event_cache.append('set') - def handle_reverse(): - event_cache.append('reverse') - def handle_sort(*pargs, **kwargs): - event_cache.append('sort') - a.x.on_insert(handle_insert) - a.x.on_del(handle_del) - a.x.on_set(handle_set) - a.x.on_reverse(handle_reverse) - a.x.on_sort(handle_sort) + a.x.on_events(lambda i, x: event_cache.append('insert'), \ + lambda i, x: event_cache.append('set'), \ + lambda i: event_cache.append('del'), \ + lambda: event_cache.append('reverse'), \ + lambda *p, **k: event_cache.append('sort')) a.x.remove('c') # ab @@ -1226,16 +1215,9 @@ class TestEventful(TestCase): class A(HasTraits): x = EventfulDict({c: c for c in 'abc'}) a = A() - - def handle_add(key, value): - event_cache.append('add') - def handle_del(key): - event_cache.append('del') - def handle_set(key, value): - event_cache.append('set') - a.x.on_add(handle_add) - a.x.on_del(handle_del) - a.x.on_set(handle_set) + a.x.on_events(lambda k, v: event_cache.append('add'), \ + lambda k, v: event_cache.append('set'), \ + lambda k: event_cache.append('del')) del a.x['c'] # ab