1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """abstract data types with built-in notification support
23 """
24
25 __version__ = "$Rev: 6964 $"
26
28 class Watched(type):
29 def __init__(self):
30 type.__init__(self)
31 self.watch_id = 0
32 self.watch_procs = {}
33
34 def watch(self, proc):
35 self.watch_id += 1
36 self.watch_procs[self.watch_id] = proc
37 return self.watch_id
38
39 def unwatch(self, id):
40 del self.watch_procs[id]
41
42 def notify_changed(self):
43 for proc in self.watch_procs.values():
44 proc(self)
45
46 def mutate(method):
47 def do_mutate(self, *args, **kwargs):
48 method(self, *args, **kwargs)
49 self.notify_changed()
50 setattr(Watched, method.__name__, do_mutate)
51 for i in mutators:
52 mutate(getattr(type, i))
53
54 return Watched
55
56 WatchedList = _make_watched(list, 'append', 'insert', 'remove', 'pop',
57 'sort', 'reverse')
58 WatchedDict = _make_watched(dict, '__setitem__', '__delitem__', 'pop',
59 'popitem', 'update')
60