Skip to content
Open
Changes from 2 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: 15 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
from typing import Callable
import functools

g_results = {}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This violates the checklist item: "Use descriptive and correct variable names." The global name g_results (line 4) is not very descriptive. Consider renaming it to something clearer like _cache, cache_store, or results_cache so intent is immediately obvious.



def cache(func: Callable) -> Callable:
# Write your code here
pass
@functools.wraps(func)
def wrapper(*args,**kwargs) -> Callable:
key = (func, args, tuple(sorted(kwargs.items())))
if key in g_results:
print("Getting from cache")
result = g_results.get(key)
else:
print("Calculating new result")
result = func(*args,**kwargs)
g_results[key] = result
return result
return wrapper
Loading