Skip to main content

python-dispatch

Lightweight event handling for Python

Build 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

pip install python-dispatch
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

Metadata

Release files for python-dispatch 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-dispatch 0.1.2
File Size Uploaded Provenance Actions
python-dispatch-0.1.2.tar.gz 13.8 kB Without provenance Details for python-dispatch-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for python-dispatch 0.1.2
File Interpreter ABI Platform Actions
python_dispatch-0.1.2-py2.py3-none-any.whl Python 3, Python 2 none any Details for python_dispatch-0.1.2-py2.py3-none-any.whl

Total release size: 28.4 kB

Release files / python-dispatch-0.1.2.tar.gz

Download URL python-dispatch-0.1.2.tar.gz
Size 13.8 kB
Tags Source
SHA-256 checksum
How to use checksums
9458e5555aa8a9a9df56285086be8ccba7f93addeb92d1c5555aa20f8d1a73ca
BLAKE2b-256 checksum
How to use checksums
4309a2b397cfd3f12e734ec921ef46847cb7676f102e410b1ccdb0af4e35f81d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.4

Release files / python_dispatch-0.1.2-py2.py3-none-any.whl

Download URL python_dispatch-0.1.2-py2.py3-none-any.whl
Size 14.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
05a331611debfd2cbde8c98970933cfd39deac872f59bdc833312740806bf1e0
BLAKE2b-256 checksum
How to use checksums
e5eb2de733a6ae56d4135bcab5899859c45bc031839b7b0294ad3ac78f97d110
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.4

Security for python-dispatch 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page