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.0-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fd3babafae634a69ef84f27fa45e4c692ea98bbb9abd35ae777fcc9348a9c577 |
|
MD5 | 86057dede4378539d937bda424f96abf |
|
BLAKE2b-256 | cead98f769e65c4a8045f75140cb03b677e0f899e15a2d0e954c9667b164cf40 |
Hashes for psygnal-0.10.0-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4a21fbae9f65f5ed36be8f893379d232bc092c8fab50cf7b11e0e3002764ee6a |
|
MD5 | 3670ee0a5cd2c07d799ec5b699b966ad |
|
BLAKE2b-256 | c8bc746f943f8fd1a71ea6856ff325b23436ebd2ab9f72142102ffb34c2fa4bb |
Hashes for psygnal-0.10.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | edd49646a2584042ec3cbc4703603fe2cbc04fde9b9c282dd75a3ffae1b81538 |
|
MD5 | 25a7a27cf54c6dd816bee4e7257f8661 |
|
BLAKE2b-256 | 6efefbc118637a3eb98d83f0d7f395a2543f5f9145dbaab5250e0068d3307687 |
Hashes for psygnal-0.10.0-cp312-cp312-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c1cd6df685d28bb5955ccd678c279f30f1de3b3f7006d6dbfe740dac7a116bfa |
|
MD5 | f76c98c502153a6a374cf37c692ac53a |
|
BLAKE2b-256 | 404fd881547629b763e028f56581077abaa210b2097c3e7a80357b88d7d7ea6e |
Hashes for psygnal-0.10.0-cp312-cp312-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb36be73adb666688d7176bb17ee99e9cef4186dfb6e41cee56284fedf3df95b |
|
MD5 | f18611513f13088eb712c3eea7e057b5 |
|
BLAKE2b-256 | 0ff4c3aa33a73aeebbf7ed9d803cfe14b8d5737bc9a1a2f673c8f48b9f12ea8f |
Hashes for psygnal-0.10.0-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 034abcf9ef85c79827028e41659e5b7a23eb247fd4b8401b34d956f4c02afa79 |
|
MD5 | 11d86723f1b4252e43689818bfe23fa9 |
|
BLAKE2b-256 | 807e29cb1bd3d1941e916713bc9651e14f11cf7f9ffdb53d82b31d2e5f2fee1e |
Hashes for psygnal-0.10.0-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 956cc402bb8c4fce13f80b9b57e2e4422012997a5be4c10e9b84c9f1ab598f9e |
|
MD5 | 34767cb5398fe3ac2d42325226d295da |
|
BLAKE2b-256 | 11294d84bd20c58530a9179cf861170ac6782bcc4b5e501bfc06cf45b1a964e9 |
Hashes for psygnal-0.10.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b82aaa6cd0880276136a88ca5ba74ed994dd4465ca22450de14bb42fc262775 |
|
MD5 | 0b84d6f306446e1befb5d5c22d712138 |
|
BLAKE2b-256 | 5da9a62a9991e45a46cd17d11acc281c707849aee2127ea1191df6e76f5769eb |
Hashes for psygnal-0.10.0-cp311-cp311-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 79a7133c62ed898dc163a8e921d0750bad8fbdbcec4741ec3f84c8134d7f26a6 |
|
MD5 | 4d9b6fdf979b2b6ad2ca79684279d22f |
|
BLAKE2b-256 | 7e20ada5e1a27c1d7262ec74e053dcd4fd7b3ddc97669a89e664b2742fda5954 |
Hashes for psygnal-0.10.0-cp311-cp311-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a73137283edcfee7806054ece942d645cd7a2f93038be357d9202f510ce8d274 |
|
MD5 | 16410e335a62b052c4fc20300182fc40 |
|
BLAKE2b-256 | 6f7e7e21871a2dc4a6d21531db74e92c9d03493cabc4e7f8ce2c758e33e5cfa2 |
Hashes for psygnal-0.10.0-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ab8b024a87e441d901a02698afc983335c89472e215e18d0e6cb426087d36bdf |
|
MD5 | e2d114b3f543cd66a220c7f292046be3 |
|
BLAKE2b-256 | eb006b68be4c0106d48cf25f98f97200baf16123019d7e87b88ca481dbe44573 |
Hashes for psygnal-0.10.0-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4150e0af0f4e36a0e06f7e4e2e52df3d01c5a8d1a40549b6f32bb6f3a1030a7e |
|
MD5 | e0de3c1be2dce5bb18a7baacb6cd9b50 |
|
BLAKE2b-256 | 802855ff11b888f53e08ce0ece34de9d869304aa2b247e6b64b678b416802ea6 |
Hashes for psygnal-0.10.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bacafa804fb2773c327d68b2b4296b59cf09a1749701e955f9b0b6519254c74a |
|
MD5 | adac2f2e2bc89db0db2c1a49f8edacc9 |
|
BLAKE2b-256 | b27477b81992f60cea6fd81ca48714c851956895d603b175c777db842557f7ff |
Hashes for psygnal-0.10.0-cp310-cp310-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 72968d01991a4b4badb21cb9a127c0005c5131483e99f03589ec81e89db69a3f |
|
MD5 | 334d155f23e5622c5c55261bca03fa96 |
|
BLAKE2b-256 | 20cb3512de31e57444daa9af0e60f490452cd91c94b228582c58ef86a04e77f7 |
Hashes for psygnal-0.10.0-cp310-cp310-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b528c90b37215085ce4e15e2b07c3348680fc9128f2f888ffed62989308afc59 |
|
MD5 | f1e15d063b9f8e7391e06b81ee137739 |
|
BLAKE2b-256 | 8b964b4bed7cddb37857046c2d97398a8bb42c7eb84e86fd4b8e2f0499b7c13f |
Hashes for psygnal-0.10.0-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8ec1fbfcd3f55a48ea20601a51c4040d2d77ed5131248f75aaaf728a1508ac56 |
|
MD5 | d3bda0588f3d840520d0b93542f53012 |
|
BLAKE2b-256 | 7099c93c64545790656637ae5724b80b5848b339e5733e084862a888f7297862 |
Hashes for psygnal-0.10.0-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 694bc17e96bf612f359fab17b21745cbd27675f420fde6e77c14fb5d9cca50f0 |
|
MD5 | f2a8e338bc19a841dbb57f76c114ce62 |
|
BLAKE2b-256 | 77645882cbef7a44d212e7c5b252486d70fa10ae2fba4534829c9c4b80f0a45f |
Hashes for psygnal-0.10.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 92e18e70748ca9c2f3f6161a86254ecb7bad5d7facaf8ae71e3bf686f662dbdd |
|
MD5 | e1e55bbfa12f2a3aecbc36b614adec36 |
|
BLAKE2b-256 | 425dbcea194ea98a93685b9682c01f22dc880074fcf7caafb25ad7e1ff918041 |
Hashes for psygnal-0.10.0-cp39-cp39-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c3e8e72f75ba33e6fb805457fa6eab4aa43ea112b4202ee770c113be81503884 |
|
MD5 | 237b9e9da8cb784987609205f8915539 |
|
BLAKE2b-256 | e21ba4d697ab5feb84a2e26ea4ff8193b0809dfed91abdcc49f24f495d81bec5 |
Hashes for psygnal-0.10.0-cp39-cp39-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 33a87ac22b50f18722cc16beff1c070a09226c799b57b18b86425b7526e2cac1 |
|
MD5 | b59974ef74a1aa6b0f0eccec4a31fdfe |
|
BLAKE2b-256 | d607d403ff178501939442f0c9d464507e82a7b773c802e9290f9d6df7536a45 |
Hashes for psygnal-0.10.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ba8fcfaf8592e1c8a8c11dfa8cb28c964cc4f0dc04258c29ad29b615092a4302 |
|
MD5 | 26803c0c6e2da76862a27e75b7ebdf3e |
|
BLAKE2b-256 | c7ba1c601b6ba30e9616a8f94b8a99e9755995476cea781c85d066deca1088e4 |
Hashes for psygnal-0.10.0-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 347455335ff465e4ec72c20965c6d318bb3051e171dc5928a4ac681dffc3fa6f |
|
MD5 | a72138633d3da8c30489d91b17ab2093 |
|
BLAKE2b-256 | 4080835b273c58f3acad616adff31886724b1ecf48396fe5a1b3ac614dec1f3b |
Hashes for psygnal-0.10.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f1be9c12526d2825209a1acb2d4571fec792709df13d45d59d99bc107eca32e |
|
MD5 | 98805bda339a52d861094ee64b0fd3d5 |
|
BLAKE2b-256 | d34577edd23dac9e1c9406aa0a03b5f10be9cb4f3dcfa1f13252fee425870337 |
Hashes for psygnal-0.10.0-cp38-cp38-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 64c56345766c31f64cc8e65f959224981ae888129d4f37c6ec392ee03ef66e57 |
|
MD5 | 892d4296de6d620265b23c0e771f7caa |
|
BLAKE2b-256 | 980d442739d77bb23eefd34999379c3354424d6156aae06e26d6176d495752a9 |
Hashes for psygnal-0.10.0-cp38-cp38-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7d579c72145df19bd130bd6deb33bcc94afc6989f7f373ad339eb3eae9f59dbc |
|
MD5 | bb7a682f51627e74a339789912fc356f |
|
BLAKE2b-256 | c170f59c42722870217f8b5d52f9e373a260780932c4a8e2fa541bfb08802d84 |