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.0rc1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a658a1605146b881fd2385ae4764a536095fb304415c62aa348ab76a3ef3514e |
|
MD5 | 6aae79d29cf4440e99a6ee17a7932782 |
|
BLAKE2b-256 | f1f64a7141cd5e0b5e9adbf1993fec85c019c185a754e5de83db85dfce0da7b6 |
Hashes for psygnal-0.10.0rc1-cp312-cp312-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e8899080af7dd97051f6e0b3c7b1b8e150ba3cc9204813c928a4aa3f10dc585 |
|
MD5 | 5f0e1db127751d7e09c6cd11d8fc7db4 |
|
BLAKE2b-256 | 08925477725ed430535bd159d1cc21f296038c3d7be3c8328cbdf52487a20dee |
Hashes for psygnal-0.10.0rc1-cp312-cp312-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8e7be3a26cbab3a964a0060f48f4eadabe525e59955271285a23dbd4d9dbe93b |
|
MD5 | 6e3e87c61335bd8e68ab6283f4113ee9 |
|
BLAKE2b-256 | 4b94585cae7c3876c90505dc298843a5559aea42c87116551ae869077f7b2d78 |
Hashes for psygnal-0.10.0rc1-cp311-cp311-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fa3412176c6b46756af8b6377fd907a60c6c26cdfaeaab4fc20aca53f2118c36 |
|
MD5 | feea72cc923bd6ef23aacc7d3302893c |
|
BLAKE2b-256 | a3b989f9afe430b537c77a11580ce09f0f40eb565553434e05659a968ab20500 |
Hashes for psygnal-0.10.0rc1-cp311-cp311-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 153e7f24665662f489b105ba3d9469c2f9338dbc2ce80530fe4bc01cba62ca00 |
|
MD5 | 2362353ea781387dab4829584c95f0bf |
|
BLAKE2b-256 | 8cb89c2c09df9b94acf29d4dfe4af1d08c6f9835859938bace3460049691c5af |
Hashes for psygnal-0.10.0rc1-cp310-cp310-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 22d1807a404b3116dcb5a206c71ade787819a25acd0613e6194f46f61cb26f42 |
|
MD5 | d7c431e1b48683e78b8551efaef7390d |
|
BLAKE2b-256 | 305c2a67e6e4f7ea36054a9ed8daf6954009f2019302f954311724495879407a |
Hashes for psygnal-0.10.0rc1-cp310-cp310-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4ab55b7ac4d30fc96ca6eae8c830a1ab52c94f439645594bd30cfc270b152ac2 |
|
MD5 | f5472c0ce384e25854eada00dd5117c1 |
|
BLAKE2b-256 | cd971302ad99f3fdfca3925aacc2a6554feca161ba5b42e07cce9091eec3537a |
Hashes for psygnal-0.10.0rc1-cp39-cp39-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 65e7c73344b8419a22a76caf00f8601898660e9766d05fdaa770d28c4ac03a66 |
|
MD5 | 8770d291d6a133121627f1dbf178e414 |
|
BLAKE2b-256 | 856d4378f6dd7f38fdf55e22f4935d588e49dee9d0e0cf74059e01c0c4bdd609 |
Hashes for psygnal-0.10.0rc1-cp39-cp39-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ff3301c4df2967a1a7de1377aad0e817e816ca9b388c30259c59126258b64bc |
|
MD5 | b180e675477d65a96f4814f05600da0c |
|
BLAKE2b-256 | bb8f509c3a26d499acf1bc514b7f6cc6c70e42919a6a0ae6795201a41bd7cd57 |
Hashes for psygnal-0.10.0rc1-cp38-cp38-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e4efaa934a57819a4ef5568432ad7d237894cdb519e9165065eff64331f0c32c |
|
MD5 | 175e37a0431e34c53ca1d0bf36664725 |
|
BLAKE2b-256 | d1da3f4e7146649ec495c615a64955a7b56fb9083158a22d0e08d146a08718de |
Hashes for psygnal-0.10.0rc1-cp38-cp38-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 450c2105689efd385addbdb744ac145af7472a2fea336c44fbc988c0750668b4 |
|
MD5 | 697f74d1f9c3251a64a97b5c31d7aef3 |
|
BLAKE2b-256 | 5f40b3d9577c36b2fa0ceafe5c33a1cd28ee5b0c81f13289491afd7444c106c2 |