signal - Callback system based on Signals and Slots, and "Delphi events"¶
Overview¶
firebird.base.signal
¶
This module provides two callback mechanisms:
- Signals and Slots (
Signal,signaldecorator): Inspired by Qt, a signal can be connected to multiple slots (callbacks). When the signal is emitted, all connected slots are called. Return values from slots are ignored. - Eventsockets (
eventsocketdecorator): Similar to Delphi events, an eventsocket holds a reference to a single slot (callback). Assigning a new slot replaces the previous one. Calling the eventsocket delegates the call directly to the connected slot. Return values are passed back from the slot.
In both cases, slots can be functions, instance/class methods, functools.partial
objects, or lambda functions. The inspect module is used to enforce signature
matching between the signal/eventsocket definition and the connected slots.
Important
All type annotations in signatures are significant, so callbacks must have exactly the same annotations as signatures used by signals or events. The sole exception are excess keyword arguments with default values defined on connected callable.
Tip
You may use functools.partial to adapt callable with different signatures. However, you can "mask" only keyword arguments (without default) and leading positional arguments (as any positional argument binded by name will not mask-out parameter from signature introspection).
Signals and Slots¶
Signals and slots are suitable for 1:N notification schemes. The Signal works as a point
to which one or more Slots could be connected. When Signal is "emitted", all connected
slots are called (executed). It's possible to pass parameters to slot callables, but any
value returned by slot callable is ignored. The Signal contructor takes inspect.Signature
argument that defines the required signature that callables (slots) must have to connect
to this signal.
This mechanism is provided in two forms:
- The
Signalclass to create signal instances for direct use. - The
signaldecorator to define signals on classes. This decorator works like builtinproperty(without setter and deleter), where the 'getter' method is used only to define the signature required for slots.
Example:
from __future__ import annotations
from firebird.base.signal import signal
class Emitor:
def __init__(self, name: str):
self.name = name
def showtime(self):
self.signal_a(self, 'They Live!', 42)
@signal
def signal_a(self, source: Emitor, msg: str, value: int) -> None:
"Documentation for signal"
class Receptor:
def __init__(self, name: str):
self.name = name
def on_signal_a(self, source: Emitor, msg: str, value: int) -> None:
print(f"{self.name} received signal from {source.name} ({msg=}, {value=})")
@classmethod
def cls_on_signal_a(cls, source: Emitor, msg: str, value: int) -> None:
print(f"{cls.__name__} received signal from {source.name} ({msg=}, {value=})")
def on_signal_a(source: Emitor, msg: str, value: int):
print(f"Function 'on_signal_a' received signal from {source.name} ({msg=}, {value=})")
e1 = Emitor('e1')
e2 = Emitor('e2')
r1 = Receptor('r1')
r2 = Receptor('r2')
#
e1.signal_a.connect(r1.on_signal_a)
e1.signal_a.connect(r2.on_signal_a)
e1.signal_a.connect(r1.cls_on_signal_a)
e2.signal_a.connect(on_signal_a)
e2.signal_a.connect(r2.on_signal_a)
#
e1.showtime()
e2.showtime()
Output from sample code:
r1 received signal from e1 (msg='They Live!', value=42)
r2 received signal from e1 (msg='They Live!', value=42)
Receptor received signal from e1 (msg='They Live!', value=42)
Function 'on_signal_a' received signal from e2 (msg='They Live!', value=42)
r2 received signal from e2 (msg='They Live!', value=42)
Events¶
Events are suitable for optional callbacks that delegate some functionality to other class or function.
The 'event' works as a point to which one 'slot' could be connected. The event itself
acts as callable, that executes the connected slot (if assigned). Events may have parameters
and return values. Events could be defined only on classes using eventsocket decorator,
that works like builtin property (without deleter), where the 'getter' method is used only
to define the signature required for slot, and 'setter' is used to assign the callable.
To disconnect the callable from event, simply assign None to the event.
Example:
from __future__ import annotations
from firebird.base.signal import eventsocket
class Component:
def __init__(self, name: str):
self.name = name
@eventsocket
def on_init(self, source: Component, arg: str) -> bool:
"Documentation for event"
@eventsocket
def on_exit(self, source: Component) -> None:
"Documentation for event"
def showtime(self) -> None:
print(f"{self.name}.on_init handler is {'SET' if self.on_init.is_set() else 'NOT SET'}")
print(f"{self.name}.on_exit handler is {'SET' if self.on_exit.is_set() else 'NOT SET'}")
print("Event handler returned", self.on_init(self, 'argument'))
print(f"{self.name} does something...")
self.on_exit(self)
class Container:
def __init__(self):
self.c1 = Component('C1')
self.c1.on_init = self.event_init
self.c2 = Component('C2')
self.c2.on_init = self.event_init
self.c2.on_exit = self.event_exit
self.c3 = Component('C3')
self.c3.on_exit = self.event_exit
def event_init(self, source: Component, arg: str) -> bool:
print(f"Handlig {source.name}.on_init({arg=})")
return source is self.c2
def event_exit(self, source: Component) -> None:
print(f"Handlig {source.name}.on_exit()")
def showtime(self) -> None:
self.c1.showtime()
self.c2.showtime()
self.c3.showtime()
cn = Container()
cn.showtime()
C1.on_init handler is SET
C1.on_exit handler is NOT SET
Handlig C1.on_init(arg='argument')
Event handler returned False
C1 does something...
C2.on_init handler is SET
C2.on_exit handler is SET
Handlig C2.on_init(arg='argument')
Event handler returned True
C2 does something...
Handlig C2.on_exit()
C3.on_init handler is NOT SET
C3.on_exit handler is SET
Event handler returned None
C3 does something...
Handlig C3.on_exit()
Classes¶
firebird.base.signal.Signal
¶
Handles connections between a signal and multiple slots (callbacks).
When the signal is emitted, all connected slots are called with the provided arguments. Return values from slots are ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signature
|
Signature
|
The |
required |
Important
Only slots that match the signature could be connected to signal. The check is performed only on parameters, and not on return value type (as signals does not have/ignore return values).
The match must be exact, including type annotations, parameter names, order, parameter type etc. The sole exception to this rule are excess slot keyword arguments with default values.
Note
Signal functions with signatures different from signal could be adapted using
functools.partial. However, you can "mask" only keyword arguments (without
default) and leading positional arguments (as any positional argument binded
by name will not mask-out parameter from signature introspection).
Source code in firebird/base/signal.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | |
block: bool
instance-attribute
¶
Toggle to block / unblock signal transmission
__call__(*args, **kwargs)
¶
clear() -> None
¶
connect(slot: Callable) -> None
¶
Connect a callable slot to this signal.
The slot will be called whenever the signal is emitted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slot
|
Callable
|
The callable (function, method, lambda, partial) to connect. Its signature must match the signal's signature (see class docs). |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Storage Note
- Regular functions are stored using
weakref.refto avoid preventing garbage collection if the signal outlives the function's scope. - Instance methods are stored using a
WeakKeyDictionarymapping the instance (weakly) to the unbound function. - Lambdas and
functools.partialobjects are stored directly, as weak references to them are often problematic.
Source code in firebird/base/signal.py
disconnect(slot: Callable) -> None
¶
Disconnect a previously connected slot from the signal.
Attempts to remove the specified slot. Does nothing if the slot is not currently connected or not callable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slot
|
Callable
|
The callable that was previously passed to |
required |
Source code in firebird/base/signal.py
emit(*args, **kwargs) -> None
¶
Emit the signal, calling all connected slots with the given arguments.
Does nothing if self.block is True. Handles different storage types
(functions, methods, lambdas, partials) correctly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Positional arguments to pass to the slots. |
()
|
|
**kwargs
|
Keyword arguments to pass to the slots. |
{}
|
Source code in firebird/base/signal.py
firebird.base.signal._EventSocket
¶
Internal EventSocket handler.
Source code in firebird/base/signal.py
is_set() -> bool
¶
Returns True if slot is assigned to eventsocket.
Decorators¶
firebird.base.signal.signal
¶
Decorator to define a Signal instance as a read-only property on a class.
The decorated function's signature (excluding 'self') defines the required signature for slots connecting to this signal. The body of the decorated function is never executed.
A unique Signal instance is lazily created for each object instance the
first time the signal property is accessed.
Example
Source code in firebird/base/signal.py
firebird.base.signal.eventsocket
¶
Decorator defining a property that holds a single callable slot (like a Delphi event).
Assigning a callable (function, method, lambda, partial) to the property connects it
as the event handler. Assigning None disconnects the current handler. Calling the
property like a method invokes the currently connected handler, passing through
arguments and returning its result.
The decorated function's signature (excluding 'self' but including the return type annotation) defines the required signature for the assigned slot.
Use the is_set() method on the property access to check if a handler is assigned.
Example
class MyComponent:
@eventsocket
def on_update(self, data: dict) -> None:
# Slots must match (data: dict) -> None
pass
def do_update(self)->:
data = {'value': 1}
self.on_update(data) # Call the assigned handler (or pass if not set)
def my_handler(data: dict) -> None:
print(f"Handler received: {data}")
comp = MyComponent()
comp.on_update = my_handler # Connect handler
comp.do_update() # Calls my_handler
comp.on_update = None # Disconnect handler
Important
Signature matching includes parameter names, types, kinds, order, and the return type annotation. The only exception is that the assigned slot may have extra keyword arguments if they have default values.
Storage Note
Similar to Signal, functions and methods are stored using weak references
where appropriate to prevent memory leaks. Lambdas/partials are stored directly.
Source code in firebird/base/signal.py
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 | |