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
Usage
For more information, see the documentation here: https://nocarryr.github.io/python-dispatch
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
Metadata
Release files for python-dispatch 0.0.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| python-dispatch-0.0.8.tar.gz | 9.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| python_dispatch-0.0.8-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 20.6 kB
Release files / python-dispatch-0.0.8.tar.gz
| Download URL | python-dispatch-0.0.8.tar.gz |
|---|---|
| Size | 9.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
dfa8a67402977c66e5813385ffc364ace68d9fd8b37fd8bd72f3cff2654c2e04
|
|
BLAKE2b-256 checksum How to use checksums |
537fc69d7e5f2a0198454cfd6d6e8b85b952fc6a0c3c73cf9728cc8524b6bfea
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / python_dispatch-0.0.8-py2.py3-none-any.whl
| Download URL | python_dispatch-0.0.8-py2.py3-none-any.whl |
|---|---|
| Size | 11.4 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
b2ea5ed792a399f3589279684bfd71d02a2384771e419603dc1e80197392c391
|
|
BLAKE2b-256 checksum How to use checksums |
1aca3545507fc0e87a10901aa301762f2638811eaf3077868e84ba0be7a928ad
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |