##// END OF EJS Templates
kwarg on_events and hardcoded callbacks
Jonathan Frederic -
Show More
@@ -25,19 +25,18 b' class EventfulDict(dict):'
25 self._set_callback = _void
25 self._set_callback = _void
26 dict.__init__(self, *args, **kwargs)
26 dict.__init__(self, *args, **kwargs)
27
27
28 def on_events(self, *callbacks):
28 def on_events(self, add_callback=None, set_callback=None, del_callback=None):
29 """Register callbacks for add, set, and del actions.
29 """Register callbacks for add, set, and del actions.
30
30
31 See the doctstrings for on_(add/set/del) for details about each
31 See the doctstrings for on_(add/set/del) for details about each
32 callback.
32 callback.
33
33
34 add_callback: callback or None
34 add_callback: [callback = None]
35 set_callback: callback or None
35 set_callback: [callback = None]
36 del_callback: callback or None"""
36 del_callback: [callback = None]"""
37 registers = ['on_add', 'on_set', 'on_del']
37 self.on_add(add_callback)
38 if len(callbacks) < len(registers):
38 self.on_set(set_callback)
39 raise ValueError('on_events takes {} callbacks'.format(len(registers)))
39 self.on_del(del_callback)
40 [getattr(self, n)(callbacks[i]) for i, n in enumerate(registers)]
41
40
42 def on_add(self, callback):
41 def on_add(self, callback):
43 """Register a callback for when an item is added to the dict.
42 """Register a callback for when an item is added to the dict.
@@ -146,21 +145,23 b' class EventfulList(list):'
146 self._reverse_callback = _void
145 self._reverse_callback = _void
147 list.__init__(self, *pargs, **kwargs)
146 list.__init__(self, *pargs, **kwargs)
148
147
149 def on_events(self, *callbacks):
148 def on_events(self, insert_callback=None, set_callback=None,
149 del_callback=None, reverse_callback=None, sort_callback=None):
150 """Register callbacks for add, set, and del actions.
150 """Register callbacks for add, set, and del actions.
151
151
152 See the doctstrings for on_(insert/set/del/reverse/sort) for details
152 See the doctstrings for on_(insert/set/del/reverse/sort) for details
153 about each callback.
153 about each callback.
154
154
155 insert_callback: callback or None
155 insert_callback: [callback = None]
156 set_callback: callback or None
156 set_callback: [callback = None]
157 del_callback: callback or None
157 del_callback: [callback = None]
158 reverse_callback: callback or None
158 reverse_callback: [callback = None]
159 sort_callback: callback or None"""
159 sort_callback: [callback = None]"""
160 registers = ['on_insert', 'on_set', 'on_del', 'on_reverse', 'on_sort']
160 this.on_insert(insert_callback)
161 if len(callbacks) < len(registers):
161 this.on_set(set_callback)
162 raise ValueError('on_events takes {} callbacks'.format(len(registers)))
162 this.on_del(del_callback)
163 [getattr(self, n)(callbacks[i]) for i, n in enumerate(registers)]
163 this.on_reverse(reverse_callback)
164 this.on_sort(sort_callback)
164
165
165 def on_insert(self, callback):
166 def on_insert(self, callback):
166 """Register a callback for when an item is inserted into the list.
167 """Register a callback for when an item is inserted into the list.
General Comments 0
You need to be logged in to leave comments. Login now