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.
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 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.
Benchmark history
https://pyapp-kit.github.io/psygnal/
and
https://codspeed.io/pyapp-kit/psygnal
Developers
Debugging
While psygnal
is a pure python module, it is compiled with mypyc to increase
performance. 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.9.1-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8835189685efdbfef4f737e846c9efe19331a72415154435546090f897054e09 |
|
MD5 | 4234000f5811c235f93c03db391696b5 |
|
BLAKE2b-256 | 72b5469df1b650fc571d1978626e985113c6e66818cd8085c6aceccfc069ee01 |
Hashes for psygnal-0.9.1-cp311-cp311-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 04278904521a242cf723d40fea42c2838af24c2664ac18e83d37cbaca2fe50fb |
|
MD5 | 282a6dd6734b6cfe2f498232049c3141 |
|
BLAKE2b-256 | 64bfac768da98b713cfa7e2b017c02785690bf27b586c445822cfd3d700cf939 |
Hashes for psygnal-0.9.1-cp311-cp311-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | be53aee8c112abb835099068230bd2761c7bb5b68a19976683b3784688931b65 |
|
MD5 | bb1f1ce5fccb82c1f1f69a3eecee2317 |
|
BLAKE2b-256 | 1d9e9fd1ea1e0bac3976fd613922ba0769e55bff445b2bd820fa4d20d168ed2e |
Hashes for psygnal-0.9.1-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc90228951b4a583e20264cf531538dbdcd206716cd0400c4b595ffcc648b428 |
|
MD5 | cc535b4912c2660ed8c5722e723e3ae9 |
|
BLAKE2b-256 | bc8ed1f7548d207825a331bc488e2d825b8ed00f6359130365e47024505a4d58 |
Hashes for psygnal-0.9.1-cp310-cp310-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3d688c36f162136142fada9b70095230bc2727de4f597808dad998dad8862631 |
|
MD5 | 649696204732a58b26d59566e5cc7691 |
|
BLAKE2b-256 | a5352c4a0af621daa9d7ee75ec969639d5cd5b5424a0b18f9ad4ec9812aceb47 |
Hashes for psygnal-0.9.1-cp310-cp310-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8bcf3457321f935c3c61d7d13fa0f9f7d83ea3accd4128f931783f7a60a57c8c |
|
MD5 | 642136209224b5ef7308a6907c38d022 |
|
BLAKE2b-256 | 61f354a7ee078ead2cecffa4d1d782db2580074b310dafddbc34fe3327774d77 |
Hashes for psygnal-0.9.1-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a87872ff495c50b9ee75ea07da826c88a4fb0b5f53a6ccc172e6e386ae62d392 |
|
MD5 | 948aa194886ed6e7f6b306d286d20754 |
|
BLAKE2b-256 | 68dbf90db6ad27c9456f8f0dd83d5b671bb72fbef2d416b29d81aca67288cb02 |
Hashes for psygnal-0.9.1-cp39-cp39-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cef8730d171354edbccbededb6130149c9d78329aff441337c6b28f34f18f03c |
|
MD5 | b926ce27c288b879945afec9089c5514 |
|
BLAKE2b-256 | e0a1f41b8aa5a2275ed52c449875174bf3f9e8ed81c6349b397b155da65acdd9 |
Hashes for psygnal-0.9.1-cp39-cp39-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6d9a89b06ac5cdde26ed0b14349197f6629497f1c9a51d9e5bda0944ba477cde |
|
MD5 | 3e289131dc30da970333d3a4702e786a |
|
BLAKE2b-256 | e46e75163a962f49358313718592fae47caa595b4e706559db5347290dd019a1 |
Hashes for psygnal-0.9.1-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dcf6eb54af7f14f073ad861c240ba60afde05ac223c49f812af1e2a2f82863f7 |
|
MD5 | 81acc113c76d083a27d6973dbb2fc13b |
|
BLAKE2b-256 | ff085a2d94b155b25fceb92926aac839829240c55a3aa0ce7052744354a1018e |
Hashes for psygnal-0.9.1-cp38-cp38-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad0fb97d6814f040e9ccde42521790e84683461b47bf5cb91b528cb2b21ed9da |
|
MD5 | 19221c98dee43cba5ee7e0d18ee88ee0 |
|
BLAKE2b-256 | 1943e81dfbc7ecfd7786a03b741cb07f53daf2b6882ce9ba97a977433dd2a27c |
Hashes for psygnal-0.9.1-cp38-cp38-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ed8985721f4db898d0ce2fd2a354be6675198490b65499a8d8272b0a361b5cc |
|
MD5 | 18e1651b96d07a5b991cac01e1d0d919 |
|
BLAKE2b-256 | 8f6b44f7aed0bc99c7a2e7988c927f8ed0eabdb22cee57e37dc0ffb9f769b84a |
Hashes for psygnal-0.9.1-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40b1747c6074cf88ec140abb6dbf99145763196a1f7cf417b37845ef66423ae5 |
|
MD5 | 661aeb2ce2fb1c7260274189525efe9f |
|
BLAKE2b-256 | b6d7f5460b8c23f941dd24fb158eeae12e9cda4f253594afac0ad5964bc83411 |
Hashes for psygnal-0.9.1-cp37-cp37m-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a0b28b51635c0d7c44246770f7b516953db1f7703352662bd34e2aee0c5b533 |
|
MD5 | 375598f98a5424e9ef01bcf52dc694fa |
|
BLAKE2b-256 | 6bc69a2adfd5d6f4509da4ecae1af396cd4c3cb100a1454b2b85ca3b53ac09f7 |