1 """I provide utility functions and class related to weak references.
2 """
3
4 from weakref import WeakValueDictionary
5
7 """I extend WeakValueDictionary with a configurable callback function.
8
9 The callback function must be given *before* any value is assigned,
10 so WeakValueDictWithCallback does not accept a dict nor keyword arguments
11 as __init__ parameters.
12 The callback function will be invoked after a key is removed, with that key
13 as its only argument.
14 """
16 WeakValueDictionary.__init__ (self)
17
18
19
20
21
22
23 def remove(wr, _callback=callback, _original=self._remove):
24 _original(wr)
25 _callback(wr.key)
26 self._remove = remove
27