Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
from typing import Callable
from functools import wraps
from typing import Any, Callable


def cache(func: Callable) -> Callable:
# Write your code here
pass
cached = {}

@wraps(func)
def wrapper(*args, **kwargs) -> Any:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The kwargs parameter is accepted but never used in the cache key. If someone calls the decorated function with keyword arguments like func(a=1, b=2), the cache won't recognize it as the same call as func(1, 2). Either include kwargs in the cache key or document that keyword arguments are not supported.

key = (args, frozenset(kwargs.items()))
if cached.get(key, None) is None:
cached[key] = func(*args, **kwargs)
print("Calculating new result")
else:
print("Getting from cache")
return cached[key]
return wrapper
Loading