-
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
'Solution' #1399
base: master
Are you sure you want to change the base?
'Solution' #1399
Conversation
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.
Hello! 😊 It looks like there are a few critical issues that need to be addressed before we can approve this pull request. Let's go through them:
-
Decorator Functionality: The
cache
decorator is not functioning as expected. It should store the results of function calls with different arguments and return cached results for repeated arguments. Currently, it simply calls the function and returns its result without caching, which is a core requirement of the task. -
Output Messages: The decorator should print specific messages ("Getting from cache" or "Calculating new result") based on whether it is returning a cached result or computing a new one. This functionality is missing, which is important for understanding the behavior of the decorator.
-
Handling Multiple Functions: The decorator should be able to handle multiple functions simultaneously, ensuring that cached results are managed separately for each function. This aspect is not addressed in the current implementation.
-
Return Type of
wrapper
Function: The return type of thewrapper
function should match the return type of the decorated function. Consider changing-> None
to-> Any
or the specific return type offunc
.
These issues significantly impact the functionality and core requirements of the task. Once these are resolved, your code will be much closer to meeting the task requirements. Keep up the great work, and don't hesitate to reach out if you need any help! 🚀
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
🔄 Fix the issues, commit the changes, and then re-request my review using the Re-request review
button in the Reviewers tab.
app/main.py
Outdated
@@ -2,5 +2,8 @@ | |||
|
|||
|
|||
def cache(func: Callable) -> Callable: | |||
# Write your code here | |||
pass | |||
def wrapper(*args, **kwargs) -> None: |
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.
The return type of the wrapper
function is currently None
, but it should match the return type of the function it decorates. Consider changing -> None
to -> Any
or the specific return type of func
.
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.
Thank you for your submission! 🎉 While your current implementation of the cache
decorator in app/main.py
is a great start, there are a few critical areas that need improvement to fully meet the task requirements:
-
Caching Functionality: The core requirement of caching the results of function calls with different arguments is missing. Ensure that the decorator stores results and returns cached results for repeated arguments.
-
Output Messages: The decorator should print specific messages: "Getting from cache" when returning a cached result and "Calculating new result" when performing a new computation. This will help in understanding the behavior of the caching mechanism.
-
Handling Multiple Functions: The decorator should be capable of managing cached results separately for each function it decorates. This aspect is crucial for the correct functionality of the cache.
-
Return Type of
wrapper
Function: Consider aligning the return type of thewrapper
function with the return type of the decorated function for better type consistency.
These improvements are essential for the functionality of your code. Please review the comments and address these issues to enhance your solution. Remember, learning from these points will be beneficial for your growth as a developer. Keep up the good work and continue to improve! 🚀
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
app/main.py
Outdated
def wrapper(*args, **kwargs) -> Any: | ||
result = func(*args, **kwargs) | ||
return result |
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.
The wrapper
function should implement caching logic. It should store results of function calls and return cached results for repeated arguments. Additionally, it should print 'Getting from cache' if a cached result is used, or 'Calculating new result' if a new computation is performed. This functionality is currently missing.
def wrapper(*args, **kwargs) -> Any: | ||
result = func(*args, **kwargs) | ||
return result | ||
return wrapper |
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.
The cache
decorator should handle multiple functions simultaneously, ensuring that cached results are managed separately for each function. This aspect is not addressed in the current implementation.
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.
Tests Failed!
app/main.py
Outdated
for _ in range(numero): | ||
results.append(func(*args, **kwargs)) | ||
for result in results: | ||
if results.count(result) > 1: | ||
print(f"Getting from cache {result}") | ||
else: | ||
print(f"Calculating new result {result}") |
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.
try to solve this task without using loops
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.
Tests Failed!
Before submitting the task for review, ensure that your code passes all tests.
app/main.py
Outdated
def cache(func: Callable) -> Any: | ||
results = {} | ||
|
||
def wrapper(*args) -> Any: |
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.
what if the function will also accept key arguments?
you should also consider this point
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.
Per task conditions, the functions take only immutable arguments. Am I correct, or is the point of the question in anything else?
app/main.py
Outdated
results = {} | ||
|
||
def wrapper(*args, **kwargs) -> Any: | ||
if args in results: |
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 should create a key based on both keyword and positional arguments, rather than using only args
as the key. This is because the function might accept the same args
but different kwargs
.
app/main.py
Outdated
if key in results: | ||
print("Getting from cache") | ||
return results[key] | ||
else: |
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.
else
operator is redundant, you can remove it
app/main.py
Outdated
key = tuple(args) | ||
key += tuple(*kwargs.items()) | ||
if key in results: |
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.
try to write it in one line
app/main.py
Outdated
|
||
def wrapper(*args, **kwargs) -> Any: |
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 wraps
results = {} | ||
wraps(func) | ||
|
||
def wrapper(*args, **kwargs) -> Any: |
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.
@wraps(func)
def wrapper(....
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.
Tests failed
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.
GJ!
No description provided.