Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Conversation

Lyutillis
Copy link

No description provided.

app/main.py Outdated
pass
stored_runs = {}

def wrapper(*args) -> int:

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.

@Shatrovskyi
Copy link

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)})

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]

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

Copy link

@LiudmylaKulinchenko LiudmylaKulinchenko left a comment

Choose a reason for hiding this comment

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

Great! ☀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants