-
Notifications
You must be signed in to change notification settings - Fork 0
/
memogen.py
37 lines (27 loc) · 806 Bytes
/
memogen.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
from functools import wraps
from lru_cache import lru_cache
from pickle import dumps
"""Decorator class for performing automatic memoization."""
class memo(object):
def __init__(self, cache):
self.cache = cache
def __call__(self, func):
# Decorator to update wrapper to look like func.
@wraps(func)
def wrapper(*args, **kwargs):
# Ensure key is immutable.
key = dumps((args, kwargs))
if key not in self.cache:
self.cache[key] = func(*args, **kwargs)
return self.cache[key]
return wrapper
@memo(lru_cache(3))
def fibo(n):
if n == 0 or n == 1:
return n
else:
return fibo(n - 1) + fibo(n - 2)
def main():
print fibo(200)
if __name__ == "__main__":
main()