##// END OF EJS Templates
Address review comments @takluyver
Jonathan Frederic -
Show More
@@ -1,5 +1,8 b''
1 """Contains eventful dict and list implementations."""
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 class EventfulDict(dict):
6 class EventfulDict(dict):
4 """Eventful dictionary.
7 """Eventful dictionary.
5
8
@@ -17,9 +20,9 b' class EventfulDict(dict):'
17
20
18 def __init__(self, *args, **kwargs):
21 def __init__(self, *args, **kwargs):
19 """Public constructor"""
22 """Public constructor"""
20 self._add_callback = None
23 self._add_callback = _void
21 self._del_callback = None
24 self._del_callback = _void
22 self._set_callback = None
25 self._set_callback = _void
23 dict.__init__(self, *args, **kwargs)
26 dict.__init__(self, *args, **kwargs)
24
27
25 def on_add(self, callback):
28 def on_add(self, callback):
@@ -33,7 +36,10 b' class EventfulDict(dict):'
33 The callback should have a signature of callback(key, value). The
36 The callback should have a signature of callback(key, value). The
34 callback should return a boolean True if the additon should be
37 callback should return a boolean True if the additon should be
35 canceled, False or None otherwise."""
38 canceled, False or None otherwise."""
39 if callable(callback):
36 self._add_callback = callback
40 self._add_callback = callback
41 else:
42 self._add_callback = _void
37
43
38 def on_del(self, callback):
44 def on_del(self, callback):
39 """Register a callback for when an item is deleted from the dict.
45 """Register a callback for when an item is deleted from the dict.
@@ -46,7 +52,10 b' class EventfulDict(dict):'
46 The callback should have a signature of callback(key). The
52 The callback should have a signature of callback(key). The
47 callback should return a boolean True if the deletion should be
53 callback should return a boolean True if the deletion should be
48 canceled, False or None otherwise."""
54 canceled, False or None otherwise."""
55 if callable(callback):
49 self._del_callback = callback
56 self._del_callback = callback
57 else:
58 self._del_callback = _void
50
59
51 def on_set(self, callback):
60 def on_set(self, callback):
52 """Register a callback for when an item is changed in the dict.
61 """Register a callback for when an item is changed in the dict.
@@ -59,28 +68,10 b' class EventfulDict(dict):'
59 The callback should have a signature of callback(key, value). The
68 The callback should have a signature of callback(key, value). The
60 callback should return a boolean True if the change should be
69 callback should return a boolean True if the change should be
61 canceled, False or None otherwise."""
70 canceled, False or None otherwise."""
71 if callable(callback):
62 self._set_callback = callback
72 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))
82 else:
73 else:
83 return True
74 self._set_callback = _void
84
75
85 def pop(self, key):
76 def pop(self, key):
86 """Returns the value of an item in the dictionary and then deletes the
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 def popitem(self):
84 def popitem(self):
94 """Pop the next key/value pair from the dictionary."""
85 """Pop the next key/value pair from the dictionary."""
95 key = dict.keys(self)[0]
86 key = next(iter(self))
96 return key, self.pop(key)
87 return key, self.pop(key)
97
88
98 def update(self, other_dict):
89 def update(self, other_dict):
@@ -115,6 +106,18 b' class EventfulDict(dict):'
115 if self._can_del(key):
106 if self._can_del(key):
116 return dict.__delitem__(self, key)
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 class EventfulList(list):
122 class EventfulList(list):
120 """Eventful list.
123 """Eventful list.
@@ -131,11 +134,11 b' class EventfulList(list):'
131
134
132 def __init__(self, *pargs, **kwargs):
135 def __init__(self, *pargs, **kwargs):
133 """Public constructor"""
136 """Public constructor"""
134 self._insert_callback = None
137 self._insert_callback = _void
135 self._set_callback = None
138 self._set_callback = _void
136 self._del_callback = None
139 self._del_callback = _void
137 self._sort_callback = None
140 self._sort_callback = _void
138 self._reverse_callback = None
141 self._reverse_callback = _void
139 list.__init__(self, *pargs, **kwargs)
142 list.__init__(self, *pargs, **kwargs)
140
143
141 def on_insert(self, callback):
144 def on_insert(self, callback):
@@ -149,7 +152,10 b' class EventfulList(list):'
149 The callback should have a signature of callback(index, value). The
152 The callback should have a signature of callback(index, value). The
150 callback should return a boolean True if the insertion should be
153 callback should return a boolean True if the insertion should be
151 canceled, False or None otherwise."""
154 canceled, False or None otherwise."""
155 if callable(callback):
152 self._insert_callback = callback
156 self._insert_callback = callback
157 else:
158 self._insert_callback = _void
153
159
154 def on_del(self, callback):
160 def on_del(self, callback):
155 """Register a callback for item deletion.
161 """Register a callback for item deletion.
@@ -162,7 +168,10 b' class EventfulList(list):'
162 The callback should have a signature of callback(index). The
168 The callback should have a signature of callback(index). The
163 callback should return a boolean True if the deletion should be
169 callback should return a boolean True if the deletion should be
164 canceled, False or None otherwise."""
170 canceled, False or None otherwise."""
171 if callable(callback):
165 self._del_callback = callback
172 self._del_callback = callback
173 else:
174 self._del_callback = _void
166
175
167 def on_set(self, callback):
176 def on_set(self, callback):
168 """Register a callback for items are set.
177 """Register a callback for items are set.
@@ -176,7 +185,10 b' class EventfulList(list):'
176 The callback should have a signature of callback(index, value). The
185 The callback should have a signature of callback(index, value). The
177 callback should return a boolean True if the set should be
186 callback should return a boolean True if the set should be
178 canceled, False or None otherwise."""
187 canceled, False or None otherwise."""
188 if callable(callback):
179 self._set_callback = callback
189 self._set_callback = callback
190 else:
191 self._set_callback = _void
180
192
181 def on_reverse(self, callback):
193 def on_reverse(self, callback):
182 """Register a callback for list reversal.
194 """Register a callback for list reversal.
@@ -186,7 +198,10 b' class EventfulList(list):'
186 The callback should have a signature of callback(). The
198 The callback should have a signature of callback(). The
187 callback should return a boolean True if the reverse should be
199 callback should return a boolean True if the reverse should be
188 canceled, False or None otherwise."""
200 canceled, False or None otherwise."""
201 if callable(callback):
189 self._reverse_callback = callback
202 self._reverse_callback = callback
203 else:
204 self._reverse_callback = _void
190
205
191 def on_sort(self, callback):
206 def on_sort(self, callback):
192 """Register a callback for sortting of the list.
207 """Register a callback for sortting of the list.
@@ -197,42 +212,10 b' class EventfulList(list):'
197 method or `callback(*pargs, **kwargs)` as a catch all. The callback
212 method or `callback(*pargs, **kwargs)` as a catch all. The callback
198 should return a boolean True if the reverse should be canceled,
213 should return a boolean True if the reverse should be canceled,
199 False or None otherwise."""
214 False or None otherwise."""
215 if callable(callback):
200 self._sort_callback = callback
216 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))
206 else:
217 else:
207 return True
218 self._sort_callback = _void
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
236
219
237 def append(self, x):
220 def append(self, x):
238 """Add an item to the end of the list."""
221 """Add an item to the end of the list."""
@@ -287,3 +270,23 b' class EventfulList(list):'
287 def __setslice__(self, start, end, value):
270 def __setslice__(self, start, end, value):
288 if self._can_set(slice(start, end), value):
271 if self._can_set(slice(start, end), value):
289 list.__setslice__(self, start, end, value)
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