Package advene :: Package util :: Module reftools
[hide private]
[frames] | no frames]

Source Code for Module advene.util.reftools

 1  """I provide utility functions and class related to weak references. 
 2  """ 
 3   
 4  from weakref import WeakValueDictionary 
 5   
6 -class WeakValueDictWithCallback(WeakValueDictionary):
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 """
15 - def __init__(self, callback):
16 WeakValueDictionary.__init__ (self) 17 # The superclass WeakValueDictionary assigns self._remove as a 18 # callback to all the KeyedRef it creates. So we have to override 19 # self._remove. 20 # Note however that self._remobe is *not* a method because, as a 21 # callback, it can be invoked after the dictionary is collected. 22 # So it is a plain function, stored as an *instance* attribute. 23 def remove(wr, _callback=callback, _original=self._remove): 24 _original(wr) 25 _callback(wr.key)
26 self._remove = remove
27