Exscript.util.event module

A simple signal/event mechanism.

class Exscript.util.event.Event[source]

Bases: future.types.newobject.newobject

A simple signal/event mechanism, to be used like this:

def mycallback(arg, **kwargs):
    print(arg, kwargs['foo'])

myevent = Event()
myevent.connect(mycallback)
myevent.emit('test', foo = 'bar')
# Or just: myevent('test', foo = 'bar')
__init__()[source]

Constructor.

connect(callback, *args, **kwargs)[source]

Connects the event with the given callback. When the signal is emitted, the callback is invoked.

Hint

The signal handler is stored with a hard reference, so you need to make sure to call disconnect() if you want the handler to be garbage collected.

Parameters:
  • callback (object) – The callback function.
  • args (tuple) – Optional arguments passed to the callback.
  • kwargs (dict) – Optional keyword arguments passed to the callback.
disconnect(callback)[source]

Disconnects the signal from the given function.

Parameters:callback (object) – The callback function.
disconnect_all()[source]

Disconnects all connected functions from all signals.

emit(*args, **kwargs)[source]

Emits the signal, passing the given arguments to the callbacks. If one of the callbacks returns a value other than None, no further callbacks are invoked and the return value of the callback is returned to the caller of emit().

Parameters:
  • args (tuple) – Optional arguments passed to the callbacks.
  • kwargs (dict) – Optional keyword arguments passed to the callbacks.
Return type:

object

Returns:

Returns None if all callbacks returned None. Returns the return value of the last invoked callback otherwise.

is_connected(callback)[source]

Returns True if the event is connected to the given function.

Parameters:callback (object) – The callback function.
Return type:bool
Returns:Whether the signal is connected to the given function.
listen(callback, *args, **kwargs)[source]

Like connect(), but uses a weak reference instead of a normal reference. The signal is automatically disconnected as soon as the handler is garbage collected.

Hint

Storing signal handlers as weak references means that if your handler is a local function, it may be garbage collected. To prevent this, use connect() instead.

Parameters:
  • callback (object) – The callback function.
  • args (tuple) – Optional arguments passed to the callback.
  • kwargs (dict) – Optional keyword arguments passed to the callback.
Return type:

Exscript.util.weakmethod.WeakMethod

Returns:

The newly created weak reference to the callback.

n_subscribers()[source]

Returns the number of connected subscribers.

Return type:int
Returns:The number of subscribers.