event¶
Module for implementing the event-driven programming paradigm.
The event module provides a means for implementing event-driven
programming. This is done through the Event object.
The Event object allows data to be communicated to callback methods
via the __trigger__() method. Callback methods can un/subscribe to the
Event object via the unsubscribe() and subscribe()
methods.
Example usage:
import os
from mcl import Event
pub = Event()
pub.subscribe(lambda data: os.sys.stdout.write(data))
pub.__trigger__('hello world')
Classes
-
class
Event[source]¶ Class for issuing events and triggering callback functions.
-
__trigger__(*args, **kwargs)[source]¶ Trigger an event and issue data to the callback functions.
Parameters: - *args – Arbitrary mandatory arguments to send to callback functions.
- **kwargs – Arbitrary keyword arguments to send to callback functions.
-
is_subscribed(callback)[source]¶ Return whether a callback is registered with this object.
Parameters: callback (function) – The callback to test for registration. Returns: Returns Trueif the callback has been registered with this object. ReturnsFalseif the callback has NOT been registered with this object.Return type: bool
-
num_subscriptions()[source]¶ Return the number of registered callbacks.
Returns: number of registered callbacks. Return type: int
-
subscribe(callback)[source]¶ Subscribe a callback to events.
Parameters: callback (function) – The callback to execute on a event. Returns: Returns Trueif the callback was successfully registered. If the callback already exists in the list of callbacks, it will not be registered again andFalsewill be returned.Return type: bool Raises: TypeError– If the input callback does not have a ‘__call__’ method, aTypeErroris raised.
-
unsubscribe(callback)[source]¶ Unsubscribe a callback from events.
Parameters: callback (function) – The callback to be removed from event notifications. Returns: Returns Trueif the callback was successfully removed. If the callback does not exist in the list of callbacks, it will not be removed andFalsewill be returned.Return type: bool
-