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.1-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b238824e2739512f26f121e921af211360bc6faf233bdf09d9ffc202a57b284 |
|
MD5 | 074819ab02275f6fd0a9a128af881fbf |
|
BLAKE2b-256 | 84b81dd649dd3be40e2d3a998821eddce4a0a2c26aa21c1ec79a5b22dca38569 |
Hashes for psygnal-0.10.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 793eaf8edb01514075c422daf0ebc593235ee6b6d96a27196ba6dceb2bb05e1e |
|
MD5 | d7ca28db8fcb96bbcb457cdeb7608b4b |
|
BLAKE2b-256 | a1eb837d83aaae3a83cd78d63ee9adf9880f918ceae5f22b23b9c342a40d7390 |
Hashes for psygnal-0.10.1-cp312-cp312-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3253518448fcb288399cd1ed6f7876bf110d67d6d3f427b720620ec1f6b72bba |
|
MD5 | 013701167da30484ae282e972c14fa37 |
|
BLAKE2b-256 | 51d92722c8d634ca2e19b312239f232ebb3d4994c3d8e17276e8415a214722d5 |
Hashes for psygnal-0.10.1-cp312-cp312-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f10d1d2bea4bbb3639538a3f85794359728833f85ce2ad92a1e04aa8712a01c7 |
|
MD5 | 294cff01b2cbca719157e8aee2cb74d1 |
|
BLAKE2b-256 | 86e16569157aee016c8c90ee8bd50f30447b787e09fac4027bc1997e535ce9af |
Hashes for psygnal-0.10.1-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b1a315e373040c3da451fd6ce32e2ae1bf72b9e83bdb2a36eaaccdd9b8a4ee17 |
|
MD5 | c7e5f7f1cfa074b5ebd774813feceb23 |
|
BLAKE2b-256 | e70a8f29d00d150ca4ae3470249b7cd513290c34421a6f39730e3d4218876d59 |
Hashes for psygnal-0.10.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e8e07ab0295848f5669618201c6ee71b27623d96cb5a5ba4a60992edf5807834 |
|
MD5 | 3610d1582bd7a28212d9c03cbc59ac22 |
|
BLAKE2b-256 | 0908f299cef473c0011a1e51ddba159752f9d7373f15d306cc20f1a27c9bbfae |
Hashes for psygnal-0.10.1-cp311-cp311-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 496e7bf61dbad3a1334191dba3e3e14612958052b1d19eaec2d79caa6729940b |
|
MD5 | f9a3e09862c7a3373913b8ddb11a32eb |
|
BLAKE2b-256 | 42e9507d3589fbc0764943f7c8bbdc14a9c55145ed250cdf274237e3b997abf7 |
Hashes for psygnal-0.10.1-cp311-cp311-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 063460578b89807d0dfba2e9c362127590ffa34b9357a42e147abed187013e3f |
|
MD5 | 52100cd48980857c0d5940d96869c9c0 |
|
BLAKE2b-256 | 4ba219a4951a4f925a98f1c4e2d003d11586433eba715f48af323e09afe7b796 |
Hashes for psygnal-0.10.1-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b91329eb39f4e2c0cc668bf486ef41fab4ac975b542088d365408f90af3b976 |
|
MD5 | cdbc29cd3a7b16318fb5686bf686f64b |
|
BLAKE2b-256 | e4174fd806e6cd6ff7509d8d3ff48590e9b31c7626cbfd2909233051ede50b70 |
Hashes for psygnal-0.10.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | daa5950bfad9b5afe3f397930bc6969c11c6adfe57e3fc3455ef621e3de210db |
|
MD5 | 0a92d5480efb868baaecdb0e5f859f46 |
|
BLAKE2b-256 | c8966753e0fa5c5be09fe2d0804c62dc76278f60a57942fc2383ff8202c985b8 |
Hashes for psygnal-0.10.1-cp310-cp310-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dfc0f414aa30f713aca3e55221bec8cbb9b17ae83ca1c1b4320ec43cdf1bae21 |
|
MD5 | 5e4b8f2d4822013c3849b3787c2b7a41 |
|
BLAKE2b-256 | 7d7004b72e36e1e2cd4595a708331ae124e61b27f5f5ad80e1c4a17a1a722a69 |
Hashes for psygnal-0.10.1-cp310-cp310-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d2c21860a44f35f5432dfaccbde1f6df82976d8a1dfad04e4a5948e9482393af |
|
MD5 | 15adaafea95bcc461ef8696bce18d843 |
|
BLAKE2b-256 | 772b33b6de650029be0f67b61ce656a8830f7b6c7ccf3b1630aba371d9b12497 |
Hashes for psygnal-0.10.1-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b371501a00efcdb479716300ec9e71fb3e494f383e3c1309070fc2ea22e07a7 |
|
MD5 | 6996d6c4259d2a2cefb0172c2f6ca809 |
|
BLAKE2b-256 | 42f3b8cd4870575fa64ed5b5c7582453f014bf64e58cbb56c69673ae44f85a4f |
Hashes for psygnal-0.10.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc8e455c74abe5039b4522ad4c5714b96e99db73124ece1112c9ab4b10127a0c |
|
MD5 | 9d023a15d0275867b5138e0d0c1267d8 |
|
BLAKE2b-256 | 28383a9f87468c187c6521f8cd8e554456d7349aef8c62b6b940ff0c374c7745 |
Hashes for psygnal-0.10.1-cp39-cp39-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2159589b19489be97456ff88aa6e9c5aa930a57f766e9ff9674198b13d9700e2 |
|
MD5 | 8e7a06f6e565d00355565ed9c50977a0 |
|
BLAKE2b-256 | ae64ba48fd06a1857f2972ea5ae61b61fbc8a8e91ec61c8a57630e4f3ea42657 |
Hashes for psygnal-0.10.1-cp39-cp39-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a6752f9ace2f21da9da51d9c0fa84b1111ef1ee36e65349da0286cd82a726e80 |
|
MD5 | 44e18c3ef136c3aa4b3342ae2ef2961d |
|
BLAKE2b-256 | 21e49f4c76f8806c09dcaded001a3538d48a218e269c47932ec2c368c341ed20 |
Hashes for psygnal-0.10.1-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 39f500cca6e323640721ef44265940cbe2037139846bf4af75633bc211918115 |
|
MD5 | 39d45914d59c22f1d0e74be259e557c7 |
|
BLAKE2b-256 | 22a1ed32bd9459eb6ba31cc68f87636e4441a0e7b3f1b3137c0620aa80391c8b |
Hashes for psygnal-0.10.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 079ba217491ca6d052a85609b376c131646f007df8b320fc1071d33c7ef19f5b |
|
MD5 | 225d08cc405b2a4e6ca7eb7b6b3c1c6b |
|
BLAKE2b-256 | ac71b45a3ac7f0a24118d5fa9f375d463a7767db6e829f93dd96f91e2fcb8a7b |
Hashes for psygnal-0.10.1-cp38-cp38-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ada36f3c12ce7b3c06012aa39da7a44e87cb2a31d1bad9afa7832f93324b4e6a |
|
MD5 | cd20ad4a671e529b63aa8f7dcf93545e |
|
BLAKE2b-256 | 0bc82c8d19978683c96f6e00f80f0af1c39ebd05c66566131024cb6db213de65 |
Hashes for psygnal-0.10.1-cp38-cp38-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b8cf76baf9a3a1c3206618dad57275cdb97c0e5236e06459802509dc699d87ac |
|
MD5 | 589ac2bde6222177dbb13cd1d91cb84b |
|
BLAKE2b-256 | 922495c53651a25e581fd2a75c94ca400675228410b34c20082c01690c35c48b |