Skip to main content

Lightweight Event Handling Library for Python

Project description

python-dispatch

Lightweight event handling for Python

uvGitHub Actions Workflow StatusCoverage StatusPyPI versionGitHub license

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

"python-dispatch" is available on PyPI and can be installed using pip:

pip install python-dispatch

Python Requirements

After version 0.1 of this project, only Python 3.6 and above will be supported. If using an older Python version, the older releases should still be available on PyPI and the correct package should be chosen automatically by pip. If not, either upgrade pip and setuptools:

pip install -U pip setuptools

Or specify the version manually:

pip install python-dispatch<0.2

Links

Project Home https://github.com/nocarryr/python-dispatch
PyPI https://pypi.python.org/pypi/python-dispatch
Documentation https://python-dispatch.readthedocs.io

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 = {'foo':'bar'}
...         # 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()

>>> # Bind to the "on_state" and "new_data" events of emitter
>>> emitter.bind(on_state=listener.on_emitter_state)
>>> emitter.bind(new_data=listener.on_new_data)

>>> emitter.do_some_stuff()
I got data: {'foo': 'bar'}
>>> 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()

>>> # Bind to the "name" and "value" properties of emitter
>>> emitter.bind(name=listener.on_name, value=listener.on_value)

>>> # Set emitter.name property (triggering the on_name callback)
>>> emitter.name = 'foo'
emitter name is foo

>>> # Set emitter.value (triggering the on_value callback)
>>> emitter.value = 42
emitter value is 42

Contributing

Contributions are welcome!

If you want to contribute through code or documentation, please see the Contributing Guide for information.

License

This project is released under the MIT License. See the LICENSE file for more information.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_dispatch-0.2.3.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_dispatch-0.2.3-py3-none-any.whl (16.7 kB view details)

Uploaded Python 3

File details

Details for the file python_dispatch-0.2.3.tar.gz.

File metadata

  • Download URL: python_dispatch-0.2.3.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for python_dispatch-0.2.3.tar.gz
Algorithm Hash digest
SHA256 ed9daaa234ebc72c0fe5302fa28a040bc33d7685f85bb39d2e10548c9b110a60
MD5 d623247a6fe0116ad361a404875778b5
BLAKE2b-256 e513bc2a3502c4313abfa6f00236dae1cb283043ccdab04b3dcb37f9084006c6

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_dispatch-0.2.3.tar.gz:

Publisher: dist-test.yml on nocarryr/python-dispatch

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file python_dispatch-0.2.3-py3-none-any.whl.

File metadata

File hashes

Hashes for python_dispatch-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 77d88571c52226a6f9745c49e7548e2b7bb3c361046fca12c1baeffd6f09eb8d
MD5 319b5603fd75c4f8360522d23d696dbd
BLAKE2b-256 b56af8c192748d17d946969c41b365a90519b87062ed0cff590a60d7a3ed5f4e

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_dispatch-0.2.3-py3-none-any.whl:

Publisher: dist-test.yml on nocarryr/python-dispatch

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page