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

The decorator "cache" is created, tested and ready to work #745

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

Conversation

Moonralex
Copy link

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.

app/main.py Outdated
if func_key in cached_data[func.__name__]:
print("Getting from cache")
return cached_data[func.__name__][func_key]
else:

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:

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
Comment on lines 5 to 36
"""
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.
"""

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

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)

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

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.

@Moonralex Moonralex requested a review from pavlejviki October 2, 2023 06:18
Copy link

@Dimosphen1 Dimosphen1 left a 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:

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.

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