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.0rc2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 54db9438329ee306ba4b5bdf2ad7b98b858563549a7505d54d5c9abebc334616 |
|
MD5 | 620f943b6f1e38246fe063200111f9f4 |
|
BLAKE2b-256 | b2c25e6bed4fc76ac4c84b55dfb07d6ea31de1ddc1cd0de9c8dc96a245a8bcd0 |
Hashes for psygnal-0.10.0rc2-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ded0fd4433a58185e3c67a78d06adb0c53569828961689ef87580ecc635b806 |
|
MD5 | 01929541ac738168f8c05923a7c629db |
|
BLAKE2b-256 | 3f6c1758568d1e617fd24c12a20f57943e990c7a1c8bc8940890766329870813 |
Hashes for psygnal-0.10.0rc2-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f6e7c810cee82508c70845a65a766a7db84b1503ff8c4ad4b8c357a139afdf5a |
|
MD5 | 293de3bfe6e8771e9ef7cc38872b01a0 |
|
BLAKE2b-256 | 285fdc57b8a18ff42a82383bdf416123dd401ec2638ea32740ef9d23538d62e2 |
Hashes for psygnal-0.10.0rc2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | da5c225866cf672be97c8434fec020b0f474fcca7e49f5f0c428de320be0e8fc |
|
MD5 | ac1fd972c27178a7e4fc91840bc8a101 |
|
BLAKE2b-256 | 79ba8df7cce258400d282f2390a388eab9be121b78e2da8ccd3833f7d185c52d |
Hashes for psygnal-0.10.0rc2-cp312-cp312-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b4f7d3cfdcec2da1bd63851d694bb393ef7b35ce6e6e6560da386cfe52619d0e |
|
MD5 | f2177538f0232e7200e97c75b16edc09 |
|
BLAKE2b-256 | 962a9a38211352f195cd1f54171c08de5e45a1e56197e5ae4ab942e197fa9caf |
Hashes for psygnal-0.10.0rc2-cp312-cp312-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a52244ec1eb679d26c13b9ca447a69c37100c138dde1f67f5cc466033fa5f04 |
|
MD5 | 080346ff9fc3fb416a022447827c7865 |
|
BLAKE2b-256 | 21eebc5aab0c15d912280f06557e3cc441c715bf886ddbfbdd427949df20a5f8 |
Hashes for psygnal-0.10.0rc2-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b0639b68df6c6ff72a827e11b82b9c61baa992ad3d91f2f6ababc7c9f4afc338 |
|
MD5 | 0e4ca32b0dfd7cd1c8cfc6ba198ea473 |
|
BLAKE2b-256 | f72ce60d91eeee104a9049d2871529fa3c3583fde5359130707510d099379678 |
Hashes for psygnal-0.10.0rc2-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7d0d862e678624708ad8dd4aa29980fbce11bbb1fd7674ad8abd522dbc0b8aa7 |
|
MD5 | 04f0a40e6260e4cf2e1b6df346468ff0 |
|
BLAKE2b-256 | af4aff2630f6cec584ef97d49cfec9b33f877c4c6df5536ba1bda586d0f279f5 |
Hashes for psygnal-0.10.0rc2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ccf40ecc4c60624895c42691f5c83b1f02a9412c9fc871895f440b555e52a229 |
|
MD5 | 4e079975cdc4b9aea48ec8b787a4723f |
|
BLAKE2b-256 | c1ac91a3d3f689c270d2d0b62b6ed5fb8d8f806b633b160b14266f6229dac66d |
Hashes for psygnal-0.10.0rc2-cp311-cp311-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8bc86e724e46842652222dbeeafb4e265925bcc2f10117557508c823acd5bf16 |
|
MD5 | aded1245a960cc1bde65b820791e6991 |
|
BLAKE2b-256 | ea10e66169aa5692b7a2a3de0a5bde727f87644830c3e0eeb5a4b636e44b9373 |
Hashes for psygnal-0.10.0rc2-cp311-cp311-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | efc534cde4722c11e090c327b6e52aa62065d2c7b2b18ce664a9bf016ecafb86 |
|
MD5 | 492e14148e86649b87b1405023ba2b0e |
|
BLAKE2b-256 | 1aa6c206abfe2cad4b968f16496db2105629c53031c8f18595d9197b193e6b5f |
Hashes for psygnal-0.10.0rc2-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8216485b694a5b8b28f73fd498300b4c9d17c5eec8dd0d82e97ca39af28f87ec |
|
MD5 | 3431da65737349ce07bf742fea1c841f |
|
BLAKE2b-256 | ce2550d133019e07820bee574e4f55eaea66d566af14a127f128140db3108e2a |
Hashes for psygnal-0.10.0rc2-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 57efd62ba4dae351f72d47c1602f29a59db19cdc7af4184237522143c0d3d3f9 |
|
MD5 | a2841ac9227a66dbd6c9289497b355f5 |
|
BLAKE2b-256 | c4f64248204ad955615d080c54d653394f6f92726218d637be3f8f6c96d242b0 |
Hashes for psygnal-0.10.0rc2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2fa49eb2b61fd5d38d21c3d734437a09c7e6d406c3e3eb9d043f5cddb512e984 |
|
MD5 | d499221081fab406716d29508fd0f31c |
|
BLAKE2b-256 | 56fee81a6b88e903817a0919ed40c3170455194872b2ad23c31d1994499e7892 |
Hashes for psygnal-0.10.0rc2-cp310-cp310-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40954935bca0a8378f90b37057e36c02d021623cea3f884ca92f52451490be73 |
|
MD5 | 3e49cd188de0c2f5bbbe13a5eb601bb5 |
|
BLAKE2b-256 | 87a218991a21477560c8ad2c0cbf7d3c6d7c83f088b68ef5ecd4d0b17b223652 |
Hashes for psygnal-0.10.0rc2-cp310-cp310-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a5d38171bd4ce6415abb5ed8c6c85e687f9670369f408456f64a2df90220ee43 |
|
MD5 | 4c996444f164df30f97dbb0dd554fb53 |
|
BLAKE2b-256 | 2739e125ab78d98005b954fd3ce5ef6ff326e143ba074109c0d2d22f6d212514 |
Hashes for psygnal-0.10.0rc2-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5fbca4aa345e3a13c5837d49f3550844907075bd73660ce64205d003681bde3b |
|
MD5 | e139de05545161576bf79438ae1f5e2a |
|
BLAKE2b-256 | 5125bc548a2acf4880a668a569736c90df4147351455060d1e53440ab7ba4829 |
Hashes for psygnal-0.10.0rc2-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b574642822b44ac2e7db9c2fe2477d69c82dacc162014e4938bd1457e17f3fc6 |
|
MD5 | 7c9934c4f87f3f54bd4f28a9dd980b13 |
|
BLAKE2b-256 | eda66f8c69c1a721131e1e2676e5a1fdc3175d30313b8dfe189509732e4c6e18 |
Hashes for psygnal-0.10.0rc2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ef227ff260cb3095ee7eb03dddee121f7c3be8b986eef46bf7aa291b4259575 |
|
MD5 | ff21ea5446f295fbf4e0ca7be822a1bc |
|
BLAKE2b-256 | 33f9dfdda7c8943a3ae9eda5aa9d6fd24b5ef0ce530bcbe9b88f90cb2805481e |
Hashes for psygnal-0.10.0rc2-cp39-cp39-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17fb4570f1a1ed97441caa4a5c0592de843a46669ee8375f999e148d858e7965 |
|
MD5 | f47672c98dcceb652a49da558822aa1e |
|
BLAKE2b-256 | 3df04b341adf845bceafbb3289a7a59cec1c3a72ad25498a4f5bd48faba2e0be |
Hashes for psygnal-0.10.0rc2-cp39-cp39-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ef6fbfe12a3ecc43e2d1a92085c1e47e9443e3e4ff5f53b6e73a7d4b2d821da |
|
MD5 | 618e866c4547fbe2f809fe02e7a2258a |
|
BLAKE2b-256 | 26405845343144c128fc4f3d343313932331834561a9c1d113b21b421118473e |
Hashes for psygnal-0.10.0rc2-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c27d0c22305bba5201089bd517332f9ac8b430eb4dbd59ca1fe6db05a66d728 |
|
MD5 | 0a587f0595af16a689d6bf384aa2f617 |
|
BLAKE2b-256 | fe8b77978292699b2709d046458b189e364a0f0de3c83962828811e2eab605e6 |
Hashes for psygnal-0.10.0rc2-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 789273d50dec1884f670cac3f5608facee6a6c6b2a8b3a5f8c42eb57efa7f3a3 |
|
MD5 | cace28bce8c2237ffd96bbe291282a04 |
|
BLAKE2b-256 | 322cbf0d4f4f9274ae93a5a3d996f93c5a557ccd8676aabf2d63d4ca8a3b054f |
Hashes for psygnal-0.10.0rc2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 80fd6a1f301b1efc7c4df4acd7f1db0e6624ae396b262af0069c4a2fee8c3e85 |
|
MD5 | e8705aa1fb5d1e14aece332f8362e0e8 |
|
BLAKE2b-256 | 665703af857da19b3659e901cbb1b240987750342351737b56c05cd03468d892 |
Hashes for psygnal-0.10.0rc2-cp38-cp38-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c78ca60033c5428ab41123478ada8b18fa789b08fd461b031a4bf2d370af1da3 |
|
MD5 | c7c515310e44246b9685707dc0a83883 |
|
BLAKE2b-256 | e41e82cdd97d9c4fda17ae4f8a5e4015e1058d5a46032a639129f694c3776eac |
Hashes for psygnal-0.10.0rc2-cp38-cp38-macosx_10_16_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2c8fd1cd31696ceee6a4518b4a358f98d0e97748b77c128d06cbead398ce6c43 |
|
MD5 | 39d9a59f0832674071ff65a4a74528db |
|
BLAKE2b-256 | a99b1e54ac0b0a7b2196911a89733bfadc3e76286e788aaff2ad91162fb683fe |