Fast python callback/event system modeled after Qt Signals
Project description
psygnal
Psygnal (pronounced "signal") is a pure python implementation of the observer pattern, with the API of Qt-style Signals with (optional) signature and type checking, and support for threading.
This library does not require or use Qt in any way, It simply implements a similar observer pattern API.
Documentation
https://psygnal.readthedocs.io/
Install
pip install psygnal
conda install -c conda-forge psygnal
Usage
The observer pattern is a software design pattern in which an object maintains a list of its dependents ("observers"), and notifies them of any state changes – usually by calling a callback function provided by the observer.
Here is a simple example of using psygnal:
from psygnal import Signal
class MyObject:
# define one or more signals as class attributes
value_changed = Signal(str)
# create an instance
my_obj = MyObject()
# You (or others) can connect callbacks to your signals
@my_obj.value_changed.connect
def on_change(new_value: str):
print(f"The value changed to {new_value}!")
# The object may now emit signals when appropriate,
# (for example in a setter method)
my_obj.value_changed.emit('hi') # prints "The value changed to hi!"
Much more detail available in the documentation!
Evented Dataclasses
A particularly nice usage of the signal pattern is to emit signals whenever a
field of a dataclass changes. Psygnal provides an @evented
decorator that will
emit a signal whenever a field changes. It is compatible with dataclasses
from the standard library,
as well as attrs, and
pydantic:
from psygnal import evented
from dataclasses import dataclass
@evented
@dataclass
class Person:
name: str
age: int = 0
person = Person('John', age=30)
# connect callbacks
@person.events.age.connect
def _on_age_change(new_age: str):
print(f"Age changed to {new_age}")
person.age = 31 # prints: Age changed to 31
See the dataclass documentation for more details.
Evented Containers
psygnal.containers
provides evented versions of mutable data structures
(dict
, list
, set
), for cases when you need to monitor mutation:
from psygnal.containers import EventedList
my_list = EventedList([1, 2, 3, 4, 5])
my_list.events.inserted.connect(lambda i, val: print(f"Inserted {val} at index {i}"))
my_list.events.removed.connect(lambda i, val: print(f"Removed {val} at index {i}"))
my_list.append(6) # Output: Inserted 6 at index 5
my_list.pop() # Output: Removed 6 at index 5
See the evented containers documentation for more details.
Benchmark history
https://pyapp-kit.github.io/psygnal/
and
https://codspeed.io/pyapp-kit/psygnal
Developers
Compiling
While psygnal
is a pure python package, it is compiled with mypyc to increase
performance. To test the compiled version locally, you can run:
make build
(which is just an alias for HATCH_BUILD_HOOKS_ENABLE=1 pip install -e .
)
Debugging
To disable all compiled files and run the pure python version, you may run:
python -c "import psygnal.utils; psygnal.utils.decompile()"
To return the compiled version, run:
python -c "import psygnal.utils; psygnal.utils.recompile()"
The psygnal._compiled
variable will tell you if you're using the compiled
version or not.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for psygnal-0.10.0rc0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9652a91176c3a17d503a6f1eaeaa4703ce5f82da17da08ebe561ed68a375bd61 |
|
MD5 | 9af363de4326fdf0d4b1fccc4a706134 |
|
BLAKE2b-256 | 708255a12e815300990667f04f310ae05fe984186cf6c32da647faa296d3634e |
Hashes for psygnal-0.10.0rc0-cp312-cp312-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f48a1d39001822ade76b171009bcc917ac4b1bb9bf2121d16956a354d0013b58 |
|
MD5 | 05dd9f9604c6a5989be88a3b3d6b4217 |
|
BLAKE2b-256 | ed73ea7b60d742fd08fc52792ea427d5fb838cc831a5a448bb1563323150e778 |
Hashes for psygnal-0.10.0rc0-cp312-cp312-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 943c6aea02d33407d00bd8af2a3c3f3431fa9c5db747b52b94012cc0ee9147cd |
|
MD5 | 0433d4fcf1891965ba455dd2b0599664 |
|
BLAKE2b-256 | 5cd6a8cc9c1b068729f92f2aeadcb2246e2995ecccc1ea69c50108873f0bf5e0 |
Hashes for psygnal-0.10.0rc0-cp311-cp311-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 054594b97653540c875675c76657519efa66f9a38064101dbd0f4899e9076787 |
|
MD5 | 443e9908a5a3d194a15677362c76bfe8 |
|
BLAKE2b-256 | 3624f424561e8a257c7856b97ddfd80d4dccf15e488d39ad10c10184ef939af2 |
Hashes for psygnal-0.10.0rc0-cp311-cp311-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2847b52f5e48f66b04218999e94cad9985e493ba303633e598ea819a734fb244 |
|
MD5 | da188df4ffa3caaf43bf3516100c90f7 |
|
BLAKE2b-256 | 5a8096051a2fc1c394e85af768aae85c3192b70e0685e8ddad66dc34c3e1d718 |
Hashes for psygnal-0.10.0rc0-cp310-cp310-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 79f93402f24d54cff49caad41782cda99c243643f2b35df1536f56d88bf28254 |
|
MD5 | e8fdefaaa30e12c257b2984fc040f39e |
|
BLAKE2b-256 | a420a160405b93ec6426b6955de4df879cf8ebbeb7cad2a780d199edc1b65b95 |
Hashes for psygnal-0.10.0rc0-cp310-cp310-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c73484bffcaf631dc2cf539e0b7e86a7ae645395586fa9abee69c78d76e61546 |
|
MD5 | 31a86cbdf463537a870007bc7039c13e |
|
BLAKE2b-256 | 14f79d4642d9988fb3a69f4645a81795064ce1553f6c6e49a8eb84943064854c |
Hashes for psygnal-0.10.0rc0-cp39-cp39-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ccea8905bd4fcc467db382edf9b8153eeee8d7f2f6bfbba639396ab5fa3b531 |
|
MD5 | 95fa2b797447efbaf8094cecef4795eb |
|
BLAKE2b-256 | 2a0a54202c4ec75afd379e288efbccd6bd5f33b6f2f329189f642e22e31d6197 |
Hashes for psygnal-0.10.0rc0-cp39-cp39-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0e2b4fbb352d793d270bef847b78c507f28e02511a9409e29a2731f506242187 |
|
MD5 | dee6517726132c84d1182441eda4264f |
|
BLAKE2b-256 | cb976af0de697d38df46fabe50b341dc7077277df19f8ca4b37310498932f9b3 |
Hashes for psygnal-0.10.0rc0-cp38-cp38-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7e8321201cec7a4a4473f28e30eed54a031ad06cc77a05d9b5ed9dac95ac4719 |
|
MD5 | 8c27dd7ba75bbfa8bf69a32c02433cec |
|
BLAKE2b-256 | bcc32b1000fcafdc8a4090e2e72c82c5e91f2166c3c6510cc5b34c932ddb2a41 |
Hashes for psygnal-0.10.0rc0-cp38-cp38-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b3bf5c6df1e52a94190e72fed554885eea66d8cb20c3cc3559000c677347851a |
|
MD5 | f76e4e929d5438ae1ac5a34807e68ec0 |
|
BLAKE2b-256 | 9580d12a9fe9a3eb3f621c5e57caa83ac42157dd0928bf185e288f7c5fc58efd |