##// END OF EJS Templates
Added on_events convinience method
Jonathan Frederic -
Show More
@@ -15,8 +15,8 b' class EventfulDict(dict):'
15 15 set `EventfulDict`s that wrap those `dict`s. Then you can wire the events
16 16 to the same handlers if necessary.
17 17
18 See the on_add, on_set, and on_del methods for registering an event
19 handler."""
18 See the on_events, on_add, on_set, and on_del methods for registering
19 event handlers."""
20 20
21 21 def __init__(self, *args, **kwargs):
22 22 """Public constructor"""
@@ -25,6 +25,20 b' class EventfulDict(dict):'
25 25 self._set_callback = _void
26 26 dict.__init__(self, *args, **kwargs)
27 27
28 def on_events(self, *callbacks):
29 """Register callbacks for add, set, and del actions.
30
31 See the doctstrings for on_(add/set/del) for details about each
32 callback.
33
34 add_callback: callback or None
35 set_callback: callback or None
36 del_callback: callback or None"""
37 registers = ['on_add', 'on_set', 'on_del']
38 if len(callbacks) < len(registers):
39 raise ValueError('on_events takes {} callbacks'.format(len(registers)))
40 [getattr(self, n)(callbacks[i]) for i, n in enumerate(registers)]
41
28 42 def on_add(self, callback):
29 43 """Register a callback for when an item is added to the dict.
30 44
@@ -141,6 +155,22 b' class EventfulList(list):'
141 155 self._reverse_callback = _void
142 156 list.__init__(self, *pargs, **kwargs)
143 157
158 def on_events(self, *callbacks):
159 """Register callbacks for add, set, and del actions.
160
161 See the doctstrings for on_(insert/set/del/reverse/sort) for details
162 about each callback.
163
164 insert_callback: callback or None
165 set_callback: callback or None
166 del_callback: callback or None
167 reverse_callback: callback or None
168 sort_callback: callback or None"""
169 registers = ['on_insert', 'on_set', 'on_del', 'on_reverse', 'on_sort']
170 if len(callbacks) < len(registers):
171 raise ValueError('on_events takes {} callbacks'.format(len(registers)))
172 [getattr(self, n)(callbacks[i]) for i, n in enumerate(registers)]
173
144 174 def on_insert(self, callback):
145 175 """Register a callback for when an item is inserted into the list.
146 176
@@ -1181,22 +1181,11 b' class TestEventful(TestCase):'
1181 1181 class A(HasTraits):
1182 1182 x = EventfulList([c for c in 'abc'])
1183 1183 a = A()
1184
1185 def handle_insert(index, value):
1186 event_cache.append('insert')
1187 def handle_del(index):
1188 event_cache.append('del')
1189 def handle_set(index, value):
1190 event_cache.append('set')
1191 def handle_reverse():
1192 event_cache.append('reverse')
1193 def handle_sort(*pargs, **kwargs):
1194 event_cache.append('sort')
1195 a.x.on_insert(handle_insert)
1196 a.x.on_del(handle_del)
1197 a.x.on_set(handle_set)
1198 a.x.on_reverse(handle_reverse)
1199 a.x.on_sort(handle_sort)
1184 a.x.on_events(lambda i, x: event_cache.append('insert'), \
1185 lambda i, x: event_cache.append('set'), \
1186 lambda i: event_cache.append('del'), \
1187 lambda: event_cache.append('reverse'), \
1188 lambda *p, **k: event_cache.append('sort'))
1200 1189
1201 1190 a.x.remove('c')
1202 1191 # ab
@@ -1226,16 +1215,9 b' class TestEventful(TestCase):'
1226 1215 class A(HasTraits):
1227 1216 x = EventfulDict({c: c for c in 'abc'})
1228 1217 a = A()
1229
1230 def handle_add(key, value):
1231 event_cache.append('add')
1232 def handle_del(key):
1233 event_cache.append('del')
1234 def handle_set(key, value):
1235 event_cache.append('set')
1236 a.x.on_add(handle_add)
1237 a.x.on_del(handle_del)
1238 a.x.on_set(handle_set)
1218 a.x.on_events(lambda k, v: event_cache.append('add'), \
1219 lambda k, v: event_cache.append('set'), \
1220 lambda k: event_cache.append('del'))
1239 1221
1240 1222 del a.x['c']
1241 1223 # ab
General Comments 0
You need to be logged in to leave comments. Login now