Skip to content

Commit a54ad7f

Browse files
committed
Refactor signal examples and enhanced documentation
1 parent ae7518e commit a54ad7f

10 files changed

+876
-27
lines changed

docs/example.md

Lines changed: 731 additions & 0 deletions
Large diffs are not rendered by default.

examples/signal_async.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# examples/02_signal_async.py
1+
# examples/signal_async.py
22

33
"""
44
Async Signal Example
@@ -7,6 +7,12 @@
77
1. Creating a signal
88
2. Connecting an async slot
99
3. Emitting a signal to async handler
10+
11+
Key Points:
12+
- Demonstrates asynchronous signal-slot communication
13+
- Shows how to use @t_slot decorator with async functions
14+
- Illustrates handling of async slot execution in event loop
15+
- Explains integration of signals with asyncio for non-blocking operations
1016
"""
1117

1218
import asyncio

examples/signal_basic.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1-
# examples/01_signal_basic.py
1+
# examples/signal_basic.py
22

33
"""
4-
Async Signal Example
4+
Basic Signal-Slot Example
55
6-
This example demonstrates the basic usage of TSignal with async slots:
6+
This example demonstrates the fundamental usage of TSignal with a synchronous slot:
77
1. Creating a signal
8-
2. Connecting an async slot
9-
3. Emitting a signal to async handler
8+
2. Connecting a regular method as a slot (without @t_slot)
9+
3. Emitting a signal to trigger slot execution
10+
11+
Key Points:
12+
- Showcases the most basic form of signal-slot connection.
13+
- The slot is a normal instance method of a class, not decorated with @t_slot.
14+
- Emphasizes that even without @t_slot, a callable method can act as a slot.
15+
- Introduces the concept of signal emission and immediate slot execution.
1016
"""
1117

1218
import asyncio
19+
import time
1320
from tsignal.core import t_with_signals, t_signal, t_slot
1421

1522

@@ -34,20 +41,19 @@ def increment(self):
3441

3542

3643
@t_with_signals
37-
class AsyncDisplay:
44+
class Display:
3845
"""
39-
A simple display class that receives count updates and processes them asynchronously.
46+
A simple display class that receives count updates and processes them.
4047
"""
4148

4249
def __init__(self):
4350
self.last_value = None
4451

45-
@t_slot
46-
async def on_count_changed(self, value):
47-
"""Async slot that receives count updates"""
52+
def on_count_changed(self, value):
53+
"""slot that receives count updates"""
4854
print(f"Display processing count: {value}")
49-
# Simulate some async processing
50-
await asyncio.sleep(1)
55+
# Simulate some heavy processing
56+
time.sleep(1)
5157
self.last_value = value
5258
print(f"Display finished processing: {value}")
5359

@@ -59,28 +65,24 @@ async def main():
5965

6066
# Create instances
6167
counter = Counter()
62-
display = AsyncDisplay()
68+
display = Display()
6369

64-
# Connect signal to async slot
65-
counter.count_changed.connect(display, display.on_count_changed)
70+
# Connect signal to slot
71+
counter.count_changed.connect(display.on_count_changed)
6672

67-
print("Starting async counter example...")
73+
print("Starting counter example...")
6874
print("Press Enter to increment counter, or 'q' to quit")
6975
print("(Notice the 1 second delay in processing)")
7076

7177
while True:
72-
# Get input asynchronously
73-
line = await asyncio.get_event_loop().run_in_executor(None, input, "> ")
78+
line = input("> ")
7479

7580
if line.lower() == "q":
7681
break
7782

7883
# Increment counter which will emit signal
7984
counter.increment()
8085

81-
# Give some time for async processing to complete
82-
await asyncio.sleep(0.1)
83-
8486

8587
if __name__ == "__main__":
8688
asyncio.run(main())

