##// END OF EJS Templates
move eventful to traitlets
Min RK -
Show More
@@ -15,7 +15,7 b' class EventfulDict(dict):'
15 set EventfulDicts that wrap those dicts. Then you can wire the events
15 set EventfulDicts that wrap those dicts. Then you can wire the events
16 to the same handlers if necessary.
16 to the same handlers if necessary.
17
17
18 See the on_events, on_add, on_set, and on_del methods for registering
18 See the on_events, on_add, on_set, and on_del methods for registering
19 event handlers."""
19 event handlers."""
20
20
21 def __init__(self, *args, **kwargs):
21 def __init__(self, *args, **kwargs):
@@ -28,7 +28,7 b' class EventfulDict(dict):'
28 def on_events(self, add_callback=None, set_callback=None, del_callback=None):
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 = None]
34 add_callback: [callback = None]
@@ -63,7 +63,7 b' class EventfulDict(dict):'
63 callback should return a boolean True if the deletion should be
63 callback should return a boolean True if the deletion should be
64 canceled, False or None otherwise."""
64 canceled, False or None otherwise."""
65 self._del_callback = callback if callable(callback) else _void
65 self._del_callback = callback if callable(callback) else _void
66
66
67 def on_set(self, callback):
67 def on_set(self, callback):
68 """Register a callback for when an item is changed in the dict.
68 """Register a callback for when an item is changed in the dict.
69
69
@@ -76,7 +76,7 b' class EventfulDict(dict):'
76 callback should return a boolean True if the change should be
76 callback should return a boolean True if the change should be
77 canceled, False or None otherwise."""
77 canceled, False or None otherwise."""
78 self._set_callback = callback if callable(callback) else _void
78 self._set_callback = callback if callable(callback) else _void
79
79
80 def pop(self, key):
80 def pop(self, key):
81 """Returns the value of an item in the dictionary and then deletes the
81 """Returns the value of an item in the dictionary and then deletes the
82 item from the dictionary."""
82 item from the dictionary."""
@@ -95,7 +95,7 b' class EventfulDict(dict):'
95 overwriting any conflicting keys in this dictionary."""
95 overwriting any conflicting keys in this dictionary."""
96 for (key, value) in other_dict.items():
96 for (key, value) in other_dict.items():
97 self[key] = value
97 self[key] = value
98
98
99 def clear(self):
99 def clear(self):
100 """Clear the dictionary."""
100 """Clear the dictionary."""
101 for key in list(self.keys()):
101 for key in list(self.keys()):
@@ -105,7 +105,7 b' class EventfulDict(dict):'
105 if (key in self and self._can_set(key, value)) or \
105 if (key in self and self._can_set(key, value)) or \
106 (key not in self and self._can_add(key, value)):
106 (key not in self and self._can_add(key, value)):
107 return dict.__setitem__(self, key, value)
107 return dict.__setitem__(self, key, value)
108
108
109 def __delitem__(self, key):
109 def __delitem__(self, key):
110 if self._can_del(key):
110 if self._can_del(key):
111 return dict.__delitem__(self, key)
111 return dict.__delitem__(self, key)
@@ -126,8 +126,8 b' class EventfulDict(dict):'
126 class EventfulList(list):
126 class EventfulList(list):
127 """Eventful list.
127 """Eventful list.
128
128
129 This class inherits from the Python intrinsic `list` class. It adds events
129 This class inherits from the Python intrinsic `list` class. It adds events
130 that allow you to listen for actions that modify the list. You can
130 that allow you to listen for actions that modify the list. You can
131 optionally cancel the actions.
131 optionally cancel the actions.
132
132
133 See the on_del, on_set, on_insert, on_sort, and on_reverse methods for
133 See the on_del, on_set, on_insert, on_sort, and on_reverse methods for
@@ -145,11 +145,11 b' class EventfulList(list):'
145 self._reverse_callback = _void
145 self._reverse_callback = _void
146 list.__init__(self, *pargs, **kwargs)
146 list.__init__(self, *pargs, **kwargs)
147
147
148 def on_events(self, insert_callback=None, set_callback=None,
148 def on_events(self, insert_callback=None, set_callback=None,
149 del_callback=None, reverse_callback=None, sort_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 = None]
155 insert_callback: [callback = None]
@@ -175,7 +175,7 b' class EventfulList(list):'
175 callback should return a boolean True if the insertion should be
175 callback should return a boolean True if the insertion should be
176 canceled, False or None otherwise."""
176 canceled, False or None otherwise."""
177 self._insert_callback = callback if callable(callback) else _void
177 self._insert_callback = callback if callable(callback) else _void
178
178
179 def on_del(self, callback):
179 def on_del(self, callback):
180 """Register a callback for item deletion.
180 """Register a callback for item deletion.
181
181
@@ -188,11 +188,11 b' class EventfulList(list):'
188 callback should return a boolean True if the deletion should be
188 callback should return a boolean True if the deletion should be
189 canceled, False or None otherwise."""
189 canceled, False or None otherwise."""
190 self._del_callback = callback if callable(callback) else _void
190 self._del_callback = callback if callable(callback) else _void
191
191
192 def on_set(self, callback):
192 def on_set(self, callback):
193 """Register a callback for items are set.
193 """Register a callback for items are set.
194
194
195 Allows the listener to detect when items are set and optionally cancel
195 Allows the listener to detect when items are set and optionally cancel
196 the setting. Note, `set` is also called when one or more items are
196 the setting. Note, `set` is also called when one or more items are
197 added to the end of the list.
197 added to the end of the list.
198
198
@@ -202,7 +202,7 b' class EventfulList(list):'
202 callback should return a boolean True if the set should be
202 callback should return a boolean True if the set should be
203 canceled, False or None otherwise."""
203 canceled, False or None otherwise."""
204 self._set_callback = callback if callable(callback) else _void
204 self._set_callback = callback if callable(callback) else _void
205
205
206 def on_reverse(self, callback):
206 def on_reverse(self, callback):
207 """Register a callback for list reversal.
207 """Register a callback for list reversal.
208
208
@@ -212,18 +212,18 b' class EventfulList(list):'
212 callback should return a boolean True if the reverse should be
212 callback should return a boolean True if the reverse should be
213 canceled, False or None otherwise."""
213 canceled, False or None otherwise."""
214 self._reverse_callback = callback if callable(callback) else _void
214 self._reverse_callback = callback if callable(callback) else _void
215
215
216 def on_sort(self, callback):
216 def on_sort(self, callback):
217 """Register a callback for sortting of the list.
217 """Register a callback for sortting of the list.
218
218
219 callback: callable or None
219 callback: callable or None
220 If you want to ignore the sort event, pass None as the callback.
220 If you want to ignore the sort event, pass None as the callback.
221 The callback signature should match that of Python list's `.sort`
221 The callback signature should match that of Python list's `.sort`
222 method or `callback(*pargs, **kwargs)` as a catch all. The callback
222 method or `callback(*pargs, **kwargs)` as a catch all. The callback
223 should return a boolean True if the reverse should be canceled,
223 should return a boolean True if the reverse should be canceled,
224 False or None otherwise."""
224 False or None otherwise."""
225 self._sort_callback = callback if callable(callback) else _void
225 self._sort_callback = callback if callable(callback) else _void
226
226
227 def append(self, x):
227 def append(self, x):
228 """Add an item to the end of the list."""
228 """Add an item to the end of the list."""
229 self[len(self):] = [x]
229 self[len(self):] = [x]
@@ -233,13 +233,13 b' class EventfulList(list):'
233 self[len(self):] = L
233 self[len(self):] = L
234
234
235 def remove(self, x):
235 def remove(self, x):
236 """Remove the first item from the list whose value is x. It is an error
236 """Remove the first item from the list whose value is x. It is an error
237 if there is no such item."""
237 if there is no such item."""
238 del self[self.index(x)]
238 del self[self.index(x)]
239
239
240 def pop(self, i=None):
240 def pop(self, i=None):
241 """Remove the item at the given position in the list, and return it. If
241 """Remove the item at the given position in the list, and return it. If
242 no index is specified, a.pop() removes and returns the last item in the
242 no index is specified, a.pop() removes and returns the last item in the
243 list."""
243 list."""
244 if i is None:
244 if i is None:
245 i = len(self) - 1
245 i = len(self) - 1
@@ -251,17 +251,17 b' class EventfulList(list):'
251 """Reverse the elements of the list, in place."""
251 """Reverse the elements of the list, in place."""
252 if self._can_reverse():
252 if self._can_reverse():
253 list.reverse(self)
253 list.reverse(self)
254
254
255 def insert(self, index, value):
255 def insert(self, index, value):
256 """Insert an item at a given position. The first argument is the index
256 """Insert an item at a given position. The first argument is the index
257 of the element before which to insert, so a.insert(0, x) inserts at the
257 of the element before which to insert, so a.insert(0, x) inserts at the
258 front of the list, and a.insert(len(a), x) is equivalent to
258 front of the list, and a.insert(len(a), x) is equivalent to
259 a.append(x)."""
259 a.append(x)."""
260 if self._can_insert(index, value):
260 if self._can_insert(index, value):
261 list.insert(self, index, value)
261 list.insert(self, index, value)
262
262
263 def sort(self, *pargs, **kwargs):
263 def sort(self, *pargs, **kwargs):
264 """Sort the items of the list in place (the arguments can be used for
264 """Sort the items of the list in place (the arguments can be used for
265 sort customization, see Python's sorted() for their explanation)."""
265 sort customization, see Python's sorted() for their explanation)."""
266 if self._can_sort(*pargs, **kwargs):
266 if self._can_sort(*pargs, **kwargs):
267 list.sort(self, *pargs, **kwargs)
267 list.sort(self, *pargs, **kwargs)
@@ -277,7 +277,7 b' class EventfulList(list):'
277 def __setslice__(self, start, end, value):
277 def __setslice__(self, start, end, value):
278 if self._can_set(slice(start, end), value):
278 if self._can_set(slice(start, end), value):
279 list.__setslice__(self, start, end, value)
279 list.__setslice__(self, start, end, value)
280
280
281 def _can_insert(self, index, value):
281 def _can_insert(self, index, value):
282 """Check if the item can be inserted."""
282 """Check if the item can be inserted."""
283 return not bool(self._insert_callback(index, value))
283 return not bool(self._insert_callback(index, value))
@@ -54,11 +54,11 b' except:'
54 from warnings import warn
54 from warnings import warn
55
55
56 from IPython.utils import py3compat
56 from IPython.utils import py3compat
57 from IPython.utils import eventful
58 from IPython.utils.getargspec import getargspec
57 from IPython.utils.getargspec import getargspec
59 from IPython.utils.importstring import import_item
58 from IPython.utils.importstring import import_item
60 from IPython.utils.py3compat import iteritems, string_types
59 from IPython.utils.py3compat import iteritems, string_types
61
60
61 from . import eventful
62 from .sentinel import Sentinel
62 from .sentinel import Sentinel
63 SequenceTypes = (list, tuple, set, frozenset)
63 SequenceTypes = (list, tuple, set, frozenset)
64
64
General Comments 0
You need to be logged in to leave comments. Login now