Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from typing import (
- Callable,
- TypeVar,
- )
- from functools import wraps
- T = TypeVar("T")
- def lru_cache(capacity: int) -> Callable[[T], T]:
- try:
- capacity = round(capacity)
- except:
- raise TypeError
- if capacity < 1:
- raise ValueError
- res = {}
- def take_in_func(func):
- @wraps(func)
- def wrapper(*args, **kwargs):
- arg = args[0]
- if arg not in res:
- res[arg] = func(*args, **kwargs)
- if len(res) > capacity:
- for k in res.keys():
- res.pop(k)
- break
- else:
- res[arg] = res.pop(arg)
- return res[arg]
- return wrapper
- return take_in_func
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement