-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cache decorator solution #730
base: master
Are you sure you want to change the base?
Conversation
app/main.py
Outdated
pass | ||
stored_runs = {} | ||
|
||
def wrapper(*args) -> int: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can't always guarantee that the function you run will return int
type.
In my opinion, for more readable code it's nice to use one return with the ternary operator in wrapper(). |
app/main.py
Outdated
print("Getting from cache") | ||
return stored_runs[args] | ||
print("Calculating new result") | ||
stored_runs.update({args: func(*args)}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use stored_runs[args] = func(*args)
, your approach introduces unnecessary complexity without any significant advantage. Also it will be more pythonic
app/main.py
Outdated
def wrapper(*args) -> Any: | ||
if args in stored_runs: | ||
print("Getting from cache") | ||
return stored_runs[args] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rewrite without having return duplicated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! ☀
No description provided.