-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdecorators_advanced.py
More file actions
451 lines (367 loc) · 14.4 KB
/
Copy pathdecorators_advanced.py
File metadata and controls
451 lines (367 loc) · 14.4 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
"""
Advanced Decorators + Type Hints
Learning: custom decorators, functools, typing, generics
"""
import time
import functools
import logging
from typing import (
TypeVar, Generic, Callable, Any, Dict, List, Optional,
Union, Tuple, Protocol, runtime_checkable
)
from datetime import datetime, timedelta
from collections import defaultdict
import asyncio
import inspect
# Logging setup
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s')
logger = logging.getLogger(__name__)
# Type Variables for Generic types
T = TypeVar('T')
F = TypeVar('F', bound=Callable[..., Any])
R = TypeVar('R') # Return type
# 1. TIMER DECORATOR
def timer(func: F) -> F:
"""
Decorator for measuring function execution time
Supports both synchronous and asynchronous functions
"""
if asyncio.iscoroutinefunction(func):
@functools.wraps(func)
async def async_wrapper(*args: Any, **kwargs: Any) -> Any:
start_time = time.perf_counter()
try:
result = await func(*args, **kwargs)
return result
finally:
end_time = time.perf_counter()
execution_time = end_time - start_time
logger.info(f"⏱️ {func.__name__} executed in {execution_time:.4f} seconds")
return async_wrapper # type: ignore
else:
@functools.wraps(func)
def sync_wrapper(*args: Any, **kwargs: Any) -> Any:
start_time = time.perf_counter()
try:
result = func(*args, **kwargs)
return result
finally:
end_time = time.perf_counter()
execution_time = end_time - start_time
logger.info(f"⏱️ {func.__name__} executed in {execution_time:.4f} seconds")
return sync_wrapper # type: ignore
# 2. RETRY DECORATOR FOR ERRORS
def retry(
max_attempts: int = 3,
delay: float = 1.0,
backoff: float = 2.0,
exceptions: Tuple[type, ...] = (Exception,)
) -> Callable[[F], F]:
"""
Decorator for retrying function on errors
Args:
max_attempts: Maximum number of attempts
delay: Initial delay between attempts (seconds)
backoff: Multiplier for increasing delay
exceptions: Exception types to retry on
"""
def decorator(func: F) -> F:
@functools.wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> Any:
current_delay = delay
last_exception = None
for attempt in range(max_attempts):
try:
return func(*args, **kwargs)
except exceptions as e:
last_exception = e
if attempt == max_attempts - 1: # Last attempt
logger.error(f"❌ {func.__name__} failed to execute after {max_attempts} attempts")
raise e
logger.warning(f"🔄 {func.__name__} attempt {attempt + 1} failed: {e}")
logger.info(f"⏳ Waiting {current_delay:.2f} seconds...")
time.sleep(current_delay)
current_delay *= backoff
# This code should never execute, but for typing
if last_exception:
raise last_exception
return wrapper # type: ignore
return decorator
# 3. ADVANCED CACHING DECORATOR
class CacheStats:
"""Cache statistics"""
def __init__(self) -> None:
self.hits: int = 0
self.misses: int = 0
self.cache_size: int = 0
@property
def hit_rate(self) -> float:
total = self.hits + self.misses
return (self.hits / total * 100) if total > 0 else 0.0
def __str__(self) -> str:
return f"Cache(hits={self.hits}, misses={self.misses}, hit_rate={self.hit_rate:.1f}%, size={self.cache_size})"
def cache(
maxsize: Optional[int] = 128,
ttl: Optional[float] = None,
typed: bool = False
) -> Callable[[F], F]:
"""
Advanced caching decorator
Args:
maxsize: Maximum cache size (None = unlimited)
ttl: Time to live for entries in seconds (None = forever)
typed: Distinguish argument types (True/False)
"""
def decorator(func: F) -> F:
cache_data: Dict[str, Tuple[Any, float]] = {}
stats = CacheStats()
def make_key(*args: Any, **kwargs: Any) -> str:
"""Create cache key"""
key_parts = []
# Add positional arguments
for arg in args:
if typed:
key_parts.append(f"{type(arg).__name__}:{arg}")
else:
key_parts.append(str(arg))
# Add keyword arguments
for k, v in sorted(kwargs.items()):
if typed:
key_parts.append(f"{k}={type(v).__name__}:{v}")
else:
key_parts.append(f"{k}={v}")
return "|".join(key_parts)
def is_expired(timestamp: float) -> bool:
"""Check TTL expiration"""
if ttl is None:
return False
return time.time() - timestamp > ttl
def cleanup_expired() -> None:
"""Clean up expired entries"""
if ttl is None:
return
current_time = time.time()
expired_keys = [
key for key, (_, timestamp) in cache_data.items()
if current_time - timestamp > ttl
]
for key in expired_keys:
del cache_data[key]
stats.cache_size = len(cache_data)
@functools.wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> Any:
# Clean up expired entries
cleanup_expired()
# Create key
cache_key = make_key(*args, **kwargs)
# Check cache
if cache_key in cache_data:
value, timestamp = cache_data[cache_key]
if not is_expired(timestamp):
stats.hits += 1
logger.debug(f"💾 Cache HIT for {func.__name__}")
return value
else:
# Remove expired entry
del cache_data[cache_key]
# Calculate value
stats.misses += 1
logger.debug(f"🔍 Cache MISS for {func.__name__}")
result = func(*args, **kwargs)
# Save to cache
current_time = time.time()
cache_data[cache_key] = (result, current_time)
# Check cache size
if maxsize is not None and len(cache_data) > maxsize:
# Remove oldest entry (simple strategy)
oldest_key = min(cache_data.keys(),
key=lambda k: cache_data[k][1])
del cache_data[oldest_key]
stats.cache_size = len(cache_data)
return result
# Add cache management methods
wrapper.cache_info = lambda: stats # type: ignore
wrapper.cache_clear = lambda: cache_data.clear() # type: ignore
return wrapper # type: ignore
return decorator
# 4. VALIDATION DECORATOR WITH PROTOCOLS
@runtime_checkable
class Validator(Protocol):
"""Protocol for validators"""
def validate(self, value: Any) -> bool:
...
def get_error_message(self, value: Any) -> str:
...
class RangeValidator:
"""Number range validator"""
def __init__(self, min_val: float, max_val: float) -> None:
self.min_val = min_val
self.max_val = max_val
def validate(self, value: Any) -> bool:
return isinstance(value, (int, float)) and self.min_val <= value <= self.max_val
def get_error_message(self, value: Any) -> str:
return f"Value {value} must be in range [{self.min_val}, {self.max_val}]"
class TypeValidator:
"""Type validator"""
def __init__(self, expected_type: type) -> None:
self.expected_type = expected_type
def validate(self, value: Any) -> bool:
return isinstance(value, self.expected_type)
def get_error_message(self, value: Any) -> str:
return f"Expected type {self.expected_type.__name__}, got {type(value).__name__}"
def validate_args(**validators: Validator) -> Callable[[F], F]:
"""
Decorator for function argument validation
Usage:
@validate_args(
age=RangeValidator(0, 150),
name=TypeValidator(str)
)
def create_user(name: str, age: int) -> User:
...
"""
def decorator(func: F) -> F:
# Get function parameter information
sig = inspect.signature(func)
param_names = list(sig.parameters.keys())
@functools.wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> Any:
# Create dictionary of all arguments
bound_args = sig.bind(*args, **kwargs)
bound_args.apply_defaults()
# Validate each argument
for param_name, validator in validators.items():
if param_name in bound_args.arguments:
value = bound_args.arguments[param_name]
if not validator.validate(value):
error_msg = validator.get_error_message(value)
raise ValueError(f"Validation of parameter '{param_name}' failed: {error_msg}")
return func(*args, **kwargs)
return wrapper # type: ignore
return decorator
# 5. LOGGING DECORATOR WITH CONTEXT
def log_calls(
level: int = logging.INFO,
include_args: bool = True,
include_result: bool = True,
max_arg_length: int = 100
) -> Callable[[F], F]:
"""
Decorator for logging function calls
"""
def decorator(func: F) -> F:
@functools.wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> Any:
# Prepare argument information
args_info = ""
if include_args:
args_str = ", ".join([
str(arg)[:max_arg_length] + ("..." if len(str(arg)) > max_arg_length else "")
for arg in args
])
kwargs_str = ", ".join([
f"{k}={str(v)[:max_arg_length]}" + ("..." if len(str(v)) > max_arg_length else "")
for k, v in kwargs.items()
])
all_args = [args_str, kwargs_str] if args_str and kwargs_str else [args_str or kwargs_str]
args_info = f"({', '.join(filter(None, all_args))})"
logger.log(level, f"🔵 Calling {func.__name__}{args_info}")
try:
result = func(*args, **kwargs)
if include_result:
result_str = str(result)[:max_arg_length]
if len(str(result)) > max_arg_length:
result_str += "..."
logger.log(level, f"✅ {func.__name__} returned: {result_str}")
else:
logger.log(level, f"✅ {func.__name__} executed successfully")
return result
except Exception as e:
logger.log(logging.ERROR, f"❌ {func.__name__} raised exception: {e}")
raise
return wrapper # type: ignore
return decorator
# DEMONSTRATION OF ALL DECORATORS
class MathOperations:
"""Class for demonstrating decorators"""
@timer
@cache(maxsize=50, ttl=10.0)
@log_calls(include_result=True)
def fibonacci(self, n: int) -> int:
"""Calculate Fibonacci number with caching"""
if n <= 1:
return n
return self.fibonacci(n - 1) + self.fibonacci(n - 2)
@retry(max_attempts=3, delay=0.1, exceptions=(ValueError, ZeroDivisionError))
@validate_args(
a=TypeValidator(float),
b=TypeValidator(float)
)
@timer
def divide(self, a: float, b: float) -> float:
"""Division with retry on errors"""
if b == 0:
raise ZeroDivisionError("Division by zero!")
return a / b
@cache(maxsize=10)
@validate_args(
base=RangeValidator(1, 1000),
exponent=RangeValidator(0, 10)
)
def power(self, base: float, exponent: float) -> float:
"""Power calculation with validation"""
time.sleep(0.1) # Simulate heavy computation
return base ** exponent
# Async functions with decorators
@timer
async def async_operation(duration: float) -> str:
"""Async operation with timing"""
await asyncio.sleep(duration)
return f"Operation completed in {duration} seconds"
def demo_decorators() -> None:
"""Demonstration of all decorators"""
print("🎭 ADVANCED DECORATORS DEMONSTRATION\n")
math_ops = MathOperations()
# 1. Caching + logging
print("1️⃣ Fibonacci caching:")
print(f"fibonacci(10) = {math_ops.fibonacci(10)}")
print(f"fibonacci(10) = {math_ops.fibonacci(10)}") # From cache
print(f"Cache info: {math_ops.fibonacci.cache_info()}")
print()
# 2. Validation + retry
print("2️⃣ Validation and retry:")
try:
result = math_ops.divide(10.0, 2.0)
print(f"10 / 2 = {result}")
except Exception as e:
print(f"Error: {e}")
try:
math_ops.divide("10", 2.0) # Type error
except ValueError as e:
print(f"Validation error: {e}")
print()
# 3. Range validation
print("3️⃣ Range validation:")
try:
result = math_ops.power(2.0, 3.0)
print(f"2^3 = {result}")
result = math_ops.power(2.0, 3.0) # From cache
print(f"2^3 = {result} (from cache)")
except ValueError as e:
print(f"Validation error: {e}")
try:
math_ops.power(2000.0, 3.0) # Out of range
except ValueError as e:
print(f"Validation error: {e}")
print()
async def demo_async_decorators() -> None:
"""Demonstration of async decorators"""
print("4️⃣ Async decorators:")
result = await async_operation(0.5)
print(f"Result: {result}")
if __name__ == "__main__":
# Synchronous demonstration
demo_decorators()
# Asynchronous demonstration
print("\n" + "="*50)
asyncio.run(demo_async_decorators())