-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoraters.py
More file actions
64 lines (51 loc) · 1.78 KB
/
decoraters.py
File metadata and controls
64 lines (51 loc) · 1.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
#Question No 1.
import time
# Decorator to calculate execution time
def timer(func):
def wrapper(*args, **kwargs):
start = time.time() # Record start time
result = func(*args, **kwargs) # Call the actual function
end = time.time() # Record end time
print(f"{func.__name__} ran in {end - start:.4f} seconds")
return result
return wrapper
@timer
def example_function(n):
time.sleep(n) # Simulate a long task
example_function(2) # OUTPUT: example_function ran in 2.00XX seconds
#Question No 2.
# Decorator to debug function call
def debug(func):
def wrapper(*args, **kwargs):
args_value = ', '.join(str(arg) for arg in args)
kwargs_value = ', '.join(f"{k}={v}" for k, v in kwargs.items())
print(f"Calling: {func.__name__} with args ({args_value}) and kwargs {{{kwargs_value}}}")
return func(*args, **kwargs)
return wrapper
@debug
def greet(name, greeting="Hello"):
print(f"{greeting}, {name}!")
greet("chai", greeting="hanji")
# OUTPUT:
# Calling: greet with args (chai) and kwargs {greeting=hanji}
# hanji, chai!
#Question No 3.
import time
# Decorator to cache function results
def cache(func):
cache_value = {} # Dictionary to store previous results
def wrapper(*args):
if args in cache_value:
print(f"Fetching from cache for {args}")
return cache_value[args]
print(f"Computing result for {args}")
result = func(*args)
cache_value[args] = result # Save result to cache
return result
return wrapper
@cache
def long_running_function(a, b):
time.sleep(4) # Simulate expensive computation
return a + b
print(long_running_function(2, 3)) # Takes 4 seconds first time
print(long_running_function(2, 3)) # Instant the second time