-
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' #1387
base: master
Are you sure you want to change the base?
'solution' #1387
Conversation
app/main.py
Outdated
result = func(*args, **kwargs) | ||
cached_data[parameters] = result | ||
print("Calculating new 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.
Avoid using additional variable, it is not needed here
app/main.py
Outdated
cached_data = {} | ||
|
||
def wrapper(*args, **kwargs) -> Callable: | ||
parameters = tuple(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.
Are you sure you can't do without tuple?
app/main.py
Outdated
def wrapper(*args) -> Callable: | ||
if args not in storage: | ||
print("Calculating new result") | ||
return storage.setdefault(args, func(*args)) | ||
else: | ||
print("Getting from cache") | ||
return storage.get(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.
Try to rewrite wrapper not using a duplicated return
, without duplicating it
else: | ||
print("Getting from cache") | ||
return storage.get(args) | ||
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.
Always add a blank line at the end of the file.
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.
Yeh, sure, but it is there (flake8 always push me to do this)
app/main.py
Outdated
storage = {} | ||
|
||
def wrapper(*args) -> Callable: | ||
if args not in storage: |
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.
In many cases , it is better not to use not
, because we always thinking if something is in
but not is not in
. But a it's a minor thing, that can save you a 3 symbols xD
By the way, it's opinion from my side for this task, sometimes we really need to check not in
because it looks better with variable names and task requirements.
At that task, if you swap you if
code and else
to avoid using not, nothing will change logically!
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.
That was your last try, so review my comment and accept it. Anyway you are doing great, that was nice to see that you used a setdefault
method (for previous solution it makes sense!!), remember how it works and use it next time if it's only needed.
Well done!
print("Getting from cache") | ||
else: | ||
print("Calculating new result") | ||
storage.setdefault(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.
There is no more sense use setdefault
method, because you are getting value at the end, we can simply set value by a key. Now it's doing one extra move - return you a value into nothing, so we don't need it.
No description provided.