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

more flexible cached_member decorator #187

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion adcc/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
## ---------------------------------------------------------------------
import warnings
import numpy as np
import inspect
from functools import wraps
from pkg_resources import parse_version

Expand Down Expand Up @@ -58,8 +59,26 @@ def cached_member_function(function):
"""
fname = function.__name__

# get the function signature and ensure that we don't have any
# keyword only arguments:
# func(..., *, kwarg=None, ...) or func(..., **kwargs).
# If we want to support them we need to add them in a well defined
# order to the cache key (sort them by name)
func_signature = inspect.signature(function)
bad_arg_types = (inspect.Parameter.KEYWORD_ONLY, inspect.Parameter.VAR_KEYWORD)
if any(arg.kind in bad_arg_types for arg in func_signature.parameters.values()):
raise ValueError("Member functions with keyword only arguments can not be "
"wrapped with the cached_member_function.")

@wraps(function)
def wrapper(self, *args):
def wrapper(self, *args, **kwargs):
# convert all arguments to poisitonal arguments and add default arguments
# for not provided arguments
bound_args = func_signature.bind(self, *args, **kwargs)
bound_args.apply_defaults()
assert not bound_args.kwargs
args = bound_args.args[1:] # remove self from args

try:
fun_cache = self._function_cache[fname]
except AttributeError:
Expand Down
45 changes: 45 additions & 0 deletions adcc/test_misc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import unittest

from adcc.misc import cached_member_function


class SomeClass:
def uncached(self, x=0, y=1):
return (x, y)

@cached_member_function
def cached(self, x=0, y=1):
return (x, y)


class TestCachedMemberFunction(unittest.TestCase):
def test_cache(self):
instance = SomeClass()
assert instance.uncached() is not instance.uncached()
res = instance.cached(0, 1)
assert res is instance.cached(0, 1)
assert res is instance.cached(0)
assert res is instance.cached()
assert res is instance.cached(x=0)
assert res is instance.cached(y=1)
assert res is instance.cached(y=1, x=0)

def test_wrappable(self):
def kwargs_only(self, *, kwarg):
pass

def kwargs(self, **kwargs):
pass

def positional_only(self, /, arg):
pass

def args(self, *args):
pass

with self.assertRaises(ValueError):
cached_member_function(kwargs_only)
with self.assertRaises(ValueError):
cached_member_function(kwargs)
cached_member_function(positional_only)
cached_member_function(args)
Loading