mlpy.modules.patterns.Listener

class mlpy.modules.patterns.Listener(o=None, events=None)[source]

Bases: object

The listener interface.

A listener subscribes to an observable identifying the events the listener is interested in. The observable calls notify to send relevant event information.

Parameters:

o : Observable, optional

The observable instance.

events : str or list[str], optional

The event names the listener wants to be notified about.

Notes

Every class inheriting from Listener must implement notify, which defines what to do with the information send by the observable.

Examples

>>> from mlpy.modules.patterns import Observable
>>>
>>> class MyObservable(Observable):
>>>     pass
>>>
>>> o = MyObservable()

This defines the observable MyObservable and creates an instance of it.

>>> from mlpy.modules.patterns import Listener
>>>
>>> class MyListener(Listener):
>>>
>>>     def notify(self, event):
>>>         print "I have been notified!"
>>>
>>> l = MyListener(o, "test")

This defines the listener MyListener that when notified will print the same text to the console regardless of which event has been thrown (as long as the listener has subscribed to the event). Then an instance of MyListener is created that subscribes to the event test of MyObservable.

When the event test is dispatched by the observable, the listener is notified and the text is printed on the stdout:

>>> o.dispatch("test", **{})
I have been notified!

Methods

notify(event) Notification from the observable.