Skip to main content

Lightweight Event Handling

Project description

Lightweight event handling for Python

Build Status Coverage Status PyPI version GitHub 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

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


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.0.tar.gz (12.9 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.0-py2.py3-none-any.whl (13.7 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

File hashes

Hashes for python-dispatch-0.1.0.tar.gz
Algorithm Hash digest
SHA256 af9562cd5a1743de413b421f2d1e8352ac86fdcbaee4c96fbabe86ce0c65765b
MD5 378d053b27614044e2cc2d1316fe4388
BLAKE2b-256 6c4eac3f8bb1929b005eedfe5337bb814091f0abed48658e0a7b52f62a43a521

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_dispatch-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 efaab90c84fe060b91da4aff8ac61e6b775d1d9ab4ffe844a9c52847c06058c0
MD5 b90dcdb0936c716908d541998a8042ad
BLAKE2b-256 afd0b59ca6503f1dd9ca70a04fbab72ecf4cef73a975843f2ed7e80f4aec633a

See more details on using hashes here.

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