director.callbacks module

class director.callbacks.BoundMethodProxy(cb)[source]

Bases: object

Our own proxy object which enables weak references to bound and unbound methods and arbitrary callables. Pulls information about the function, class, and instance out of a bound method. Stores a weak reference to the instance to support garbage collection.

@organization: IBM Corporation @copyright: Copyright (c) 2005, 2006 IBM Corporation @license: The BSD License

Minor bugfixes by Michael Droettboom

class director.callbacks.CallbackRegistry(signals)[source]

Handle registering and disconnecting for a set of signals and callbacks:

signals = 'eat', 'drink', 'be merry'

def oneat(x):
    print 'eat', x

def ondrink(x):
    print 'drink', x

callbacks = CallbackRegistry(signals)

ideat = callbacks.connect('eat', oneat)
iddrink = callbacks.connect('drink', ondrink)

#tmp = callbacks.connect('drunk', ondrink) # this will raise a ValueError

callbacks.process('drink', 123)    # will call oneat
callbacks.process('eat', 456)      # will call ondrink
callbacks.process('be merry', 456) # nothing will be called
callbacks.disconnect(ideat)        # disconnect oneat
callbacks.process('eat', 456)      # nothing will be called

In practice, one should always disconnect all callbacks when they are no longer needed to avoid dangling references (and thus memory leaks). However, real code in matplotlib rarely does so, and due to its design, it is rather difficult to place this kind of code. To get around this, and prevent this class of memory leaks, we instead store weak references to bound methods only, so when the destination object needs to die, the CallbackRegistry won’t keep it alive. The Python stdlib weakref module can not create weak references to bound methods directly, so we need to create a proxy object to handle weak references to bound methods (or regular free functions). This technique was shared by Peter Parente on his “Mindtrove” blog.

addSignal(sig)[source]
connect(s, func)[source]

register func to be called when a signal s is generated func will be called

disconnect(cid)[source]

disconnect the callback registered with callback id cid

getCallbacks(s)[source]

return callbacks registered to signal s.

process(s, *args, **kwargs)[source]

process signal s. All of the functions registered to receive callbacks on s will be called with *args and **kwargs