persistent-function-cache 0.2.1__tar.gz → 0.2.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {persistent_function_cache-0.2.1/src/persistent_function_cache.egg-info → persistent_function_cache-0.2.2}/PKG-INFO +1 -1
- {persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/pyproject.toml +1 -1
- {persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/src/persistent_cache/main/cache_slot.py +11 -1
- {persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/src/persistent_cache/main/decorator.py +4 -0
- {persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2/src/persistent_function_cache.egg-info}/PKG-INFO +1 -1
- {persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/tests/test_cache.py +11 -0
- {persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/LICENSE +0 -0
- {persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/README.md +0 -0
- {persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/setup.cfg +0 -0
- {persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/src/persistent_cache/__init__.py +0 -0
- {persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/src/persistent_cache/caches/__init__.py +0 -0
- {persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/src/persistent_cache/caches/deep_learning.py +0 -0
- {persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/src/persistent_cache/caches/speedup_deep_learning.py +0 -0
- {persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/src/persistent_cache/cli/__init__.py +0 -0
- {persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/src/persistent_cache/cli/clear_cache.py +0 -0
- {persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/src/persistent_cache/main/__init__.py +0 -0
- {persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/src/persistent_cache/main/hashing.py +0 -0
- {persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/src/persistent_cache/models/__init__.py +0 -0
- {persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/src/persistent_cache/models/path.py +0 -0
- {persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/src/persistent_cache/py.typed +0 -0
- {persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/src/persistent_cache/reducers/__init__.py +0 -0
- {persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/src/persistent_cache/reducers/base.py +0 -0
- {persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/src/persistent_cache/reducers/deep_learning.py +0 -0
- {persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/src/persistent_cache/reducers/speedup_deep_learning.py +0 -0
- {persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/src/persistent_function_cache.egg-info/SOURCES.txt +0 -0
- {persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/src/persistent_function_cache.egg-info/dependency_links.txt +0 -0
- {persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/src/persistent_function_cache.egg-info/entry_points.txt +0 -0
- {persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/src/persistent_function_cache.egg-info/requires.txt +0 -0
- {persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/src/persistent_function_cache.egg-info/top_level.txt +0 -0
- {persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/tests/test_clear_cache.py +0 -0
- {persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/tests/test_deep_learning_cache.py +0 -0
|
@@ -3,6 +3,7 @@ import pickle
|
|
|
3
3
|
from collections.abc import Callable, Iterable, Iterator
|
|
4
4
|
from dataclasses import dataclass
|
|
5
5
|
from functools import cached_property
|
|
6
|
+
from inspect import BoundArguments
|
|
6
7
|
from typing import Any
|
|
7
8
|
|
|
8
9
|
from persistent_cache.models import Path
|
|
@@ -18,6 +19,7 @@ class CacheSlot:
|
|
|
18
19
|
kwargs: dict[str, Any]
|
|
19
20
|
directory: Path
|
|
20
21
|
key_arguments: Iterable[str] | str | None
|
|
22
|
+
argument_reducers: dict[str, Callable[[Any], Any]] | None
|
|
21
23
|
extra_keys: Any
|
|
22
24
|
key_reducer: type[Reducer] | None
|
|
23
25
|
deep_learning: bool
|
|
@@ -60,17 +62,25 @@ class CacheSlot:
|
|
|
60
62
|
|
|
61
63
|
@property
|
|
62
64
|
def argument_values(self) -> Iterator[Any]:
|
|
63
|
-
if self.key_arguments is None:
|
|
65
|
+
if self.key_arguments is None and self.argument_reducers is None:
|
|
64
66
|
yield from self.args
|
|
65
67
|
yield from self.kwargs.values()
|
|
66
68
|
else:
|
|
67
69
|
arguments = inspect.signature(self.function).bind(*self.args, **self.kwargs)
|
|
68
70
|
arguments.apply_defaults()
|
|
71
|
+
yield from self.extract_argument_values(arguments)
|
|
72
|
+
|
|
73
|
+
def extract_argument_values(self, arguments: BoundArguments) -> Iterator[Any]:
|
|
74
|
+
if self.key_arguments is not None:
|
|
69
75
|
if isinstance(self.key_arguments, str):
|
|
70
76
|
yield arguments.arguments.get(self.key_arguments)
|
|
71
77
|
else:
|
|
72
78
|
for name in self.key_arguments:
|
|
73
79
|
yield arguments.arguments.get(name)
|
|
80
|
+
if self.argument_reducers is not None:
|
|
81
|
+
for argument_name, reducer in self.argument_reducers.items():
|
|
82
|
+
argument = arguments.arguments.get(argument_name)
|
|
83
|
+
yield reducer(argument)
|
|
74
84
|
|
|
75
85
|
@property
|
|
76
86
|
def reducer(self) -> type[Reducer]:
|
|
@@ -16,6 +16,7 @@ def cache(
|
|
|
16
16
|
*,
|
|
17
17
|
cache_directory: Path = Path.cache,
|
|
18
18
|
cache_key_arguments: Iterable[str] | str | None = None,
|
|
19
|
+
argument_reducers: dict[str, Callable[[Any], Any]] | None = None,
|
|
19
20
|
extra_cache_keys: Iterable[Any] | None = None,
|
|
20
21
|
key_reducer: type[Reducer] = Reducer,
|
|
21
22
|
deep_learning: bool = False,
|
|
@@ -29,6 +30,7 @@ def cache(
|
|
|
29
30
|
*,
|
|
30
31
|
cache_directory: Path = Path.cache,
|
|
31
32
|
cache_key_arguments: Iterable[str] | str | None = None,
|
|
33
|
+
argument_reducers: dict[str, Callable[[Any], Any]] | None = None,
|
|
32
34
|
extra_cache_keys: Any = None,
|
|
33
35
|
key_reducer: type[Reducer] = Reducer,
|
|
34
36
|
deep_learning: bool = False,
|
|
@@ -41,6 +43,7 @@ def cache( # noqa: PLR0913
|
|
|
41
43
|
*,
|
|
42
44
|
cache_directory: Path = Path.cache,
|
|
43
45
|
cache_key_arguments: Iterable[str] | str | None = None,
|
|
46
|
+
argument_reducers: dict[str, Callable[[Any], Any]] | None = None,
|
|
44
47
|
extra_cache_keys: Any = None,
|
|
45
48
|
key_reducer: type[Reducer] | None = None,
|
|
46
49
|
deep_learning: bool = False,
|
|
@@ -68,6 +71,7 @@ def cache( # noqa: PLR0913
|
|
|
68
71
|
kwargs,
|
|
69
72
|
cache_directory,
|
|
70
73
|
cache_key_arguments,
|
|
74
|
+
argument_reducers,
|
|
71
75
|
extra_cache_keys,
|
|
72
76
|
key_reducer,
|
|
73
77
|
deep_learning,
|
|
@@ -84,3 +84,14 @@ def test_cache_with_reducer(
|
|
|
84
84
|
key_reducer=Reducer,
|
|
85
85
|
)
|
|
86
86
|
cached_function("test")
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
@pytest.mark.parametrize("cache_decorator", caches)
|
|
90
|
+
def test_cache_with_argument_reducer(
|
|
91
|
+
cache_decorator: Callable[..., Any],
|
|
92
|
+
) -> None:
|
|
93
|
+
cached_function = cache_decorator(
|
|
94
|
+
calculate_with_name,
|
|
95
|
+
argument_reducers={"value": lambda x: x.strip()},
|
|
96
|
+
)
|
|
97
|
+
cached_function("test")
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/src/persistent_cache/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/src/persistent_cache/py.typed
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{persistent_function_cache-0.2.1 → persistent_function_cache-0.2.2}/tests/test_clear_cache.py
RENAMED
|
File without changes
|
|
File without changes
|