Pure python library for utilising a Signalling event pattern
Project description
Overview
This library exposes a mechanism for signalling in Python. Through this mechanism you can create a Signal class and then connect that signal to any number of callable end points. Upon that signal being emitted the Signal class will then trigger a call to all the connected callables.
This pattern allows for events to be propagated to different classes/functions when they occur.
Install
pip install signalling
Example
This example shows how to use a basic signal:
import signalling
# -- Declare some functions. This can be functions, methods
# -- or classsmethods
def do_something():
print("doing something")
def do_something_else():
print("doing something else")
# -- Create a singal
signal = signalling.Signal()
# -- Connect the signal to various end points
signal.connect(do_something)
signal.connect(do_something_else)
# -- Now we can emit the signal and all the end
# -- points will be called (in order they were
# -- connected)
signal.emit()
WeakSignal
The standard Signal
class will hold a direct reference to the callable its connected
to. This means the endpoint will never be garbage collected whilst the signal is alive.
However, there are times that we want to use the signalling mechanism but we dont want
to hold a direct reference. For this we can use signalling.WeakSignal
. This version
of the signalling class will only hold a weak reference to the callable end point, so
it allows for the garbage collecting of end points if nothing else is holding a
reference to it outside of the Signal class.
The approach to using the WeakSignal
is exactly the same, and all the weakref
management is carried out by the signal class itself.
import signalling
# -- Declare some functions. This can be functions, methods
# -- or classsmethods
def do_something():
print("doing something")
def do_something_else():
print("doing something else")
# -- Create a singal
signal = signalling.WeakSignal()
# -- Connect the signal to various end points
signal.connect(do_something)
signal.connect(do_something_else)
# -- Now we can emit the signal and all the end
# -- points will be called (in order they were
# -- connected)
signal.emit()
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 Distribution
File details
Details for the file signalling-1.0.1.tar.gz
.
File metadata
- Download URL: signalling-1.0.1.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
ae9fda8d9ca0199935ef4c245f07c04a06f0cca64d3b9269c874cf0bd4f04874
|
|
MD5 |
b0d84b934156750dd952768e9d502e41
|
|
BLAKE2b-256 |
75c085ee081a6d3134afa76929ab34eac075f519e7536ecbe8a9cdc4969876a7
|
File details
Details for the file signalling-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: signalling-1.0.1-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
937331b4944f09d0c2b83cf4f0abd3d633e84b06baac6a3e69c72763d0c9a6a6
|
|
MD5 |
b41911abd6cf5d1fd977cdc4189dfa49
|
|
BLAKE2b-256 |
fb0ac8be17d3cf41aadfae50aa0a71389655f357e6e75d0c3914e309c97b16f7
|