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. It has no dependencies.
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.0rc4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 027f7f4ddce7988be75ceaa65f3ba87af5d9ec729be322fc083973621dfc2c59 |
|
MD5 | 3c324225e506fc732c34ecaf1191903b |
|
BLAKE2b-256 | dab1776d58dbd1fb53e9b361a79fb1dbd84b29840a681e470a0a6fc22dd9c1ed |
Hashes for psygnal-0.10.0rc4-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e62dfc180fb3f92bde9b279c6ccd3960ba6a4e20c801cfae5b708a2fb7001e11 |
|
MD5 | 38bbbba629afc87e539e3803e5a30e52 |
|
BLAKE2b-256 | 9712f1d23d7f83a7577a67b92985d5cd2ae4250656056490765f1304d9165071 |
Hashes for psygnal-0.10.0rc4-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fbe2ce9def290b206d06f64adbb41f2e7fabd82b53a603cae9aaedaed9a7a541 |
|
MD5 | fbdb69afb163363450c5eb7d52a600cd |
|
BLAKE2b-256 | f813757308c545fa27cd70c9248a5d2ead6dbff34f222fbcbfaa3bff229f737d |
Hashes for psygnal-0.10.0rc4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c003af34b0aadfbf6c6e43db1c988e3e5ca7fd3c8562eb39bef6a9c218560d99 |
|
MD5 | de4b3ebf1c92ccaeb262faea7328261a |
|
BLAKE2b-256 | 7e4ab2d74cf8088341831534e66ec8fefa01f044be55dc3d5b4dd88c16d0dfc9 |
Hashes for psygnal-0.10.0rc4-cp312-cp312-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6186ab2787f4f5c908fb3c074adfabbbe789007ae3d81711831b11b518411f33 |
|
MD5 | b0694509ccd52d232043b238756a07c7 |
|
BLAKE2b-256 | 769176ea852731ebc76ac0df4d34a4ec67950977d45ffca6434b7294e0e66a95 |
Hashes for psygnal-0.10.0rc4-cp312-cp312-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d50d8f161b5ecdbc92980b3acad68cca7750258dea888083040b0ceaf9c58ec |
|
MD5 | ae9c010919b05884e6b9f0bae7b6d894 |
|
BLAKE2b-256 | 976b7d66a59c5cbda0288b8d507a24541d3753a5be402dd8be5129c4736c4087 |
Hashes for psygnal-0.10.0rc4-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c727ebef5d3b7968685d44a1924c6cfe4f8def06e07fe0097bbf6207d0d325b7 |
|
MD5 | b7415cb63685425050b6f68b61b46d58 |
|
BLAKE2b-256 | cd04b0084374abdbc787b112278f964e12e34909310c20995dc83e7fc4b128c1 |
Hashes for psygnal-0.10.0rc4-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 145b2de0a0586a66ca1cc66aecca29db623f0204550e3db779d3a72878750540 |
|
MD5 | 56178e506901a9190b6cda2783c8ac1c |
|
BLAKE2b-256 | 6474d61b876d4af7b39a72ca9edf8e63237f9965baa6d25ab6e911b9e0339555 |
Hashes for psygnal-0.10.0rc4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c0bf4b80b39b3d3f48fa7ea90e8be8ea8b3f23b83d778785a6b1f1568a2a2a8 |
|
MD5 | c68b47f9fe26b2504e3c2275e131a315 |
|
BLAKE2b-256 | 7ab9dc9e7173c1d5f0749d829af8155d9d6e70ac9a63a826af5315ef4e02314a |
Hashes for psygnal-0.10.0rc4-cp311-cp311-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ad2a7737d58fe3662a04e83ecdf4cf59d6dc678ef0cb7cdf4d7999f2282a9c4 |
|
MD5 | d6e3d321a33368cabd5deacb3ddc3943 |
|
BLAKE2b-256 | 32e661c1ce43eb2157d5f404b1887a6053164581fa36e2688fd10313e13b28fb |
Hashes for psygnal-0.10.0rc4-cp311-cp311-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 27ec2435a38e7ff33cdcbb50850a57f8f8cff863fd3bab38e251fef06155817f |
|
MD5 | 7f3b5fabb0c7d572a20e4e73337c4e8e |
|
BLAKE2b-256 | a83009b0ea24b6637863e4edd7368851bdf75358ba3982535cd390c9d5795491 |
Hashes for psygnal-0.10.0rc4-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fbc200b23bef6f7deedf1367ac257c1494e2d234f43f79b002535118d2bc8206 |
|
MD5 | dc3cb86e701269c731a7e9cdb78185b0 |
|
BLAKE2b-256 | 521c77189c2faa387776c65868ec87fbd5bf40b60bd3959ecbe5c5d01c85c019 |
Hashes for psygnal-0.10.0rc4-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 856efc87960517be4e42655a1fc7881e508ec9f8dea1ba5fd0bacd32008af660 |
|
MD5 | 617cceccc0f50461501ba7c3dd95180e |
|
BLAKE2b-256 | f625bc7d2e581fa3f204c5e65d456c4ef3468ead8ded54d77e0325d3fdbf9560 |
Hashes for psygnal-0.10.0rc4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5c663b9da7d55fb30dd98f4b0808884605e50862b932afb498fc27c1997bccd9 |
|
MD5 | b55e50dbb7d5d8088fb841282978f067 |
|
BLAKE2b-256 | 355002bfca8a3af2ef18acec0ad293384551bb4db40bae728ca1b2a38afc22de |
Hashes for psygnal-0.10.0rc4-cp310-cp310-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc723ab9aa206aaba31aa2489f320aad46a33cfd233f912a6ea5b443878c6193 |
|
MD5 | 0d365c700a16bd2cde3602d91ced0fd2 |
|
BLAKE2b-256 | b7c8cb6ea55ba46a6abf5180ac271bf0c68588b48cc08d7c1d2de07ca016b42c |
Hashes for psygnal-0.10.0rc4-cp310-cp310-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f5108dae3f47d2743da1739bf880d7ee00205b539d2f1279b9fd5c263725a9a5 |
|
MD5 | dbd223c3a3ba2f887e8cee7f5d861467 |
|
BLAKE2b-256 | d91ced5be470f7d58508d1527ebfba772ed347a266fbcf16a524acbde8d04689 |
Hashes for psygnal-0.10.0rc4-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 447cf97b6600b3be1bd378f5a984e7c81616f73206c3a6571fb3512f906fbf9a |
|
MD5 | b4286052f50ae176e292a4083067f16d |
|
BLAKE2b-256 | cd501e034fb4be9a2b32a2698d3f2b38e2efc4aaf67868e7cfc1f5c934950838 |
Hashes for psygnal-0.10.0rc4-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b139c8db7e9312a13ec8523738cabf8889bd6b58006e4efb4c56872adcc68629 |
|
MD5 | f8629deddaf763d331d3eba38980d623 |
|
BLAKE2b-256 | 834d28886102bd6aba3d899ecd52616d42a527a58e189890e61815ba25f9bd39 |
Hashes for psygnal-0.10.0rc4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 95da41ea75caf931eccc2cf27060bcafba0937e665b714d885567c7eabb8f234 |
|
MD5 | 2e0a158835ac342dbed9cc96fd614d00 |
|
BLAKE2b-256 | d78feb3edc01d3c74e8433f3ae7224eff36dab2b030ff7a29b8f46a2515e89a3 |
Hashes for psygnal-0.10.0rc4-cp39-cp39-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9217001fafb6845ffd5d4ba9896e9c63dbc9ff498634125988765bf3a4ab53e |
|
MD5 | 6bc6930de21bcad1f7add1a84224dec9 |
|
BLAKE2b-256 | 86b76e5fe38255f5f5e8b77b5a5183afbdd97916fbd1fd930964c61286c31810 |
Hashes for psygnal-0.10.0rc4-cp39-cp39-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 94d301c605060843f8a0e85930d667ee800a18b28b7ab83f4b78cc931338fc5d |
|
MD5 | 63acfdf36e7be69e94a280985fdf14fd |
|
BLAKE2b-256 | c4b00ed4bddab4124c91659adad8ee216176ec890a4c231e7fd0997d6516180e |
Hashes for psygnal-0.10.0rc4-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 43ff4c6588b2b256ff620501ec0eb7f88e308cef0ceb2fc4d61d386d4cce9f20 |
|
MD5 | c90f102552a4ee3525fdadf903098e81 |
|
BLAKE2b-256 | 05c80c264800fad58b0f77d4fc2c74d6050734a176a3da6342d0f2a568ced6e7 |
Hashes for psygnal-0.10.0rc4-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aecc2fa78e3551528ab2feee26fb3a9128daadce16bca1cf38aa8c619ddfa14a |
|
MD5 | be2c6c743cba0ff702d0e015d5dbdc61 |
|
BLAKE2b-256 | 52f8aebddbd1bb872019adc760c238308d26237a11280047f4ff9b864877d7c3 |
Hashes for psygnal-0.10.0rc4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 57090b89ffa2545c22ad7d5657172c1975865b7bb66b4ba9c1a1f784f7e51ef9 |
|
MD5 | a29c006d72e88fce4ce5cc928fbf46eb |
|
BLAKE2b-256 | 62c27c28ba8c0d999495299b1081799ad0529cb3b3712fb37a38c6c5e4cb6565 |
Hashes for psygnal-0.10.0rc4-cp38-cp38-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 347a2b628400ff8ea6d6901214804caa552d17c10773322323383adce24dd9d3 |
|
MD5 | c9489d47e2bea144c1797d6572099a54 |
|
BLAKE2b-256 | cd605d3f3e28214cfe6075ba64bb03c53cd9f25929da92bddcefee099c61133f |
Hashes for psygnal-0.10.0rc4-cp38-cp38-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b3e05ec50c59678efc33301e9d6326d34ee9953075d5a4e9624bcc1dc9b4a00a |
|
MD5 | ab3e01535e111f75a6ff83cc6aba31f9 |
|
BLAKE2b-256 | 6fb7422754c6054af29fc92bada48c5498b087d392b443afe86863c2b2b5a867 |