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.0-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8897e84c34388a45b4c68a22c363e349d76be581711a30b6478fbfb9911f1dd9 |
|
MD5 | 40d206828c84cf838cfcceb8db0be9e6 |
|
BLAKE2b-256 | 0f825a5f0224ab676b986b3b88dea02a12164905780e66526d2712fc3ddc3334 |
Hashes for psygnal-0.11.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e153bd1e5d7a0970693dc23c3122169994b03f3e2a4e9de42717e53d4fed1c31 |
|
MD5 | e42ea97bf71c7533c149e68fa86f98a4 |
|
BLAKE2b-256 | b2941599fa4cd60931c7d2b2e2b2c916b7381722c0f4fbdf6024ae4566a2cb15 |
Hashes for psygnal-0.11.0-cp312-cp312-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c9423f1734ce4aacd8d068225d8b1ccfe4fb5f4472bfa05f4f5b76317c7124bb |
|
MD5 | 370c4ac05b1b874cf2fa3cec69ed1e3f |
|
BLAKE2b-256 | daef70e8c93e39912fe13d28a5a29b18d476d9dde945755824a728f2edb98cd6 |
Hashes for psygnal-0.11.0-cp312-cp312-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c2a41669d010cb22ed8d38a5e9c982e0949797af48cdc2ec9d29b774382c1e9e |
|
MD5 | 26bf0f9f55090a867ea166b1298797bc |
|
BLAKE2b-256 | 338f45997c85eb60d1967eaf80849dd2b3f52b40480aed6d640dbd01f8cf4b8c |
Hashes for psygnal-0.11.0-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b87466ac06b5967be25b9eb6e85fa51133670e01370ddbab857e9ac9a5ba30fb |
|
MD5 | d4370b1393adebcf9a984f56e2bab197 |
|
BLAKE2b-256 | f5d0886bcad3f4bfca2034b9fc70cfa1e7cd6d1eecd5ce0a79811489d4117858 |
Hashes for psygnal-0.11.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e20ba244394a191b1faea0345c321c4819e8804a5cbad3792694475988ae23e |
|
MD5 | 162938f67d540bac0a1675bd8436e84f |
|
BLAKE2b-256 | 5bf4c75c4daf3827249bce1877a0222af722e203a51965e490870ddaa818d172 |
Hashes for psygnal-0.11.0-cp311-cp311-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a841376635af537af63197ed282e4688df19911cdb58d12f04ff528b4aa24dc4 |
|
MD5 | 1b0d065d6644a048ebbc555f7b75e20b |
|
BLAKE2b-256 | 01706b81d8fd262690eb6333456989520d216ea92dc4ecbdf0c00556b1b4a2a2 |
Hashes for psygnal-0.11.0-cp311-cp311-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 698b1fd559aba2eccfada1a6d41bef73875a0093f62f599b1915f0820ea9bc0b |
|
MD5 | f7a6021bb8f52d830375920cb7599f48 |
|
BLAKE2b-256 | ab86d9cebae77fad93c6c19e4ed3371168ac719f38942e8723a42632e51c9e93 |
Hashes for psygnal-0.11.0-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9df6e0625acb9820e076a9a2eb456d8b754ac998ae0c247d13d176306edfa6c5 |
|
MD5 | 1e4c9069140d49ee04e1a501a23a3955 |
|
BLAKE2b-256 | 7358bc6d2f6047b6339c24ce41d925a8c05f84e05d76dcfe84e34749e5e30662 |
Hashes for psygnal-0.11.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 353e136cadd61fba2a43bfe6a7ed02bf3475766beaed8439cbccc191e600a75e |
|
MD5 | 4e714e0fba0fa8f6a6ca051885523097 |
|
BLAKE2b-256 | 342915038a155942352e87a5ffe296f9194e8c3377d9a760b3e0872925f74767 |
Hashes for psygnal-0.11.0-cp310-cp310-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c5ba0cd1d1544a3cc622ad40d41a1fc567b9415cec01204286ce75bd83d91a61 |
|
MD5 | dddc186c5e4287ee7d03186c259aedea |
|
BLAKE2b-256 | 80e4936cf2f0db5a4cc8ceaca980fa6235580f73dd0bfea943f5cc492abe8382 |
Hashes for psygnal-0.11.0-cp310-cp310-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ec29354483a4771864799d1c680e75b0175b18af3bfdf3ca57009c26ca41d3a2 |
|
MD5 | fced17690b62fa7c8948a994e3e40202 |
|
BLAKE2b-256 | edbc95804c24612b689b2fd008ff0b26004711f359a2201b4b70fc6432140393 |
Hashes for psygnal-0.11.0-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1cd43e31333982833dbfd51364b6b1a2f1906a1f1372d033015c0692912eeb47 |
|
MD5 | 195130ebcde21fa73bf8f031f406376c |
|
BLAKE2b-256 | 69774d686e64d0907cc3e6cd39ab9514b0656d8516a11bafac29c1b47b3d0382 |
Hashes for psygnal-0.11.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e4b0bbac69dacdbfe9e12b2db8fc517ddeaf8b0c7e283f5d16eb1510eb7ad0ef |
|
MD5 | 9fe70d9e36d8335584f202c4dfd998cb |
|
BLAKE2b-256 | 232249cf6f2cf2924c78ee2d954c2cf903eb2fea8caf32331600826e5af0709e |
Hashes for psygnal-0.11.0-cp39-cp39-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 50677d18e181cd6f8c80d5bb911ef5ddbcdbd490801d7205a0cd9e7bd587f163 |
|
MD5 | b83b88525081df41e293bbcd65a300f2 |
|
BLAKE2b-256 | b2f21eb651dea2e1358efb57ff6691bf3014e2dfe5ad3eb562c82dc6d49123b8 |
Hashes for psygnal-0.11.0-cp39-cp39-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 036499cec39743381ed4f4426af0800d73b0950fe93b7b662b1bab2d0ae5577c |
|
MD5 | eaf9b228cd1586dade372728fe4bf948 |
|
BLAKE2b-256 | 530c58cf45b451428e83354e5383ec570a85737e15c0585b1adc026f0e675168 |
Hashes for psygnal-0.11.0-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | db3fccec88ccc0f1f59e40ca9f29aa539b557125aa23fb98781717fb37f16ed3 |
|
MD5 | 1ef9a206359d8d9b85c447dcc8bf46e2 |
|
BLAKE2b-256 | 4ff8bef67b011ac05adec2732dc87e6e4eb63c356d31833a00acdbbb30f9a7e9 |
Hashes for psygnal-0.11.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ecce1d07aa8715c9b25cbd0dbfe12bae6b80099cbe20b4a4d4c356839750d269 |
|
MD5 | edf7431d06ee99a02ae00b9299855f2e |
|
BLAKE2b-256 | 4eabd117a8d6f8073bb3b6d696108e37fd34eb88c5b2db98a5c6d6e0fc710546 |
Hashes for psygnal-0.11.0-cp38-cp38-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 33c270a7877d9a4bf6daaec19f048ad8573ba0b6adea4fba45efed97f26bf0de |
|
MD5 | ff80ab466020a53d8f375b4646061f1b |
|
BLAKE2b-256 | e6851efe6ec0baa0444e59c5b427946edc1379251aacfd3bfadd53c572e39045 |
Hashes for psygnal-0.11.0-cp38-cp38-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 309d872eac9da37282433d2934beebb316a4272b828bd857159f210608d1dbc9 |
|
MD5 | 81104e05b93d03a6005e1c1aeb57eb63 |
|
BLAKE2b-256 | fb7924c9922bc05348642ee5d898985cb95d4bc0ada80cc74c95d8310209f487 |