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.0rc3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c8b5c144071ef079760cceaca2558a50785a6b27575f95a9a5b4ea151e5f174e |
|
MD5 | dd19e423586944148725c464d9b2a849 |
|
BLAKE2b-256 | 98ec6ac18342cff77241d9d1e70a25a8c7db39a170ecaeb7c6a57cca96d99c59 |
Hashes for psygnal-0.10.0rc3-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 548871ed7d1fbbba70b9ca067ce2a6b054ecfbdb41ae47eb1b3dffd168cdea9f |
|
MD5 | 6d75da0d6e835e75f1fb10be868cbbe4 |
|
BLAKE2b-256 | 3eb186b09f9dcc92f2c4ac99fb75a072c90eb7f6a945a8a3bcc7f336b1f04c48 |
Hashes for psygnal-0.10.0rc3-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ef82cc89f43e315338ea9968afb17b6eca20a214d912c2cba320205cb5bb0fb |
|
MD5 | dee9fd1ef26f4a87f95455cb0d3f6a06 |
|
BLAKE2b-256 | 13a444929bfee4bcaa4c2723369a257a9a100ba7bb366428d5b24c3e5c8c5c0d |
Hashes for psygnal-0.10.0rc3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dc60a07db73d5a8edd70f6e00e953986f9e45cb38a12a8173173de75927e82b1 |
|
MD5 | a1877e38ed14f157f47f9fe91dfbf0d7 |
|
BLAKE2b-256 | 81ec0adc8aa05cad3bcd28cca85ee561ea51d50f34d34df8c74e302b4bbf3290 |
Hashes for psygnal-0.10.0rc3-cp312-cp312-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dd991b6e85bda026d2827372cd5b57bfcfb5ed4646e5ba7c6cdbce6ca8ea7669 |
|
MD5 | 45581acdd72d5c8e3daa72cd8de8a478 |
|
BLAKE2b-256 | e21317baa7c807079577805f56ed57050e09aeddc37946c4ae53657d87eba6d9 |
Hashes for psygnal-0.10.0rc3-cp312-cp312-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 911db9d93eecd1e02c94dd77843945c8283c90a7804d3556caecbc3cbe377584 |
|
MD5 | 2d4fea600705532cfd69d36819653dcd |
|
BLAKE2b-256 | 9d00028401046565eeb9ec77d8e5c1d126d055e09c3a5c62aae48f99feff2cd6 |
Hashes for psygnal-0.10.0rc3-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d5afb2ed5a469b44524438cb4f0b9f4a0d678ccb98c12be5ca7d846c2ed3e7e7 |
|
MD5 | 12b36141a32c36dd435f8622a98f2584 |
|
BLAKE2b-256 | 18e2f763b7aa71911c5e4ee4da4d4a918791876b130bdfb3da777a1c0dec6777 |
Hashes for psygnal-0.10.0rc3-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a5ae8aab950a68938e4c52647f959b774aa920515d6076988c317590353842ef |
|
MD5 | 3e205f8814a5ebc2f94940752af27bd2 |
|
BLAKE2b-256 | 73fedba65ae3faf467d61ea4f5532576c0cc4d4ff4574e88531b4634aa7d3386 |
Hashes for psygnal-0.10.0rc3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 38432bc38d7fc4b742550dacd772a5fede99b400ac2d067f5eac4ce37da600e3 |
|
MD5 | b93dee41f30d72826d2de1d03c4330e0 |
|
BLAKE2b-256 | 2f98f26b7676c4d1f2e054c7db754f9c8d9188f47b5ce8aa57889fc3b57bb250 |
Hashes for psygnal-0.10.0rc3-cp311-cp311-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0311a0a31006b7528576cbc4a6e25a1cdf76028895307dd9c3003c6f3141b171 |
|
MD5 | ad0cc6cfb86bbb53d5c4973c7da5b78b |
|
BLAKE2b-256 | e0a2753a78948c7f7ab027261761ac6f87b3084f1f5c500b203e71eea9a84f53 |
Hashes for psygnal-0.10.0rc3-cp311-cp311-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2c9c7d9648c05fb41680dd81507e5bb00c54d13c8f5b51477140840051c2bf6e |
|
MD5 | 18bfa5b8ba7a1fd7aa0c917100244be4 |
|
BLAKE2b-256 | 30ff433c2a402d032701d8f1d99bb4f8ea14c525ed1579f366f570380eaaa733 |
Hashes for psygnal-0.10.0rc3-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f352f9f13d5c5277c4637d0c5c521abbc91f4d624e569e37451a66d45c6ec6c1 |
|
MD5 | 933bedb4e417022d20afc6a645f7e75b |
|
BLAKE2b-256 | 81976164a23f15b4abbbccd65960d4c0af98927f6736940b13c8882fccfb3136 |
Hashes for psygnal-0.10.0rc3-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b40ade19afaae35099695629e56d2bd063d6093d03476cf6136caa87b4136dab |
|
MD5 | 5f094f89f6bde343207e40dd1076634f |
|
BLAKE2b-256 | a6fca7310f684b9a989cb450528b12c46768429c964bf5220b29616e203cce9e |
Hashes for psygnal-0.10.0rc3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 18d58e6c7c6bc1e3a10de5442d6e9f309a7d59533d6de818ed77c25acafc1834 |
|
MD5 | 7c5b4afbad14ebcd6218ae5a8f62595e |
|
BLAKE2b-256 | f2eeb81f0c4b93a43cd895d0cd4e6f82f32fa39bade60ccf3d073c45d4546281 |
Hashes for psygnal-0.10.0rc3-cp310-cp310-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5948e87f2bdad3eff92b4f0aec20501dc1b125b10415cbc48005b88301b27be6 |
|
MD5 | 7f2f3f9c6f1bca1e19e774a1e426b2db |
|
BLAKE2b-256 | 87445019346d7c41895b054879603d2b8c1086191ba648f15da09bcbe6eea27e |
Hashes for psygnal-0.10.0rc3-cp310-cp310-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e38407e8d5ac6e9216d8cbd78220dc8f6b436c6d8b1a7d1f353165c1ee23690c |
|
MD5 | be123ed4ce988cb064661f3c7037cd01 |
|
BLAKE2b-256 | 18eadfb02b28f88933fae33b88cfbd985712968f139f45df64cd66a4fdf761d4 |
Hashes for psygnal-0.10.0rc3-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 89d80f48b6cc199dc9903d83e0337e917b13bb912b9cf9d39938e7ee790069b6 |
|
MD5 | 2bb805c08b86e259ef65d79ba3b1f063 |
|
BLAKE2b-256 | 43c700c4b6145c9ca0b7349adb170a5732fb00c2d324f8e1ce3d006d06e8da23 |
Hashes for psygnal-0.10.0rc3-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 293f058eeb981e276341f7a60f288da4167f4df2a5e76dae7ca0883851595170 |
|
MD5 | 70f1ec7fbff591c84db2adec5ac2a0cc |
|
BLAKE2b-256 | ae34d4f940f2e65fc14aa1de922db2d7f5af2f66d2477d3de4a2828b6b517a9a |
Hashes for psygnal-0.10.0rc3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8ff8da5f699c56f163dce4f137e9d3f5b9e25f03185103d3c1b8767d93a7d1d7 |
|
MD5 | 242c8c2e6b78b1f548adda8f45c4b8b0 |
|
BLAKE2b-256 | 8eac99b4dab7cfeb72fb615edefe57807de87633577c26479675ea892e067210 |
Hashes for psygnal-0.10.0rc3-cp39-cp39-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 70d49ee6f113842976e1431643e8c825cc2a0559c316fd797de5559f5f5c49fe |
|
MD5 | cfab5954572b24308144440085e328c2 |
|
BLAKE2b-256 | c874c4b7392bad1b2a324a4e8f3889d22e0a77aeaf734edce12658e2e27f989b |
Hashes for psygnal-0.10.0rc3-cp39-cp39-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c03b8e2389152adfbdf103cf63fa9392bd1e4bd740a24a37600974cd275f7d33 |
|
MD5 | f2723eb16147fb96e9da3884039df93e |
|
BLAKE2b-256 | d75fbde551826728ebe4f3bbf7609cb968f83d4f969e0bf5a2ef94bbd26d487c |
Hashes for psygnal-0.10.0rc3-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4069546e19c0c77420ccc7629c9d8b95422a4e9680d3c016c9899923ec557ccc |
|
MD5 | 406dcc3c817b90e61eeb74f893ac05d4 |
|
BLAKE2b-256 | c104c8c8ad14311b5c9771e856226a705fb8a5edd9ad6628da2b805cff65a7a1 |
Hashes for psygnal-0.10.0rc3-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ebd6283dbc4a70212bf6865338b65e3d82211cb4e36d38807c4ac7a2448701f |
|
MD5 | 0c42a98bcad0cf56683f513e382bf0a1 |
|
BLAKE2b-256 | c38ba25a1c1af3cd30f7c412ba0f1e425ea0926871f926b3fd11a74327353042 |
Hashes for psygnal-0.10.0rc3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b455a2a912d928a89733ef7bf635167904fdb2d06210f91e61478d9731f5c088 |
|
MD5 | b6525ef25cc886af2037538d6c0c8258 |
|
BLAKE2b-256 | bee697eb73fbf29cb91550d2d9b262d76ed426ba7adfdf41343bb7f1c446bf84 |
Hashes for psygnal-0.10.0rc3-cp38-cp38-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ed6a7e408e768aa0a4e2b455fce0713499a915f3917a15ad6307d878fd5a15d |
|
MD5 | 3af6e53c94325447af5ef8aa06fed8d4 |
|
BLAKE2b-256 | 760804224247fabe5aecceafbb18f22e69b79890b0fab7e82e140e156b2dea66 |
Hashes for psygnal-0.10.0rc3-cp38-cp38-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5335bb746efc8be1dffb7cd5f4cad79bf9cdb0ffc8f2a80a708c93d65ed8bc1f |
|
MD5 | d9c618148a63fd7b57ee6faba5993f08 |
|
BLAKE2b-256 | f123a2b4bd5c3f3439b3c1cf195911b84162aa1a23273d9650584add55727a5a |