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

Create cache decorator #1427

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

Conversation

ihorhalyskiy
Copy link

No description provided.

Copy link

@4ikkamony 4ikkamony left a comment

Choose a reason for hiding this comment

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

Nice job!
But a few things to note: two return statements are unnecessary—you can go with just one. Like this:

if cache_data_check in cached_data:
    print("Getting from cache")
else:
    result = func(*args)
    cached_data.update({cache_data_check: result})
    print("Calculating new result")
return cached_data[cache_data_check]

This way, when you reach the return statement, it's guaranteed that the key you want to store is there, because it was either already there, or you just added it if it wasn't.

It's a minor detail, but good to know)

Also, you have a few typos:

Not "cash" but "cache".
Not "cashed" but "cached." Cash is money)
Oh, and your wrapper doesn't actually return None, as you specified in the type hint. It returns whatever the decorated function returns. This is why you should use typing.Any as the return type. Like this:

from typing import Callable, Any
...
def wrapper(*args) -> Any:
    ...

Otherwise, as I said, great work! Fix this minor stuff, and it'll be outright perfect)

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Sorry, the pull request review was unsuccessful for the following reasons:

Warning

Too many changes in the pull request. Please make sure that you have added only the necessary changes to the pull request.
[CODE: 6]

If the issue is within your control, please address it.
The next review attempt will automatically launch in 1 hour.
If you believe this is an error or need assistance, please contact the support team.

Copy link

@4ikkamony 4ikkamony left a comment

Choose a reason for hiding this comment

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

Nice

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Sorry, the pull request review was unsuccessful for the following reasons:

Warning

Too many changes in the pull request. Please make sure that you have added only the necessary changes to the pull request.
[CODE: 6]

If the issue is within your control, please address it.
The next review attempt will automatically launch in 1 hour.
If you believe this is an error or need assistance, please contact the support team.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Sorry, the pull request review was unsuccessful for the following reasons:

Warning

Too many changes in the pull request. Please make sure that you have added only the necessary changes to the pull request.
[CODE: 6]

If the issue is within your control, please address it.
The next review attempt will automatically launch in 1 hour.
If you believe this is an error or need assistance, please contact the support team.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Sorry, the pull request review was unsuccessful for the following reasons:

Warning

Too many changes in the pull request. Please make sure that you have added only the necessary changes to the pull request.
[CODE: 6]

If the issue is within your control, please address it.
The next review attempt will automatically launch in 1 hour.
If you believe this is an error or need assistance, please contact the support team.

Copy link

@4ikkamony 4ikkamony left a comment

Choose a reason for hiding this comment

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

Even nicer

app/main.py Outdated

def wrapper(*args) -> None:

cache_data_check = args
Copy link

Choose a reason for hiding this comment

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

You don't need a variable here

app/main.py Outdated
pass
cached_data = {}

def wrapper(*args) -> None:
Copy link

Choose a reason for hiding this comment

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

It looks that you can return not only None

app/main.py Outdated
Comment on lines 14 to 15
result = func(*args)
cached_data.update({cache_data_check: result})
Copy link

Choose a reason for hiding this comment

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

Try to write it into one line, and think about do you need an update method here, or it can be simplified

Copy link

@L1nk3rrr L1nk3rrr left a comment

Choose a reason for hiding this comment

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

Check my comments to improve your code!

@ihorhalyskiy ihorhalyskiy requested a review from L1nk3rrr December 2, 2024 17:42
app/main.py Outdated
pass
cached_data = {}

def wrapper(*args) -> Callable:
Copy link

Choose a reason for hiding this comment

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

Wrapper return value from function that was calculated, looks like Any type, not Callable here

app/main.py Outdated
Comment on lines 9 to 15
if args in cached_data:
print("Getting from cache")
return cached_data[args]
else:
print("Calculating new result")
cached_data[args] = func(*args)
return cached_data[args]
Copy link

Choose a reason for hiding this comment

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

double return looks bad, maybe we try can to use only return one here?

@ihorhalyskiy ihorhalyskiy requested a review from L1nk3rrr December 3, 2024 19:26
Copy link

@petrykivd petrykivd left a comment

Choose a reason for hiding this comment

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

Good job! 😎

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.

5 participants