Lightweight Event Handling
Project description
python-dispatch
Lightweight event handling for Python
Description
This is an implementation of the "Observer Pattern" with inspiration from the Kivy framework. Many of the features though are intentionally stripped down and more generalized. The goal is to have a simple drop-in library with no dependencies that stays out of the programmer's way.
Installation
pip install python-dispatch
Links
Project Home | https://github.com/nocarryr/python-dispatch |
PyPI | https://pypi.python.org/pypi/python-dispatch |
Documentation | https://nocarryr.github.io/python-dispatch |
Usage
Events
from pydispatch import Dispatcher class MyEmitter(Dispatcher): # Events are defined in classes and subclasses with the '_events_' attribute _events_ = ['on_state', 'new_data'] def do_some_stuff(self): # do stuff that makes new data data = self.get_some_data() # Then emit the change with optional positional and keyword arguments self.emit('new_data', data=data) # An observer - could inherit from Dispatcher or any other class class MyListener(object): def on_new_data(self, *args, **kwargs): data = kwargs.get('data') print('I got data: {}'.format(data)) def on_emitter_state(self, *args, **kwargs): print('emitter state changed') emitter = MyEmitter() listener = MyListener() emitter.bind(on_state=listener.on_emitter_state) emitter.bind(new_data=listener.on_new_data) emitter.do_some_stuff() # >>> I got data: ... emitter.emit('on_state') # >>> emitter state changed
Properties
from pydispatch import Dispatcher, Property class MyEmitter(Dispatcher): # Property objects are defined and named at the class level. # They will become instance attributes that will emit events when their values change name = Property() value = Property() class MyListener(object): def on_name(self, instance, value, **kwargs): print('emitter name is {}'.format(value)) def on_value(self, instance, value, **kwargs): print('emitter value is {}'.format(value)) emitter = MyEmitter() listener = MyListener() emitter.bind(name=listener.on_name, value=listener.on_value) emitter.name = 'foo' # >>> emitter name is foo emitter.value = 42 # >>> emitter value is 42
Project details
Release history Release notifications
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Filename, size | File type | Python version | Upload date | Hashes |
---|---|---|---|---|
Filename, size python_dispatch-0.1.2-py2.py3-none-any.whl (14.6 kB) | File type Wheel | Python version py2.py3 | Upload date | Hashes View hashes |
Filename, size python-dispatch-0.1.2.tar.gz (13.8 kB) | File type Source | Python version None | Upload date | Hashes View hashes |
Close
Hashes for python_dispatch-0.1.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 05a331611debfd2cbde8c98970933cfd39deac872f59bdc833312740806bf1e0 |
|
MD5 | 78d55803c98bf6068508d32555d808c6 |
|
BLAKE2-256 | e5eb2de733a6ae56d4135bcab5899859c45bc031839b7b0294ad3ac78f97d110 |