pyee supplies an event_emitter object that acts similar to the EventEmitter that comes with node.js.
In [1]: from pyee import Event_emitter
In [2]: ee = Event_emitter()
In [3]: @ee.on('event')
...: def event_handler():
...: print 'BANG BANG'
...:
In [4]: ee.emit('event')
BANG BANG
In [5]:
Easy-peasy.
sudo pip install pyee
ee.on(event, f=None): Registers the function f
to the event name event
. Example:
ee.on('data', some_fxn)
If f
is not specified, ee.on returns a function that takes f
as a callback, which allows for decorator styles:
@ee.on('data')
def data_handler(data):
print data
ee.emit(event, *args,kwargs)*: Emits the event, calling the attached functions withargs. For example:
ee.emit('data', '00101001')
which will call data('00101001')'
ee.once(event, f=None): The same as ee.on
except that the listener is automatically removed after it's called.
ee.remove_listener(event, fxn): Removes the function fxn
from event
. Requires that the function is not closed over by ee.on---that is, being able to use this with the decorator style is unfortunately not possible.
ee.remove_all_listeners(event): Removes all listeners from event
.
ee.listeners(event): Returns the array of all listeners registered to the given event
.
(Special) Events: -------
"new_listener": Fires whenever a new listener is created. Listeners for this event do not fire upon their own creation.
nosetests
If you're a python and/or node.js fan and like what you see (or don't quite like what you see), I heartily invite you to dig in, fork it up and git push it good.
MIT.