-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
45 lines (31 loc) · 929 Bytes
/
Copy pathexample.py
File metadata and controls
45 lines (31 loc) · 929 Bytes
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
"""functools — lru_cache, partial, reduce, and wraps."""
import functools
from operator import add
@functools.lru_cache(maxsize=128)
def fibonacci(n):
if n < 2:
return n
return fibonacci(n - 1) + fibonacci(n - 2)
def multiply(a, b, c):
return a * b * c
def pipeline(*functions):
def run(value):
for func in functions:
value = func(value)
return value
return run
if __name__ == "__main__":
print("=== lru_cache ===")
print(fibonacci(30))
print(fibonacci.cache_info())
print("\n=== partial ===")
double = functools.partial(multiply, 2)
print(double(5, 3))
print("\n=== reduce ===")
print(functools.reduce(add, [1, 2, 3, 4, 5]))
print("\n=== pipeline ===")
process = pipeline(str.strip, str.upper)
print(process(" hello "))
print("\n=== cache_clear ===")
fibonacci.cache_clear()
print("Cache cleared")