##// END OF EJS Templates
Address review comments @takluyver
Jonathan Frederic -
Show More
@@ -1,5 +1,8 b''
1 1 """Contains eventful dict and list implementations."""
2 2
3 # void function used as a callback placeholder.
4 def _void(*p, **k): return None
5
3 6 class EventfulDict(dict):
4 7 """Eventful dictionary.
5 8
@@ -17,9 +20,9 b' class EventfulDict(dict):'
17 20
18 21 def __init__(self, *args, **kwargs):
19 22 """Public constructor"""
20 self._add_callback = None
21 self._del_callback = None
22 self._set_callback = None
23 self._add_callback = _void
24 self._del_callback = _void
25 self._set_callback = _void
23 26 dict.__init__(self, *args, **kwargs)
24 27
25 28 def on_add(self, callback):
@@ -33,7 +36,10 b' class EventfulDict(dict):'
33 36 The callback should have a signature of callback(key, value). The
34 37 callback should return a boolean True if the additon should be
35 38 canceled, False or None otherwise."""
36 self._add_callback = callback
39 if callable(callback):
40 self._add_callback = callback
41 else:
42 self._add_callback = _void
37 43
38 44 def on_del(self, callback):
39 45 """Register a callback for when an item is deleted from the dict.
@@ -46,7 +52,10 b' class EventfulDict(dict):'
46 52 The callback should have a signature of callback(key). The
47 53 callback should return a boolean True if the deletion should be
48 54 canceled, False or None otherwise."""
49 self._del_callback = callback
55 if callable(callback):
56 self._del_callback = callback
57 else:
58 self._del_callback = _void
50 59
51 60 def on_set(self, callback):
52 61 """Register a callback for when an item is changed in the dict.
@@ -59,28 +68,10 b' class EventfulDict(dict):'
59 68 The callback should have a signature of callback(key, value). The
60 69 callback should return a boolean True if the change should be
61 70 canceled, False or None otherwise."""
62 self._set_callback = callback
63
64 def _can_add(self, key, value):
65 """Check if the item can be added to the dict."""
66 if callable(self._add_callback):
67 return not bool(self._add_callback(key, value))
68 else:
69 return True
70
71 def _can_del(self, key):
72 """Check if the item can be deleted from the dict."""
73 if callable(self._del_callback):
74 return not bool(self._del_callback(key))
75 else:
76 return True
77
78 def _can_set(self, key, value):
79 """Check if the item can be changed in the dict."""
80 if callable(self._set_callback):
81 return not bool(self._set_callback(key, value))
71 if callable(callback):
72 self._set_callback = callback
82 73 else:
83 return True
74 self._set_callback = _void
84 75
85 76 def pop(self, key):
86 77 """Returns the value of an item in the dictionary and then deletes the
@@ -92,7 +83,7 b' class EventfulDict(dict):'
92 83
93 84 def popitem(self):
94 85 """Pop the next key/value pair from the dictionary."""
95 key = dict.keys(self)[0]
86 key = next(iter(self))
96 87 return key, self.pop(key)
97 88
98 89 def update(self, other_dict):
@@ -115,6 +106,18 b' class EventfulDict(dict):'
115 106 if self._can_del(key):
116 107 return dict.__delitem__(self, key)
117 108
109 def _can_add(self, key, value):
110 """Check if the item can be added to the dict."""
111 return not bool(self._add_callback(key, value))
112
113 def _can_del(self, key):
114 """Check if the item can be deleted from the dict."""
115 return not bool(self._del_callback(key))
116
117 def _can_set(self, key, value):
118 """Check if the item can be changed in the dict."""
119 return not bool(self._set_callback(key, value))
120
118 121
119 122 class EventfulList(list):
120 123 """Eventful list.
@@ -131,11 +134,11 b' class EventfulList(list):'
131 134
132 135 def __init__(self, *pargs, **kwargs):
133 136 """Public constructor"""
134 self._insert_callback = None
135 self._set_callback = None
136 self._del_callback = None
137 self._sort_callback = None
138 self._reverse_callback = None
137 self._insert_callback = _void
138 self._set_callback = _void
139 self._del_callback = _void
140 self._sort_callback = _void
141 self._reverse_callback = _void
139 142 list.__init__(self, *pargs, **kwargs)
140 143
141 144 def on_insert(self, callback):
@@ -149,7 +152,10 b' class EventfulList(list):'
149 152 The callback should have a signature of callback(index, value). The
150 153 callback should return a boolean True if the insertion should be
151 154 canceled, False or None otherwise."""
152 self._insert_callback = callback
155 if callable(callback):
156 self._insert_callback = callback
157 else:
158 self._insert_callback = _void
153 159
154 160 def on_del(self, callback):
155 161 """Register a callback for item deletion.
@@ -162,7 +168,10 b' class EventfulList(list):'
162 168 The callback should have a signature of callback(index). The
163 169 callback should return a boolean True if the deletion should be
164 170 canceled, False or None otherwise."""
165 self._del_callback = callback
171 if callable(callback):
172 self._del_callback = callback
173 else:
174 self._del_callback = _void
166 175
167 176 def on_set(self, callback):
168 177 """Register a callback for items are set.
@@ -176,7 +185,10 b' class EventfulList(list):'
176 185 The callback should have a signature of callback(index, value). The
177 186 callback should return a boolean True if the set should be
178 187 canceled, False or None otherwise."""
179 self._set_callback = callback
188 if callable(callback):
189 self._set_callback = callback
190 else:
191 self._set_callback = _void
180 192
181 193 def on_reverse(self, callback):
182 194 """Register a callback for list reversal.
@@ -186,7 +198,10 b' class EventfulList(list):'
186 198 The callback should have a signature of callback(). The
187 199 callback should return a boolean True if the reverse should be
188 200 canceled, False or None otherwise."""
189 self._reverse_callback = callback
201 if callable(callback):
202 self._reverse_callback = callback
203 else:
204 self._reverse_callback = _void
190 205
191 206 def on_sort(self, callback):
192 207 """Register a callback for sortting of the list.
@@ -197,42 +212,10 b' class EventfulList(list):'
197 212 method or `callback(*pargs, **kwargs)` as a catch all. The callback
198 213 should return a boolean True if the reverse should be canceled,
199 214 False or None otherwise."""
200 self._sort_callback = callback
201
202 def _can_insert(self, index, value):
203 """Check if the item can be inserted."""
204 if callable(self._insert_callback):
205 return not bool(self._insert_callback(index, value))
215 if callable(callback):
216 self._sort_callback = callback
206 217 else:
207 return True
208
209 def _can_del(self, index):
210 """Check if the item can be deleted."""
211 if callable(self._del_callback):
212 return not bool(self._del_callback(index))
213 else:
214 return True
215
216 def _can_set(self, index, value):
217 """Check if the item can be set."""
218 if callable(self._set_callback):
219 return not bool(self._set_callback(index, value))
220 else:
221 return True
222
223 def _can_reverse(self):
224 """Check if the list can be reversed."""
225 if callable(self._reverse_callback):
226 return not bool(self._reverse_callback())
227 else:
228 return True
229
230 def _can_sort(self, *pargs, **kwargs):
231 """Check if the list can be sorted."""
232 if callable(self._sort_callback):
233 return not bool(self._sort_callback(*pargs, **kwargs))
234 else:
235 return True
218 self._sort_callback = _void
236 219
237 220 def append(self, x):
238 221 """Add an item to the end of the list."""
@@ -287,3 +270,23 b' class EventfulList(list):'
287 270 def __setslice__(self, start, end, value):
288 271 if self._can_set(slice(start, end), value):
289 272 list.__setslice__(self, start, end, value)
273
274 def _can_insert(self, index, value):
275 """Check if the item can be inserted."""
276 return not bool(self._insert_callback(index, value))
277
278 def _can_del(self, index):
279 """Check if the item can be deleted."""
280 return not bool(self._del_callback(index))
281
282 def _can_set(self, index, value):
283 """Check if the item can be set."""
284 return not bool(self._set_callback(index, value))
285
286 def _can_reverse(self):
287 """Check if the list can be reversed."""
288 return not bool(self._reverse_callback())
289
290 def _can_sort(self, *pargs, **kwargs):
291 """Check if the list can be sorted."""
292 return not bool(self._sort_callback(*pargs, **kwargs))
General Comments 0
You need to be logged in to leave comments. Login now