-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercises.py
More file actions
93 lines (73 loc) · 3.78 KB
/
Copy pathexercises.py
File metadata and controls
93 lines (73 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
Chapter 10: Exercises — Design Patterns with First-Class Functions
==================================================================
Original exercises applying functional refactoring to classic patterns.
"""
import sys
from typing import Callable
sys.stdout.reconfigure(encoding="utf-8")
def section(title: str) -> None:
print(f"\n{'=' * 60}\n=== {title}\n{'=' * 60}")
# ─────────────────────────────────────────────────────────────────────────────
# Exercise 1: Refactor Template Method
# ─────────────────────────────────────────────────────────────────────────────
# The Template Method pattern defines the skeleton of an algorithm in a base class,
# letting subclasses override specific steps.
# Refactor this class-based approach into a single function that accepts Callables
# for the specific steps.
# THE OLD WAY:
class ReportGenerator:
def generate(self) -> str:
data = self.fetch_data()
return self.format_data(data)
def fetch_data(self) -> str: raise NotImplementedError
def format_data(self, data: str) -> str: raise NotImplementedError
# TODO: Write a purely functional `generate_report`
def generate_report(fetcher: Callable[[], str], formatter: Callable[[str], str]) -> str:
# Implement the template structure here
return formatter(fetcher())
def test_ex1_template() -> None:
section("Exercise 1: Functional Template Method")
def get_db_data() -> str: return "raw_db_row"
def html_format(d: str) -> str: return f"<b>{d}</b>"
res = generate_report(get_db_data, html_format)
print(f"Generated report: {res}")
assert res == "<b>raw_db_row</b>", "Exercise 1 failed"
print("✓ Exercise 1 passed")
# ─────────────────────────────────────────────────────────────────────────────
# Exercise 2: Building a Command Queue
# ─────────────────────────────────────────────────────────────────────────────
# Implement a `CommandQueue` class. It should accept Callables.
# It has an `add_task(func)` method and a `run_all()` method.
# If a function fails, the queue should catch the error, print it, and continue.
class CommandQueue:
def __init__(self):
# TODO: Initialize tasks list
self.tasks: list[Callable] = []
def add_task(self, task: Callable) -> None:
# TODO: Add to list
self.tasks.append(task)
def run_all(self) -> int:
"""Runs all tasks and returns the number of successful tasks."""
# TODO: Implement robust execution loop
success_count = 0
for task in self.tasks:
try:
task()
success_count += 1
except Exception as e:
print(f"Task failed: {e}")
return success_count
def test_ex2_command_queue() -> None:
section("Exercise 2: Command Queue")
queue = CommandQueue()
queue.add_task(lambda: print("Task 1 Done"))
queue.add_task(lambda: 1 / 0) # Will fail
queue.add_task(lambda: print("Task 3 Done"))
successes = queue.run_all()
print(f"Successful tasks: {successes}")
assert successes == 2, "Exercise 2 failed"
print("✓ Exercise 2 passed")
if __name__ == "__main__":
test_ex1_template()
test_ex2_command_queue()