-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5891c38
commit 359c6cd
Showing
1 changed file
with
18 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,21 @@ | ||
from typing import Callable | ||
from typing import Callable, Any | ||
import functools | ||
|
||
|
||
def cache(func: Callable) -> Callable: | ||
# Write your code here | ||
pass | ||
calculated_data = [] | ||
|
||
@functools.wraps(func) | ||
def wrapper(*args, **kwargs) -> Any: | ||
for value in args: | ||
if isinstance(value, (list, set, dict)): | ||
return None | ||
for args_save, kwargs_save, result_save in calculated_data: | ||
if args_save == args and kwargs_save == kwargs: | ||
print("Getting from cache") | ||
return result_save | ||
result = func(*args, **kwargs) | ||
calculated_data.append((args, kwargs, result)) | ||
print("Calculating new result") | ||
return result | ||
return wrapper |