examples/signal_function_slots.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# examples/signal_function_slots.py
2+
3+
"""
4+
Signal-Function Slots Example
5+
6+
This example demonstrates how to connect a signal to a standalone function
7+
(not a class method). This highlights that slots can be any callable, not just methods.
8+
9+
Steps:
10+
1. Define a signal in a class (Counter) that emits when its count changes.
11+
2. Define a standalone function that processes the emitted value.
12+
3. Connect the signal to this standalone function as a slot.
13+
4. Emit the signal by incrementing the counter and observe the function being called.
14+
15+
Key Points:
16+
- Illustrates flexibility in choosing slots.
17+
- Standalone functions can serve as slots without additional decorators.
18+
"""
19+
20+
import asyncio
21+
from tsignal.core import t_with_signals, t_signal
22+
23+
24+
@t_with_signals
25+
class Counter:
26+
def __init__(self):
27+
self.count = 0
28+
29+
@t_signal
30+
def count_changed(self):
31+
"""Emitted when the count changes."""
32+
33+
def increment(self):
34+
self.count += 1
35+
print(f"Counter incremented to: {self.count}")
36+
self.count_changed.emit(self.count)
37+
38+
39+
def print_value(value):
40+
"""A standalone function acting as a slot."""
41+
print(f"Function Slot received value: {value}")
42+
43+
44+
async def main():
45+
counter = Counter()
46+
# Connect the signal to the standalone function slot
47+
counter.count_changed.connect(print_value)
48+
49+
print("Press Enter to increment counter, or 'q' to quit.")
50+
while True:
51+
line = input("> ")
52+
if line.lower() == "q":
53+
break
54+
counter.increment()
55+
56+
57+
if __name__ == "__main__":
58+
asyncio.run(main())

examples/signal_lamba_slots.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# examples/signal_lambda_slots.py
2+
3+
"""
4+
Signal-Lambda Slots Example
5+
6+
This example demonstrates connecting a signal to a lambda function slot.
7+
It shows that you can quickly define inline, anonymous slots for simple tasks.
8+
9+
Steps:
10+
1. Define a signal in a class (Counter) that emits when its count changes.
11+
2. Connect the signal to a lambda function that prints the received value.
12+
3. Increment the counter and observe the lambda being called.
13+
14+
Key Points:
15+
- Demonstrates that slots can be lambdas (anonymous functions).
16+
- Useful for quick, inline actions without defining a separate function or method.
17+
"""
18+
19+
import asyncio
20+
from tsignal.core import t_with_signals, t_signal
21+
22+
23+
@t_with_signals
24+
class Counter:
25+
def __init__(self):
26+
self.count = 0
27+
28+
@t_signal
29+
def count_changed(self):
30+
"""Emitted when the count changes."""
31+
32+
def increment(self):
33+
self.count += 1
34+
print(f"Counter incremented to: {self.count}")
35+
self.count_changed.emit(self.count)
36+
37+
38+
async def main():
39+
counter = Counter()
40+
# Connect the signal to a lambda slot
41+
counter.count_changed.connect(lambda v: print(f"Lambda Slot received: {v}"))
42+
43+
print("Press Enter to increment counter, or 'q' to quit.")
44+
while True:
45+
line = input("> ")
46+
if line.lower() == "q":
47+
break
48+
counter.increment()
49+
50+
51+
if __name__ == "__main__":
52+
asyncio.run(main())

examples/stock_monitor_console.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# examples/06_stock_monitor_console.py
1+
# examples/stock_monitor_console.py
22

33
"""
44
Stock monitor console example.

examples/stock_monitor_simple.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# examples/05_stock_monitor_simple_updated.py
1+
# examples/stock_monitor_simple.py
22

33
"""
44
Stock monitor simple example.

examples/stock_monitor_ui.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# examples/07_stock_monitor_ui.py
1+
# examples/stock_monitor_ui.py
22

33
"""
44
Stock monitor UI example.

examples/thread_basic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# examples/03_thread_basic.py
1+
# examples/thread_basic.py
22

33
"""
44
Thread Communication Example

examples/thread_worker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# examples/04_thread_worker.py
1+
# examples/thread_worker.py
22

33
"""
44
Thread Worker Pattern Example

0 commit comments

Comments
 (0)