-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctools_module.py
223 lines (156 loc) · 6.15 KB
/
functools_module.py
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
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
222
223
"""
Functools Module Examples
This module demonstrates various utilities from the functools module including
partial, lru_cache, reduce, singledispatch, and total_ordering.
"""
import time
from functools import lru_cache, partial, reduce, singledispatch, total_ordering, wraps
from typing import Any, Callable, Dict, List, TypeVar, cast
# ----- partial: Creating specialized functions -----
def multiply(x: float, y: float) -> float:
"""Multiply two numbers."""
return x * y
# Create specialized functions using partial
double = partial(multiply, 2)
triple = partial(multiply, 3)
# Example with keyword arguments
def format_text(text: str, prefix: str = "", suffix: str = "") -> str:
"""Format text with optional prefix and suffix."""
return f"{prefix}{text}{suffix}"
# Create a function that wraps text in brackets
bracket_text = partial(format_text, prefix="[", suffix="]")
# ----- lru_cache: Memoization for performance -----
@lru_cache(maxsize=128)
def fibonacci(n: int) -> int:
"""Calculate Fibonacci numbers with caching."""
if n <= 1:
return n
return fibonacci(n - 1) + fibonacci(n - 2)
def fibonacci_no_cache(n: int) -> int:
"""Calculate Fibonacci numbers without caching (for comparison)."""
if n <= 1:
return n
return fibonacci_no_cache(n - 1) + fibonacci_no_cache(n - 2)
def demonstrate_lru_cache() -> None:
"""Show the performance benefits of lru_cache."""
n = 35
# Measure time with caching
start = time.time()
result1 = fibonacci(n)
cached_time = time.time() - start
# Clear the cache
fibonacci.cache_clear()
# Measure first-time call with empty cache
start = time.time()
fibonacci(n) # Store result without unused variable
first_time = time.time() - start
# Measure time without caching
start = time.time()
fibonacci_no_cache(n - 10) # Use smaller n to avoid excessive time
no_cache_time = time.time() - start
print(f"Fibonacci({n}) = {result1}")
print(f"Time with warmed cache: {cached_time:.6f} seconds")
print(f"Time with cold cache: {first_time:.6f} seconds")
print(f"Time without cache (n={n-10}): {no_cache_time:.6f} seconds")
# ----- reduce: Cumulative operations -----
def sum_numbers(numbers: List[int]) -> int:
"""Sum a list of numbers using reduce."""
return reduce(lambda a, b: a + b, numbers, 0)
def factorial(n: int) -> int:
"""Calculate factorial using reduce."""
return reduce(lambda a, b: a * b, range(1, n + 1), 1)
def concatenate_strings(strings: List[str], separator: str = "") -> str:
"""Concatenate a list of strings using reduce."""
if not strings:
return ""
return reduce(lambda a, b: f"{a}{separator}{b}", strings)
# ----- singledispatch: Function overloading based on type -----
@singledispatch
def process_data(data: Any) -> str:
"""Process data based on its type."""
return f"Unknown type: {type(data).__name__}"
@process_data.register
def _(data: str) -> str:
return f"Processing string: {data.upper()}"
@process_data.register
def _(data: int) -> str:
return f"Processing integer: {data * 2}"
@process_data.register
def _(data: list) -> str:
return f"Processing list with {len(data)} items"
@process_data.register(dict) # Alternative registration syntax
def _(data: Dict[Any, Any]) -> str:
return f"Processing dictionary with {len(data)} keys"
# ----- total_ordering: Complete comparison operations -----
@total_ordering
class Person:
"""Person class with comparison operations defined."""
def __init__(self, name: str, age: int) -> None:
self.name = name
self.age = age
def __eq__(self, other: Any) -> bool:
if not isinstance(other, Person):
return NotImplemented
return self.age == other.age
def __lt__(self, other: Any) -> bool:
if not isinstance(other, Person):
return NotImplemented
return self.age < other.age
def __repr__(self) -> str:
return f"Person(name='{self.name}', age={self.age})"
# ----- wraps: Preserve metadata in decorators -----
T = TypeVar("T", bound=Callable[..., Any])
def debug(func: T) -> T:
"""Decorator to print function name and arguments."""
@wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> Any:
"""Wrapper preserving original function metadata."""
print(f"Calling {func.__name__} with args: {args}, kwargs: {kwargs}")
result = func(*args, **kwargs)
print(f"{func.__name__} returned: {result}")
return result
return cast(T, wrapper)
@debug
def greet(name: str) -> str:
"""Function with a docstring that should be preserved."""
return f"Hello, {name}!"
# Example usage
if __name__ == "__main__":
# partial examples
print("\n--- partial examples ---")
print(f"double(5) = {double(5)}")
print(f"triple(5) = {triple(5)}")
print(f"bracket_text('example') = {bracket_text('example')}")
# lru_cache examples
print("\n--- lru_cache examples ---")
demonstrate_lru_cache()
# reduce examples
print("\n--- reduce examples ---")
print(f"sum_numbers([1, 2, 3, 4, 5]) = {sum_numbers([1, 2, 3, 4, 5])}")
print(f"factorial(5) = {factorial(5)}")
print(
f"concatenate_strings(['Hello', 'World']) = {concatenate_strings(['Hello', 'World'])}"
)
print(
f"concatenate_strings(['a', 'b', 'c'], '-') = {concatenate_strings(['a', 'b', 'c'], '-')}"
)
# singledispatch examples
print("\n--- singledispatch examples ---")
print(process_data("hello"))
print(process_data(42))
print(process_data([1, 2, 3]))
print(process_data({"a": 1, "b": 2}))
print(process_data(3.14)) # Uses default implementation
# total_ordering examples
print("\n--- total_ordering examples ---")
alice = Person("Alice", 30)
bob = Person("Bob", 25)
charlie = Person("Charlie", 30)
print(f"{alice} > {bob}: {alice > bob}")
print(f"{alice} == {charlie}: {alice == charlie}")
print(f"{bob} <= {charlie}: {bob <= charlie}")
# wraps examples
print("\n--- wraps examples ---")
print(f"greet.__name__: {greet.__name__}")
print(f"greet.__doc__: {greet.__doc__}")
result = greet("Claude")