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

"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 = 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

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.

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.1.32.tar.gz (14.6 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.1.32-py2.py3-none-any.whl (15.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file python-dispatch-0.1.32.tar.gz.

File metadata

  • Download URL: python-dispatch-0.1.32.tar.gz
  • Upload date:
  • Size: 14.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.12

File hashes

Hashes for python-dispatch-0.1.32.tar.gz
Algorithm Hash digest
SHA256 07f2d00fb108e36e308d754b9eb3199678fa5da6fd509d5ce393790b3580aedb
MD5 fa7dfa69fb2ad163c2edebe10796fb32
BLAKE2b-256 9ae1a68dc980a238242c5abe4bb3db22b3e3ef65b62442086b405d37039c4162

See more details on using hashes here.

File details

Details for the file python_dispatch-0.1.32-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for python_dispatch-0.1.32-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 25172e310ede130a1f30b8935e6703f3d3a8db6863a3b637ab8ca3941e9f78db
MD5 6cf4db1459a64fc193a452cbd98abcf2
BLAKE2b-256 31ec2e1130a7302e0447bbacbf1ddd4c3c92ba24dcff8b59768f221e029661b0

See more details on using hashes here.

Release history Release notifications | RSS feed

0.2.3

2 files

0.2.2

2 files

0.2.1

2 files

0.2.0

2 files

This release

0.1.32 This release

2 files

0.1.31

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

0.0.91

2 files

0.0.9

2 files

0.0.8

2 files

0.0.7

2 files

0.0.6

2 files

0.0.5

2 files

0.0.4

2 files

0.0.3

2 files

0.0.2

2 files

0.0.1

2 files

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