Pure python implementation of Qt Signals
Project description
psygnal
Pure python implementation of Qt-style Signals, with (optional) signature and type checking, and support for threading.
Usage
Install
pip install psygnal
Basic usage
If you are familiar with the Qt Signals &
Slots API as implemented in
PySide and
PyQt5,
then you should be good to go! psygnal
aims to be a superset of those APIs
(some functions do accept additional arguments, like
check_nargs
and
check_types
).
Note: the name "Signal
" is used here instead of pyqtSignal
, following the
qtpy
and PySide
convention.
from psygnal import Signal
# create an object with class attribute Signals
class MyObj:
# this signal will emit a single string
value_changed = Signal(str)
def __init__(self, value=0):
self._value = value
def set_value(self, value):
if value != self._value:
self._value = str(value)
# emit the signal
self.value_changed.emit(self._value)
def on_value_changed(new_value):
print(f"The new value is {new_value!r}")
# instantiate the object with Signals
obj = MyObj()
# connect one or more callbacks with `connect`
obj.value_changed.connect(on_value_changed)
# callbacks are called when value changes
obj.set_value('hello!') # prints: 'The new value is 'hello!'
# disconnect callbacks with `disconnect`
obj.value_changed.disconnect(on_value_changed)
connect
as a decorator
.connect()
returns the object that it is passed, and so
can be used as a decorator.
@obj.value_changed.connect
def some_other_callback(value):
print(f"I also received: {value!r}")
obj.set_value('world!')
# prints:
# I also received: 'world!'
Connection safety (number of arguments)
psygnal
prevents you from connecting a callback function that is guaranteed
to fail due to an incompatible number of positional arguments. For example,
the following callback has too many arguments for our Signal (which we declared
above as emitting a single argument: Signal(str)
)
def i_require_two_arguments(first, second):
print(first, second)
obj.value_changed.connect(i_require_two_arguments)
raises:
ValueError: Cannot connect slot 'i_require_two_arguments' with signature: (first, second):
- Slot requires at least 2 positional arguments, but spec only provides 1
Accepted signature: (p0: str, /)
Note: Positional argument checking can be disabled with connect(..., check_nargs=False)
Extra positional arguments ignored
While a callback may not require more positional arguments than the signature
of the Signal
to which it is connecting, it may accept less. Extra
arguments will be discarded when emitting the signal (so it
isn't necessary to create a lambda
to swallow unnecessary arguments):
obj = MyObj()
def no_args_please():
print(locals())
obj.value_changed.connect(no_args_please)
# otherwise one might need
# obj.value_changed.connect(lambda a: no_args_please())
obj.value_changed.emit('hi') # prints: "{}"
Connection safety (types)
For type safety when connecting slots, use check_types=True
when connecting a
callback. Recall that our signal was declared as accepting a string
Signal(str)
. The following function has an incompatible type annotation: x: int
.
# this would fail because you cannot concatenate a string and int
def i_expect_an_integer(x: int):
print(f'{x} + 4 = {x + 4}')
# psygnal won't let you connect it
obj.value_changed.connect(i_expect_an_integer, check_types=True)
raises:
ValueError: Cannot connect slot 'i_expect_an_integer' with signature: (x: int):
- Slot types (x: int) do not match types in signal.
Accepted signature: (p0: str, /)
Note: unlike Qt, psygnal
does not perform any type coercion when emitting
a value.
Query the sender
Similar to Qt's QObject.sender()
method, a callback can query the sender using the Signal.sender()
class
method. (The implementation is of course different than Qt, since the receiver
is not a QObject
.)
obj = MyObj()
def curious():
print("Sent by", Signal.sender())
assert Signal.sender() == obj
obj.value_changed.connect(curious)
obj.value_changed.emit(10)
# prints (and does not raise):
# Sent by <__main__.MyObj object at 0x1046a30d0>
If you want the actual signal instance that is emitting the signal
(obj.value_changed
in the above example), use Signal.current_emitter()
.
Emitting signals asynchronously (threading)
There is experimental support for calling all connected slots in another thread,
using emit(..., asynchronous=True)
obj = MyObj()
def slow_callback(arg):
import time
time.sleep(0.5)
print(f"Hi {arg!r}, from another thread")
obj.value_changed.connect(slow_callback)
This one is called synchronously (note the order of print statements):
obj.value_changed.emit('friend')
print("Hi, from main thread.")
# after 0.5 seconds, prints:
# Hi 'friend', from another thread
# Hi, from main thread.
This one is called asynchronously, and immediately returns to the caller.
A threading.Thread
object is returned.
thread = obj.value_changed.emit('friend', asynchronous=True)
print("Hi, from main thread.")
# immediately prints
# Hi, from main thread.
# then after 0.5 seconds this will print:
# Hi 'friend', from another thread
Note: The user is responsible for joining
and managing the
threading.Thread
instance returned when calling .emit(..., asynchronous=True)
.
Experimental! While thread-safety is the goal,
(RLocks
are
used during important state mutations) it is not guaranteed. Please use at your
own risk. Issues/PRs welcome.
Other similar libraries
There are other libraries that implement similar event-based signals, they may server your purposes better depending on what you are doing.
PySignal (deprecated)
This package borrows inspiration from – and is most similar to – the now
deprecated PySignal project, with a few
notable new features in psygnal
regarding signature and type checking, sender
querying, and threading.
similarities with PySignal
- still a "Qt-style" signal implementation that doesn't depend on Qt
- supports class methods, functions, lambdas and partials
differences with PySignal
- the class attribute
pysignal.ClassSignal
is called simplySignal
inpsygnal
(to more closely match the PyQt/Pyside syntax). Correspondinglypysignal.Signal
is similar topsygnal.SignalInstance
. - Whereas
PySignal
refrained from doing any signature and/or type checking either at slot-connection time, or at signal emission time,psygnal
offers signature declaration similar to Qt with , for example,Signal(int, int)
. along with opt-in signature compatibility (withcheck_nargs=True
) and type checking (withcheck_types=True
)..connect(..., check_nargs=True)
in particular ensures that any slot to connected to a signal will at least be compatible with the emitted arguments. - You can query the sender in
psygnal
by using theSignal.sender()
orSignal.current_emitter()
class methods. (The former returns the instance emitting the signal, similar to Qt'sQObject.sender()
method, whereas the latter returns the currently emittingSignalInstance
.) - There is basic threading support (calling all slots in another thread), using
emit(..., asynchronous=True)
. This is experimental, and while thread-safety is the goal, it is not guaranteed. - There are no
SignalFactory
classes here.
The following two libraries implement django-inspired signals, they do not attempt to mimic the Qt API.
Blinker
Blinker provides a fast dispatching system that allows any number of interested parties to subscribe to events, or "signals".
SmokeSignal
(This appears to be unmaintained)
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
File details
Details for the file psygnal-0.1.0.tar.gz
.
File metadata
- Download URL: psygnal-0.1.0.tar.gz
- Upload date:
- Size: 22.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 34529b9f387615c7986f37749222683d0a17709ef726ef6fbaa8077da44bd0e4 |
|
MD5 | 85d2a349d470b99f10a0ae3e8c81e1be |
|
BLAKE2b-256 | 863d59f886ffab292606a9c4fda8ef1333b2f57e0bdb54ba645886aba735f210 |
Provenance
File details
Details for the file psygnal-0.1.0-py2.py3-none-any.whl
.
File metadata
- Download URL: psygnal-0.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 12.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | baf1e4f1c32980e0c3942c4be3c727d1d95ea5f59be29817596a49bdb6da55b4 |
|
MD5 | e17f9b6afa3d0379b605548bcc3aa658 |
|
BLAKE2b-256 | 5c218421f6c6fc5ea1ddea524cb5c09b38d8210d944025df91a9ab0c2967db73 |
Provenance
File details
Details for the file psygnal-0.1.0-cp39-cp39-win_amd64.whl
.
File metadata
- Download URL: psygnal-0.1.0-cp39-cp39-win_amd64.whl
- Upload date:
- Size: 126.6 kB
- Tags: CPython 3.9, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3f38c1d0144b551a305392dbc915b776149d5e4deef24c1fa8e36e144f002d09 |
|
MD5 | cce7552dc0c8dd215d3cd8be085a65e1 |
|
BLAKE2b-256 | afa8143cde530ad4bd45f25ba599997d83b3b0abe7a2d3cb8a80b05e71cf5045 |
Provenance
File details
Details for the file psygnal-0.1.0-cp39-cp39-manylinux2014_x86_64.whl
.
File metadata
- Download URL: psygnal-0.1.0-cp39-cp39-manylinux2014_x86_64.whl
- Upload date:
- Size: 653.2 kB
- Tags: CPython 3.9
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d23b139495b70197af714864f661f0bce60c749c8c2735c9a70b02e9ffb1a40 |
|
MD5 | 95ac2c785b873e5d4b179d629736b359 |
|
BLAKE2b-256 | 1207b4766f21bbeac4c46bc6cd72da4eab79c56ae9008b7eb536b5b2bd6fd726 |
Provenance
File details
Details for the file psygnal-0.1.0-cp39-cp39-manylinux2014_i686.whl
.
File metadata
- Download URL: psygnal-0.1.0-cp39-cp39-manylinux2014_i686.whl
- Upload date:
- Size: 624.1 kB
- Tags: CPython 3.9
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f0fa5721b817088d0e29d1b5a6b6ce42b3791060441d4db747188f6e9ec734b |
|
MD5 | 97ecf468c078bb3b9627c6de511da9b9 |
|
BLAKE2b-256 | 6077ca5e306f5d3f5a1a36a3abb982b0ec602980ee81f6d1d09146694e6933a3 |
Provenance
File details
Details for the file psygnal-0.1.0-cp39-cp39-manylinux1_i686.whl
.
File metadata
- Download URL: psygnal-0.1.0-cp39-cp39-manylinux1_i686.whl
- Upload date:
- Size: 624.1 kB
- Tags: CPython 3.9
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 51a1432b40a333e6071d46ec9010ba00569435716a3b65a31eac858b2fb01977 |
|
MD5 | 3585aad3cc49051cfb7bb7e544928dbb |
|
BLAKE2b-256 | a4a4eef4305d2e2f5b2ff30521e2989d230e02a215f278e859acc0b1dcb37244 |
Provenance
File details
Details for the file psygnal-0.1.0-cp39-cp39-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: psygnal-0.1.0-cp39-cp39-macosx_10_9_x86_64.whl
- Upload date:
- Size: 154.6 kB
- Tags: CPython 3.9, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48d3b3f443f6d31f9a4e742b735f0af960f0238dfe98de9cc8f92455c58c3bc7 |
|
MD5 | bc15c3d5d7309bd013d90cb5def15216 |
|
BLAKE2b-256 | 4202e891cc5de51ba1c4f3cd59503570753cb69815823856b27939dfab53dc4f |
Provenance
File details
Details for the file psygnal-0.1.0-cp38-cp38-win_amd64.whl
.
File metadata
- Download URL: psygnal-0.1.0-cp38-cp38-win_amd64.whl
- Upload date:
- Size: 128.3 kB
- Tags: CPython 3.8, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3341e476eb37c6a205bf6dfedec95dd0431c8c77e4cf86b8376ae85e0b60926f |
|
MD5 | 4786c4d32ebf2236a97b4ca7f242422d |
|
BLAKE2b-256 | 678ce872d5d28f84d9f52d5e5490101c2f9369828730f1ddefefc382e8d80d5b |
Provenance
File details
Details for the file psygnal-0.1.0-cp38-cp38-manylinux2014_x86_64.whl
.
File metadata
- Download URL: psygnal-0.1.0-cp38-cp38-manylinux2014_x86_64.whl
- Upload date:
- Size: 747.8 kB
- Tags: CPython 3.8
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 277bd9823fd451e079103a9dc86c8c3b88c174a696aded20b499a16f140a72de |
|
MD5 | 8411580d404a575d1fc5043aa00257c6 |
|
BLAKE2b-256 | 698489c94efedf333e6a522ab6ed2836044a8ece39832ca28af5bfa19f3822c0 |
Provenance
File details
Details for the file psygnal-0.1.0-cp38-cp38-manylinux2014_i686.whl
.
File metadata
- Download URL: psygnal-0.1.0-cp38-cp38-manylinux2014_i686.whl
- Upload date:
- Size: 702.0 kB
- Tags: CPython 3.8
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 388c99987c86854e1e9d215cd7f9cb2a5e4bd3d53a379ca47db4cd263d5d6b70 |
|
MD5 | 72f5200b4a7d5d32d7bc555b5b3def87 |
|
BLAKE2b-256 | e4a5c562c07f2babb033153ec3e2887a332d24d19c36cb8662f90d51d86b46d0 |
Provenance
File details
Details for the file psygnal-0.1.0-cp38-cp38-manylinux1_i686.whl
.
File metadata
- Download URL: psygnal-0.1.0-cp38-cp38-manylinux1_i686.whl
- Upload date:
- Size: 702.0 kB
- Tags: CPython 3.8
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 42e5b633dfa51787bc21250d2c8e8388cc18556648e623d61f46f5ddbc4d2ec5 |
|
MD5 | 8ad699a5b0e052005ebc2f4dffa5a654 |
|
BLAKE2b-256 | d92fc0b30f3f7ccfd629795a3855fbf53cca55e121096abe1260a87c213dc09c |
Provenance
File details
Details for the file psygnal-0.1.0-cp38-cp38-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: psygnal-0.1.0-cp38-cp38-macosx_10_9_x86_64.whl
- Upload date:
- Size: 153.6 kB
- Tags: CPython 3.8, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc962c576e4dd93f071a3b6acc9cf35df5b89a9a59a5d3d751005a0b2596a62d |
|
MD5 | 9c5fc8f018a7052abb3764ce579b464a |
|
BLAKE2b-256 | cd08eb5d23fb4ab9280eadea6c81bc34b799fba2056bcd7477e1d15ed5cfeafe |
Provenance
File details
Details for the file psygnal-0.1.0-cp37-cp37m-win_amd64.whl
.
File metadata
- Download URL: psygnal-0.1.0-cp37-cp37m-win_amd64.whl
- Upload date:
- Size: 124.4 kB
- Tags: CPython 3.7m, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dfb0bf67dc9c3208f7a111d86cc4d87e83d3bebd68e45c5d3c09977c3b8dac35 |
|
MD5 | 72b852a925beeb29603c9153d5badad7 |
|
BLAKE2b-256 | 3a7d642ecebc96ab3664b8086f400b5f133439c8b60ceea87c4c2a53db935c74 |
Provenance
File details
Details for the file psygnal-0.1.0-cp37-cp37m-manylinux2014_x86_64.whl
.
File metadata
- Download URL: psygnal-0.1.0-cp37-cp37m-manylinux2014_x86_64.whl
- Upload date:
- Size: 586.7 kB
- Tags: CPython 3.7m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 828ee455d84fef06bfa2753a6f953dc5f94fda6f530763fdda59c437382c5de7 |
|
MD5 | 67d6c58358ed2a5d54df8ea289461a63 |
|
BLAKE2b-256 | ab67b6296f99baa0a25579bcd4373e9c44d804f8382afdfdde440ab8df958189 |
Provenance
File details
Details for the file psygnal-0.1.0-cp37-cp37m-manylinux2014_i686.whl
.
File metadata
- Download URL: psygnal-0.1.0-cp37-cp37m-manylinux2014_i686.whl
- Upload date:
- Size: 560.9 kB
- Tags: CPython 3.7m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9363a28b02f0a8590693252c01124c4ecb42064722d322364d258286496588b3 |
|
MD5 | 811d967940679b8597aaf52b4b6ec44e |
|
BLAKE2b-256 | 61cb31946cd7474b1c4cd440d8bb2393c382ecd0f81f95e2cfdb11ac81b672ad |
Provenance
File details
Details for the file psygnal-0.1.0-cp37-cp37m-manylinux1_i686.whl
.
File metadata
- Download URL: psygnal-0.1.0-cp37-cp37m-manylinux1_i686.whl
- Upload date:
- Size: 560.9 kB
- Tags: CPython 3.7m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3bbfa77720711d308ba5291702ee9ffed516184e5b85e9c3545de8d0e165c8a9 |
|
MD5 | 0447e3991512f83b6592d087f135a209 |
|
BLAKE2b-256 | cddf35450c0ab9e03b9b3e72bad69ab60e03455c232cf0df9875e34a0c9ea614 |
Provenance
File details
Details for the file psygnal-0.1.0-cp37-cp37m-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: psygnal-0.1.0-cp37-cp37m-macosx_10_9_x86_64.whl
- Upload date:
- Size: 150.3 kB
- Tags: CPython 3.7m, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5ad288e195cb09020fa15a08cbe8ff649fc1b01f072ce50d6f41d46237d3b8f |
|
MD5 | cd11bd6c57d32b7b2bb8d62a15a4c9fc |
|
BLAKE2b-256 | 192b3086637a3b3ac54706304d825e0f3ea3194331a761b678dfad81ebce54d3 |