pyobservable
Description
pyobservable provides a simple event system for Python with weak reference support.
This ensures that the event handlers do not stay in memory when they aren't needed anymore.
Installation
pyobservable can be installed using pip:
$ pip install pyobservable
Alternatively, you can download the repository and run the following command from within the source directory:
$ python setup.py install
Usage
For a quick start, a minimal example is:
from pyobservable import Observable
obs = Observable()
obs.add_event('foo')
obs.add_event('bar')
# Event keys can be any object that is hashable
event = object()
obs.add_event(event)
# Binding with decorator usage
@obs.bind('foo')
def foo_handler(foo_number):
print('foo_handler called:', foo_number)
# Binding with function usage
def bar_handler(bar_list):
print('bar_handler called:', bar_list)
obs.bind('bar', bar_handler)
obs.notify('foo', 1)
obs.notify('bar', [1, 2, 3])
The rationale behind the requirement to add events before binding to them is to ensure the code is less error-prone from mistyping event names.
Also, if a duplicated event key is present, ValueError will be raised.
However, the next example shows that event registration can be simplified using the special _events_ attribute:
from pyobservable import Observable
class EventEmitter(Observable):
_events_ = ['foo', 2]
def triggers_foo(self):
self.notify('foo', 1, 2, 3)
event_emitter = EventEmitter()
@event_emitter.bind('foo')
def foo_handler(*args):
print(*args)
event_emitter.triggers_foo()
Also note that _events_ can be defined multiple times in an inheritance tree.
Observable scans the MRO for this attribute and adds every event it finds.
Again, a ValueError will be raised if a duplicate event key is present.
Finally, here's an advanced and clean example using enum:
from enum import Enum, auto
from pyobservable import Observable
class EventType(Enum):
FOO = auto()
BAR = auto()
class EventEmitter(Observable):
_events_ = EventType # Enums are iterable
def triggers_foo(self):
self.notify(EventType.FOO, 'foo happened!')
class EventListener:
def on_foo(self, message):
print("Here's a message from foo:", message)
event_emitter = EventEmitter()
event_listener = EventListener()
event_emitter.bind(EventType.FOO, event_listener.on_foo) # pyobservable also supports bound methods
event_emitter.triggers_foo()
For more information, please refer to the Observable class docstrings.
Tests
The simplest way to run tests:
$ python tests.py
As a more robust alternative, you can install tox (or tox-conda if you use conda) to automatically support testing across the supported python versions, then run:
$ tox
Issue tracker
Please report any bugs and enhancement ideas using the issue tracker.
License
pyobservable is licensed under the terms of the MIT License (see LICENSE.txt for more information).
Metadata
Release files for pyobservable 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pyobservable-1.0.1.tar.gz | 5.1 kB | Without provenance | Details for pyobservable-1.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pyobservable-1.0.1-py3-none-any.whl | Python 3 | none | any | Details for pyobservable-1.0.1-py3-none-any.whl |
Total release size: 10.1 kB
Release files / pyobservable-1.0.1.tar.gz
| Download URL | pyobservable-1.0.1.tar.gz |
|---|---|
| Size | 5.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3adce42409bf6e1d7382f6c98e2a7c9e7aa094ca98092d1afa863cd1e5fdb5e7
|
|
BLAKE2b-256 checksum How to use checksums |
56b70231d481a1e720aa5b1875c47ac9819d18072ea311c7cbe1e496f0658233
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5
|
Release files / pyobservable-1.0.1-py3-none-any.whl
| Download URL | pyobservable-1.0.1-py3-none-any.whl |
|---|---|
| Size | 5.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
273999b5fee1ad1e18fd638dc6a44064350a7bef6784031c29ec5ad79430b063
|
|
BLAKE2b-256 checksum How to use checksums |
410b222bae9d7dcf6a8622f281769c1139bda8657414548fdf925d844e25fa45
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5
|
Security for pyobservable 1.0.1
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.