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.2-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 12a73bebc739a6331a618ecaa7ad0aee410b5009ec7fa0cd5f55e521ee776132 |
|
MD5 | f1578c62ae8a4ec0bb62c50b08509c07 |
|
BLAKE2b-256 | 51ba0a3fc341938379d1b5a6ae187992b6026f2bef748d3ef42972321c60dab1 |
Hashes for psygnal-0.10.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c56ecf7a5f4125bc0b3721c46efe80756df4ebf394aad435ae8114c95a5316c6 |
|
MD5 | 15ce6a243c79970abfdbea1339caf7a1 |
|
BLAKE2b-256 | f6392cdd2c1904b122b227e28c574297a233e05f073cee9339719013e6c8661a |
Hashes for psygnal-0.10.2-cp312-cp312-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 92e61acfd42ed89b5c6df84ce19eb4d12f9376d2ac8d47f9cd903ae95ebd0813 |
|
MD5 | 00e5773cc1bbacb888596c3663535735 |
|
BLAKE2b-256 | 65ea3d4bec9989faf1c763c7c15b1d4d180ed36978346fb595af26f85f1a5cbd |
Hashes for psygnal-0.10.2-cp312-cp312-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 407729dda66b67b0f19c5a49b33c14b657e994ce4807cbdf82efc723a909b0ac |
|
MD5 | c8be8340b40e26c80fb087dca437e875 |
|
BLAKE2b-256 | 9649bf8ca1bb1514e90cf8a958d80269e9410433e94dcbc366b23c6ba40ca7fb |
Hashes for psygnal-0.10.2-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd758ca3ef9a4c36589492117cf8a7dbc0f92cef8d3c179f8efb66190ce66f18 |
|
MD5 | b637325e5e11380955dafb8d29cab073 |
|
BLAKE2b-256 | cc1fae845d320c1f59b5f99465ac8b8ee8d3a515a4c2266819fe7b9b0ed9d120 |
Hashes for psygnal-0.10.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fffea50e36e1ff3073bc6d8d8481d4d7707e298f9f379e1824de6dee71908045 |
|
MD5 | 63ede3674676e1784435e355b89808d0 |
|
BLAKE2b-256 | 4f625ffa087855df8e41658a1cb179fb3afa71efed862e0a7dfab3e602545014 |
Hashes for psygnal-0.10.2-cp311-cp311-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a453304c48f61613d82461cc6cfa390b972cf41db8f834d3a48ee90471cd818 |
|
MD5 | 47ba22f203adc1f6b6e7c920390c21b4 |
|
BLAKE2b-256 | c95972b8a3742186a4fce56114a0ae1df6d75046fecfa007ab17e1ff54e9b0d0 |
Hashes for psygnal-0.10.2-cp311-cp311-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7bd295bcabcce237bf5d2c05e51c946f83d399bedc92bd0a1d5ab8eb82b7b69c |
|
MD5 | 30d01ca74e40d54476d018f1bd7486cf |
|
BLAKE2b-256 | 9f190609d607b994ee9517906199866fb4c77c3b301663e9253fdfd1af96de34 |
Hashes for psygnal-0.10.2-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 09bc8121adf93cca7d5110900e26252ac3b3fe0dacdb96297e1b7d93b80c4525 |
|
MD5 | 4a235be4c5bc9e0c5e94c2333daa9a32 |
|
BLAKE2b-256 | 52a049d924aba2fa0640dc1c04d381f730f32bb019456da15127c1309109df38 |
Hashes for psygnal-0.10.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 55378d0eb4821875649da13d9524aad75542479e1f5817f44754622571ac4a75 |
|
MD5 | f3b462d2cb8369e6bfbc3cc378a85172 |
|
BLAKE2b-256 | 333cb308a711202a1bc2f538a9900193863bf14eb6474b4232fa1cd35940fa4c |
Hashes for psygnal-0.10.2-cp310-cp310-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a3fbbac9445871b57726d41e4d0262f738c6ecc60e3a48572f3fe5829471f1ee |
|
MD5 | 735cbdea9a8365e925c7a90603474d79 |
|
BLAKE2b-256 | 00b501e813e8155a4d88a3330496618cfb3f4cb545d0f50941738de1705e9ff3 |
Hashes for psygnal-0.10.2-cp310-cp310-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 004def7d5334dfad12b3d0dcfb7e16c6e3a948c42998eda0472fcc73bb5ae85d |
|
MD5 | 268cbb61614835b46d663a1bf08b14dc |
|
BLAKE2b-256 | c38c7106531209d9f93bf1189a7f08a76739e08ac7011384075ebf4c8a3b706c |
Hashes for psygnal-0.10.2-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a4738f8b53cd3ad268e474e4570f734ac4a23951123d14a1573e199c5294b103 |
|
MD5 | 171ac9699fdf80bcc18840e9448de980 |
|
BLAKE2b-256 | 7eeb34d066a2a1e30122dabe9364c45f20b2bb9e771117d111d91ef8b9c1232e |
Hashes for psygnal-0.10.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e56ef3992e3f09e76cacec79563ec4a65c584f86742fc099adbf7d2f61e94ef |
|
MD5 | 3622e2e7ce56270baef73982a3fc1534 |
|
BLAKE2b-256 | 1873c04e99517965640992a2e4180fc949de9555a0d68c12254276ddb4879c40 |
Hashes for psygnal-0.10.2-cp39-cp39-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e9a5ed1a1843e81928490919a09af33301fb9d7920195bbae13f050e4664e4ae |
|
MD5 | b8ff8ef296337a8be5992eca161c4743 |
|
BLAKE2b-256 | 350b66ac68145164b509191bdd00fdb79fcba405e5eca5c7affd99174a9a7d06 |
Hashes for psygnal-0.10.2-cp39-cp39-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b904424fb994e6c0088b3aab1d15305d8f27fa716c435dd5ebd5d58f1d6087b |
|
MD5 | fd3981f67a3831b084812632d3423a27 |
|
BLAKE2b-256 | 5f32373cd282a6a0cc23da2dea815c4e86403963ace9e70ff2afa40ab6d66dd3 |
Hashes for psygnal-0.10.2-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9cf48ef4be9ef473edb68fb89241a0d6ee787e1c4c8c81a19a41b16db90bcb3 |
|
MD5 | 4510c932728cf8144fb0e53070b5c19a |
|
BLAKE2b-256 | 11957bb56bd6ac370ffd4d12ab0c3fec3d4e8571f28c7968be5d2c78f5acdbfa |
Hashes for psygnal-0.10.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 09c6cdd85976c1d8b8064481ec581d72efaa52eeff232e479454c412260d26e9 |
|
MD5 | 3306e8a8a1d6e50f4cf1ae11e4f87ce0 |
|
BLAKE2b-256 | 242fef5fcf3a32a5f027eb007f60b1b70b9d20f314a02318bec3e34ee1564b0a |
Hashes for psygnal-0.10.2-cp38-cp38-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dc6a974700a1273fffe0ceecb0f4e331d41c570a86047f89383811c94d7b9157 |
|
MD5 | a2327115b5f037e187ffa3bd1774fa7a |
|
BLAKE2b-256 | 718548d496dde1bcca8083cc800e05e2729e3d623023bef292984c5e8319e6b1 |
Hashes for psygnal-0.10.2-cp38-cp38-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3ace1fa396190e3a08c8a8f3e9d59c030535987d59950da19589c6003a0b02f6 |
|
MD5 | 0cfd9f7f3b37c7ce46a92c8b7fd48f00 |
|
BLAKE2b-256 | 6dd3b77b8422341fb8d8216db19f781fea5b7ee859fc8ea3a52e7bd2b989f3e6 |