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.11.1-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c2388360a9ffcd1381e9b36d0f794287a270d58e69bf17658a194bbf86685c1 |
|
MD5 | 18fdd3e2840f86ad36d978398e2fc3e6 |
|
BLAKE2b-256 | 1454b29b854dff0e27bdaf42a7c1edc65f6d3ea35866e9d9250f1dbabf6381a0 |
Hashes for psygnal-0.11.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 885922a6e65ece9ff8ccf2b6810f435ca8067f410889f7a8fffb6b0d61421a0d |
|
MD5 | 0c320e34a78de522d736476e2c8713f9 |
|
BLAKE2b-256 | 49ad8ee3f8ac1d59cf269ae2d55f7cac7c65fe3b3f41cada5d6a17bc2f4c5d6d |
Hashes for psygnal-0.11.1-cp312-cp312-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c7dd3cf809c9c1127d90c6b11fbbd1eb2d66d512ccd4d5cab048786f13d11220 |
|
MD5 | f9779ce01a14f5f9e4899a020fd28a3c |
|
BLAKE2b-256 | c466e1bd57a8efef6582141939876d014f86792adbbb8853bd475a1cbf3649ca |
Hashes for psygnal-0.11.1-cp312-cp312-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b55cb42e468f3a7de75392520778604fef2bc518b7df36c639b35ce4ed92016 |
|
MD5 | e26bfc9d64f719b40e65535e149583e0 |
|
BLAKE2b-256 | 335d9b2d8f91a9198dda6ad0eaa276f975207b1314ac2d22a2f905f0a6e34524 |
Hashes for psygnal-0.11.1-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d77f1a71fe9859c0335c87d92afe1b17c520a4137326810e94351839342d8fc7 |
|
MD5 | 5ac5bc5f410769ff029746167f50c6ed |
|
BLAKE2b-256 | da7d24ca61d177b26e6ab89e9c520dca9c6341083920ab0ea8ac763a31b2b029 |
Hashes for psygnal-0.11.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 24e69ea57ee39e3677298f38a18828af87cdc0bf0aa64685d44259e608bae3ec |
|
MD5 | 5b885a1e1b5eefb7db503479d29fe823 |
|
BLAKE2b-256 | 846f868f1d7d22c76b96e0c8a75f8eb196deaff83916ad2da7bd78d1d0f6a5df |
Hashes for psygnal-0.11.1-cp311-cp311-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8f77317cbd11fbed5bfdd40ea41b4e551ee0cf37881cdbc325b67322af577485 |
|
MD5 | 9e7ca3576ea396df53660ce7c727785c |
|
BLAKE2b-256 | 25926dcab17c3bb91fa3f250ebdbb66de55332436da836c4c547c26e3942877e |
Hashes for psygnal-0.11.1-cp311-cp311-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3c04baec10f882cdf784a7312e23892416188417ad85607e6d1de2e8a9e70709 |
|
MD5 | 72d6c4ed40b33585324541d2af1c97c5 |
|
BLAKE2b-256 | a6a8ed06fe70c8bd03f02ab0c1df020f53f079a6dbae056eba0a91823c0d1242 |
Hashes for psygnal-0.11.1-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c392f638aac2cdc4f13fffb904455224ae9b4dbb2f26d7f3264e4208fee5334d |
|
MD5 | c70f68f3d719056e30b6094d23ce6fd0 |
|
BLAKE2b-256 | a5938d91aef01261123640406d132add52973e16d74b6c6e63b6fb54cc261f1e |
Hashes for psygnal-0.11.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7676e89225abc2f37ca7022c300ffd26fefaf21bdc894bc7c41dffbad5e969df |
|
MD5 | 61dce6938f385e9d711a63e043b7dfe7 |
|
BLAKE2b-256 | c33fae610fd14cdbae8735344abfc7f67c76ff8bcf18e0e3c5f26a1ca590014e |
Hashes for psygnal-0.11.1-cp310-cp310-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cec87aee468a1fe564094a64bc3c30edc86ce34d7bb37ab69332c7825b873396 |
|
MD5 | 88a59d69128bc8c73a1165b4d63d898b |
|
BLAKE2b-256 | 660a52b7e40f4c7ec82c9809c62e568ee9c117dd911d3f6f562ac3007a4ad969 |
Hashes for psygnal-0.11.1-cp310-cp310-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d9187700fc608abefeb287bf2e0980a26c62471921ffd1a3cd223ccc554181b |
|
MD5 | 14af5938b87f1c5e3733b533ef49a241 |
|
BLAKE2b-256 | 92bf2dee9491518402489909c0613004d3a0f79672f27ce16aae774c5addc506 |
Hashes for psygnal-0.11.1-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 09c75d21eb090e2ffafb32893bc5d104b98ed237ed64bebccb45cca759c7dcf4 |
|
MD5 | 46b02b9bea248e9fea559eb6f53ab6ef |
|
BLAKE2b-256 | b5e3ae3b178f0c0f9528a9b957f302800d65ddc6fcd47f18724006de6414fa85 |
Hashes for psygnal-0.11.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 713dfb96a1315378ce9120376d975671ede3133de4985884a43d4b6b332faeee |
|
MD5 | 948d225173a2ba90dd95f786866e5bc0 |
|
BLAKE2b-256 | bc0e8bfb65ad186f36a52b2bfe6193f37f3b792f548d1ccfa302b5859bd8c648 |
Hashes for psygnal-0.11.1-cp39-cp39-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c05f474b297e2577506b354132c3fed054f0444ccce6d431f299d3750c2ede4b |
|
MD5 | 59d72615caad62c96e7a359691d5f2c5 |
|
BLAKE2b-256 | b0f8db318ba1b1e1e31455e62b83fcf754a97d061ab59a3e1c11c612abe57e48 |
Hashes for psygnal-0.11.1-cp39-cp39-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c9dde42a2cdf34f9c5fe0cd7515e2ab1524e3207afb37d096733c7a3dcdf388a |
|
MD5 | 44d00201b679bd8657dd8f82e644207d |
|
BLAKE2b-256 | 05bd134c50dea67e1adf510e89c055bc69ea1e6487dd68af10840c9443a0988d |
Hashes for psygnal-0.11.1-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fe70023fe4cf8bb6a0f27e89fd8f1cf715893dfb004b790937a0bc59d9071aab |
|
MD5 | c283f9f7740469c3ff1e644422d0a2be |
|
BLAKE2b-256 | 2276b0d4f0eaadd0414755e91d8d744f8474519ee13926543dce969b9a5bba62 |
Hashes for psygnal-0.11.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dc260f19349485bd58e276e731cf8be40d8891cc6ff1c165762bd2c1b84f1ff7 |
|
MD5 | be1f957bcc3db2fd7479d2fc9150b5dc |
|
BLAKE2b-256 | 4fb47c94454dd1a9d9b5980d9b8053b054c1f246a3adf433fbde691974e8cf76 |
Hashes for psygnal-0.11.1-cp38-cp38-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 36cd667dd1d3e70e3fd970463a8571436e5ae58f02cc05a4a1669e6d8550d263 |
|
MD5 | ea4e81f8018650abfcb179699f17031c |
|
BLAKE2b-256 | 33a7fdfa16c98b45b5823c383bc1fe5e038af01392331aa1a4e8f3a976633cc9 |
Hashes for psygnal-0.11.1-cp38-cp38-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2deec4bf7adbb9e3ef0513ae8b9e98bb815eb62b76a7bf1986f1d6ed626c8784 |
|
MD5 | edb51c3cdba06cec899c4fa6b6e94da4 |
|
BLAKE2b-256 | a0144c3f4b9c5e723b7afd741dc529fa350d2afb584ec0ef8eda7f70436d3831 |