-
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
The decorator "cache" is created, tested and ready to work #745
base: master
Are you sure you want to change the base?
Conversation
app/main.py
Outdated
if func_key in cached_data[func.__name__]: | ||
print("Getting from cache") | ||
return cached_data[func.__name__][func_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.
remove redundant 'else'
app/main.py
Outdated
func_result = func(*args) | ||
cached_data[func.__name__][func_key] = func_result | ||
return func_result | ||
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.
remove redundant 'else'
app/main.py
Outdated
""" | ||
Decorator for caching results of functions with immutable arguments. | ||
|
||
The `cache` function is designed to automatically cache results of | ||
functions with immutable (unchangeable) arguments. Each function | ||
decorated with this decorator gets its own cache to store results. | ||
In this decorator, the cache is reset with each new test suite. | ||
|
||
Args: | ||
func (Callable): The function to be decorated. | ||
|
||
Returns: | ||
Callable: The modified function that caches results based on | ||
arguments. | ||
|
||
Examples: | ||
@cache | ||
def long_time_func(a: int, b: int, c: int) -> int: | ||
return (a ** b ** c) % (a * c) | ||
result = long_time_func(2, 3, 4) # Computation is performed | ||
# and result is cached | ||
result = long_time_func(2, 3, 4) # Result is retrieved from | ||
# cache, computation is not | ||
# repeated | ||
|
||
Note: | ||
This decorator supports caching results for functions with | ||
arguments that are immutable types, such as int, float, str, | ||
tuple, bool, and NoneType. The results are stored in memory | ||
and used for subsequent calls to the function with the same | ||
arguments. | ||
""" |
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.
docstring is super redundant here
remove it
app/main.py
Outdated
immutable_type = (int, float, str, tuple, bool, type(None)) | ||
|
||
def argument_based_cacher(*args) -> Any: | ||
nonlocal cached_data |
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're modifying the dictionary that cached_data points to (by adding keys and values). Since you're not re-assigning the variable, you don't need nonlocal.
app/main.py
Outdated
def argument_based_cacher(*args) -> Any: | ||
nonlocal cached_data | ||
if all([isinstance(variable, immutable_type) for variable in args]): | ||
func_key = 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.
args is already a tuple, and there's no need to convert it again using tuple(args).
func_result = func(*args) | ||
cached_data[func.__name__][func_key] = func_result | ||
return func_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.
Decorator cache is supposed to decorate only functions that take immutable arguments.
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.
LGTM, one comment is left for consideration.
pass | ||
cached_data = {} | ||
|
||
def argument_based_cacher(*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.
You could use decorator from functools
to preserve the wrapped function naming and docstring.
Decorator for caching results of functions with immutable arguments.
The
cache
function is designed to automatically cache the results of functions with immutable (unchangeable) arguments.Each function decorated with this decorator gets its own cache to store results.
In this decorator, the cache is reset with each new test suite.
Args:
func (Callable): The function to be decorated.
Returns:
Callable: The modified function that caches results based on arguments.
Examples:
@cache
def long_time_func(a: int, b: int, c: int) -> int:
return (a ** b ** c) % (a * c)
result = long_time_func(2, 3, 4) # Computation is performed and result is cached
result = long_time_func(2, 3, 4) # Result is retrieved from cache, computation is not repeated
Note:
This decorator supports caching results for functions with arguments that are immutable types,
such as int, float, str, tuple, bool, and NoneType. The results are stored in memory
and used for subsequent calls to the function with the same arguments.