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.0-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 35c3b54f7783d47845e56b2dac6f68b87adeea1bfed068fc4456ba4c9cab0e26 |
|
MD5 | cb93aea9970cb40a65c8640f585d5075 |
|
BLAKE2b-256 | 964a9ec647abf2d63f3c9a449f907f9cadc5b3a31e328c85f1822a4353055c93 |
Hashes for psygnal-0.9.0-cp311-cp311-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 248021f9b352ac617b216b3e2345174406b510d02bda276ace04696b678286f8 |
|
MD5 | 2f911425744a5899107a3bd2024ea571 |
|
BLAKE2b-256 | 9f20708c5cbb2b1ef366475fc791b44bca0195d2d6a4cfe641a3866c9eacd22c |
Hashes for psygnal-0.9.0-cp311-cp311-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 45cb9900d8e486a0294328f0768691d63532d542afe59a8c2186c55ba8d0a662 |
|
MD5 | 68f7b4a2a10f60183bc23089aae5e689 |
|
BLAKE2b-256 | bb49dea7ff2cbbc915fae08255d60ce4256f81b35e7e29e8ae54717ef651dde0 |
Hashes for psygnal-0.9.0-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e200063e6f373b3ac3702dc83e3fa8ee1ddd6f31ddcbf90df50d1875221343c5 |
|
MD5 | f45be57261da331ce1a2d5d1cd90d430 |
|
BLAKE2b-256 | 965df9aefe9a3d28143037549d5deb7f64b3772cbfa2f50e0bb41c7f052ce3a6 |
Hashes for psygnal-0.9.0-cp310-cp310-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 823546c1bdb4b048acec2f128508ff65e3c657f1ad116373bddd959d11017625 |
|
MD5 | 08b8ccdd3637ee4ac986bf06dbd0eb91 |
|
BLAKE2b-256 | b74f7447898ba0be4be04f5fe546507a42755010e2de9360146f25340231fa73 |
Hashes for psygnal-0.9.0-cp310-cp310-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce059b1bf01c57759f1caf655f422901a1adde73550d150b189f783702bd70d2 |
|
MD5 | 6df3555bd9c3f76ef58ae66504f4e34b |
|
BLAKE2b-256 | d8e8e9f2f73d538f83a31cc882a6f709d76457aa27aee3b48b5e8283f15137be |
Hashes for psygnal-0.9.0-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2927cd816e8b884f114126d672043100a2ad8dfaf8e53aedfcda4575af671dbc |
|
MD5 | edd295246cfa0fab1478d86f6af6b73e |
|
BLAKE2b-256 | 0d678e02a2206de0cafb9c48208e61085f25d3bc6dcf96e61efc937ccea30bca |
Hashes for psygnal-0.9.0-cp39-cp39-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 46ab9e13007c6952e468b70875f8e39a8163730835a32f90eec0a1f1be183a40 |
|
MD5 | 7d5cb17e61598181e65fbbf399970f77 |
|
BLAKE2b-256 | 15bc9339f6a30687d68045a24da9bcddc398ee3fc5eb97cd09f01a897af9c89c |
Hashes for psygnal-0.9.0-cp39-cp39-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 82ac2d6a4c451538741e2a09800730b2830b3ba2aed4a61384f58b647e9ae685 |
|
MD5 | 784822e5835259c5f549a9e8f85d3374 |
|
BLAKE2b-256 | a7a2b15b071a310e5c1d5f8b3f43c47917f3520343addda1d4906cdfd501656b |
Hashes for psygnal-0.9.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b7451baeece17de009e0c808f338c9fa96d7225edb1c60a1e28e8c900b76980 |
|
MD5 | 1b1a738d6b0d07e29737ffe2fcb06b0c |
|
BLAKE2b-256 | d50ff99c4c62ab0671d034028697d4daa1756de4b9f04ad484e4e0cfd6fce2c0 |
Hashes for psygnal-0.9.0-cp38-cp38-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5875dba61ad96949d1e13e0056d4742ca3c5442fbd7ad4e57f0e59a5a06ccd2e |
|
MD5 | e298fd95df0be7bde343d63306cc2dca |
|
BLAKE2b-256 | 633e50c2225e4e92d5ea7fe54bbc459d11048376dca5d107b6adee35a46f2b6a |
Hashes for psygnal-0.9.0-cp38-cp38-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ec7446bdbb518562a840902ef6ab2deb899d2af7757209f45518cb87b11d9a04 |
|
MD5 | e0cfc99e155947bf703c8cbbcd6b3da9 |
|
BLAKE2b-256 | 1d47753ba7175204457a348c97411d42e15014ff8623bed4b86428f3ce581b30 |
Hashes for psygnal-0.9.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8ccd63cd4290626e354898f789c68afa306fcf47c813f8d8be99adbc18b62f14 |
|
MD5 | 862854539a298f65fdbd958cf426bacb |
|
BLAKE2b-256 | 5fc8cd96d89e6da7147b4c29830b708be2e8d01cbb303e326048ff4ac75221d6 |
Hashes for psygnal-0.9.0-cp37-cp37m-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 72c8f2e683f353f80af5aeb4c93bd300c2a4f9c2236d6b8484fb0eb682d5fb8d |
|
MD5 | 1d063cacb191ca3b8cf39f6fadcc9377 |
|
BLAKE2b-256 | fd591b505e12571aad331013d7257df2c1dc9bdde7adbfcd08bdae3c1d1e1cb0 |