persistent-function-cache 0.1.1__py3-none-any.whl → 0.2.0__py3-none-any.whl

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.
@@ -1,18 +1,5 @@
1
- from collections.abc import Callable
1
+ import functools
2
2
 
3
3
  from persistent_cache.main import decorator
4
- from persistent_cache.models import F, Path
5
- from persistent_cache.reducers import Reducer
6
4
 
7
-
8
- def cache(
9
- function: F | None = None,
10
- *,
11
- key_reducer: type[Reducer] | None = None,
12
- cache_path: Path = Path.cache,
13
- ) -> Callable[[F], F]:
14
- if key_reducer is None:
15
- from persistent_cache.reducers import deep_learning
16
-
17
- key_reducer = deep_learning.Reducer
18
- return decorator.cache(function, key_reducer=key_reducer, cache_path=cache_path)
5
+ cache = functools.partial(decorator.cache, deep_learning=True)
@@ -1,18 +1,5 @@
1
- from collections.abc import Callable
1
+ import functools
2
2
 
3
3
  from persistent_cache.main import decorator
4
- from persistent_cache.models import F, Path
5
- from persistent_cache.reducers import Reducer
6
4
 
7
-
8
- def cache(
9
- function: F | None = None,
10
- *,
11
- key_reducer: type[Reducer] | None = None,
12
- cache_path: Path = Path.cache,
13
- ) -> Callable[[F], F]:
14
- if key_reducer is None:
15
- from persistent_cache.reducers import speedup_deep_learning
16
-
17
- key_reducer = speedup_deep_learning.Reducer
18
- return decorator.cache(function, key_reducer=key_reducer, cache_path=cache_path)
5
+ cache = functools.partial(decorator.cache, speedup_deep_learning=True)
@@ -15,12 +15,11 @@ class CacheSlot(Generic[T]):
15
15
  self,
16
16
  function: Callable[..., T],
17
17
  args: tuple[Any, ...],
18
- kwargs: Any,
19
18
  key_reducer: type[Reducer] = Reducer,
20
19
  cache_path: Path = Path.cache,
21
20
  ) -> None:
22
21
  # change cache key when implementation changes
23
- cache_keys = (function, args, kwargs)
22
+ cache_keys = (function, args)
24
23
  self.location = (
25
24
  cache_path
26
25
  / function.__module__.replace(".", "_")
@@ -1,4 +1,5 @@
1
- from collections.abc import Callable
1
+ import inspect
2
+ from collections.abc import Callable, Iterator
2
3
  from functools import wraps
3
4
  from typing import Any, cast, overload
4
5
 
@@ -12,8 +13,11 @@ from .cacheslot import CacheSlot
12
13
  def cache(
13
14
  function: F,
14
15
  *,
15
- key_reducer: type[Reducer] = Reducer,
16
16
  cache_path: Path = Path.cache,
17
+ cache_key_arguments: tuple[str, ...] | str | None = None,
18
+ key_reducer: type[Reducer] = Reducer,
19
+ deep_learning: bool = False,
20
+ speedup_deep_learning: bool = False,
17
21
  ) -> F: ...
18
22
 
19
23
 
@@ -21,16 +25,22 @@ def cache(
21
25
  def cache(
22
26
  function: None = None,
23
27
  *,
24
- key_reducer: type[Reducer] = Reducer,
25
28
  cache_path: Path = Path.cache,
29
+ cache_key_arguments: tuple[str, ...] | str | None = None,
30
+ key_reducer: type[Reducer] = Reducer,
31
+ deep_learning: bool = False,
32
+ speedup_deep_learning: bool = False,
26
33
  ) -> Callable[[F], F]: ...
27
34
 
28
35
 
29
- def cache(
36
+ def cache( # noqa: PLR0913
30
37
  function: F | None = None,
31
38
  *,
32
- key_reducer: type[Reducer] = Reducer,
33
39
  cache_path: Path = Path.cache,
40
+ cache_key_arguments: tuple[str, ...] | str | None = None,
41
+ key_reducer: type[Reducer] | None = None,
42
+ deep_learning: bool = False,
43
+ speedup_deep_learning: bool = False,
34
44
  ) -> F | Callable[[F], F]:
35
45
  """A decorator to cache function results. Decorated functions are only executed if
36
46
  result is not present in cache. The arguments of the function can be any nested
@@ -45,10 +55,19 @@ def cache(
45
55
  ...
46
56
  """
47
57
 
58
+ reducer = extract_reducer(
59
+ key_reducer,
60
+ deep_learning=deep_learning,
61
+ speedup_deep_learning=speedup_deep_learning,
62
+ )
63
+
48
64
  def cache_decorator(function: F) -> F:
49
65
  @wraps(function)
50
66
  def wrapped_function(*args: Any, **kwargs: Any) -> Any:
51
- cache_slot = CacheSlot(function, args, kwargs, key_reducer, cache_path)
67
+ arguments = tuple(
68
+ extract_argument_values(function, args, kwargs, cache_key_arguments),
69
+ )
70
+ cache_slot = CacheSlot(function, arguments, reducer, cache_path)
52
71
  try:
53
72
  value = cache_slot.value
54
73
  except KeyError:
@@ -61,3 +80,43 @@ def cache(
61
80
  if function is not None:
62
81
  cache_decorator = cache_decorator(function)
63
82
  return cache_decorator
83
+
84
+
85
+ def extract_argument_values(
86
+ function: F,
87
+ args: tuple[Any, ...],
88
+ kwargs: dict[str, Any],
89
+ cache_key_arguments: tuple[str, ...] | str | None = None,
90
+ ) -> Iterator[Any]:
91
+ if cache_key_arguments is None:
92
+ yield from (args, kwargs)
93
+ else:
94
+ arguments = inspect.signature(function).bind(*args, **kwargs)
95
+ arguments.apply_defaults()
96
+ if isinstance(cache_key_arguments, str):
97
+ yield arguments.arguments.get(cache_key_arguments)
98
+ else:
99
+ for name in cache_key_arguments:
100
+ yield arguments.arguments.get(name)
101
+
102
+
103
+ def extract_reducer(
104
+ reducer: type[Reducer] | None,
105
+ *,
106
+ deep_learning: bool,
107
+ speedup_deep_learning: bool,
108
+ ) -> type[Reducer]:
109
+ if reducer is None:
110
+ if deep_learning:
111
+ from persistent_cache.reducers.deep_learning import Reducer as Reducer_
112
+
113
+ reducer = Reducer_
114
+ elif speedup_deep_learning:
115
+ from persistent_cache.reducers.speedup_deep_learning import (
116
+ Reducer as Reducer_,
117
+ )
118
+
119
+ reducer = Reducer_
120
+ else:
121
+ reducer = Reducer
122
+ return reducer
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: persistent-function-cache
3
- Version: 0.1.1
3
+ Version: 0.2.0
4
4
  Summary: Persistent cache for expensive functions
5
5
  Author-email: Quinten Roets <qdr2104@columbia.edu>
6
6
  License: MIT
@@ -1,13 +1,13 @@
1
1
  persistent_cache/__init__.py,sha256=VUq2BaLpsu66LXVBmRSBHGspctti2qavBqJNyja2yf8,91
2
2
  persistent_cache/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
3
  persistent_cache/caches/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- persistent_cache/caches/deep_learning.py,sha256=mFqdc72GylCrM6rDjN0gTt13xYQhDVOghdpF9htHL7o,545
5
- persistent_cache/caches/speedup_deep_learning.py,sha256=zebw9KM1JkOxQIFHEvc1iBwfzu0D3oX9N_u_FmdcHgQ,561
4
+ persistent_cache/caches/deep_learning.py,sha256=tJQrRJK2Sbv7tIGwnrnFAn8nP-Fh7ZgugocKcXeiMnU,126
5
+ persistent_cache/caches/speedup_deep_learning.py,sha256=bywNXeEZsg8Rs2A1_w-ZrJi9Ew2TvNCz59YryGyV0NI,134
6
6
  persistent_cache/cli/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
7
  persistent_cache/cli/clear_cache.py,sha256=YbKfqzUB9IyEpU-2n0h5BrK9kZKjbayqiMTzjPbDl3s,1541
8
8
  persistent_cache/main/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
- persistent_cache/main/cacheslot.py,sha256=XIobyDDtMx1KpVxcz9dKo6-QEsFmK9Xej5j6dFieXk4,1246
10
- persistent_cache/main/decorator.py,sha256=ekRSAru2cRA6yU0rkpwzyQVnIw1qmW_kTCgSUSrm4nk,1572
9
+ persistent_cache/main/cacheslot.py,sha256=6jNF6B1aEW185aPJ7Wd7tzLIOcwqyNr2p9tkxUHhk5E,1217
10
+ persistent_cache/main/decorator.py,sha256=ojMDckrv5Fwhh8RvUCeZwQl06I1Q61olvs1HSz-jtVY,3457
11
11
  persistent_cache/main/hashing.py,sha256=YP_HO9nJe2_ZkiQf-unM6W9y_k5TJW1JvWeZfPIXtug,2650
12
12
  persistent_cache/models/__init__.py,sha256=No2WagrzFJdeQE3hTIeFd2H8hhvvAFcdVH6z07D1vmI,47
13
13
  persistent_cache/models/function.py,sha256=C1w0rVOhUkqvpPkXNj85rc4GSJDqT0oIZLsZvhVwn58,113
@@ -16,9 +16,9 @@ persistent_cache/reducers/__init__.py,sha256=hCozFuvrAb_gXLcqMMq92v4KdwBbQugwGwC
16
16
  persistent_cache/reducers/base.py,sha256=HsodcuE8Id5oV93_b_RN18I69UWsJ21yazXCwT1f6YU,1428
17
17
  persistent_cache/reducers/deep_learning.py,sha256=cyVolwmwm_ZN2gRxPtfJFqiNQlNuq_CrehO7ZSVWeVw,800
18
18
  persistent_cache/reducers/speedup_deep_learning.py,sha256=kMkr1467zUsQUPOi_N8tWKWAtKUcFgsk3rh94QhETg0,1970
19
- persistent_function_cache-0.1.1.dist-info/LICENSE,sha256=ENpNaBSvIV7ifD7iNz_-lBhImbiYowqPzBc5V5R8IhM,1070
20
- persistent_function_cache-0.1.1.dist-info/METADATA,sha256=XvGcWqgMSIS0u2b0zM9y5zxiW6KZ5h-Bkr-lG0ZhuHs,2164
21
- persistent_function_cache-0.1.1.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
22
- persistent_function_cache-0.1.1.dist-info/entry_points.txt,sha256=fSZfuL6Wn42mZldojgGyg7dzSPCRVAuZox3w6dLd2ZI,88
23
- persistent_function_cache-0.1.1.dist-info/top_level.txt,sha256=Jj-Z9wxHRlRyDUxrfC9GeaV5-8hAf8R4LclQFZesxi8,17
24
- persistent_function_cache-0.1.1.dist-info/RECORD,,
19
+ persistent_function_cache-0.2.0.dist-info/LICENSE,sha256=ENpNaBSvIV7ifD7iNz_-lBhImbiYowqPzBc5V5R8IhM,1070
20
+ persistent_function_cache-0.2.0.dist-info/METADATA,sha256=thXWcG0Xy3rs1wV86vs7GcRcIYL8c_zAdwLitFoknNs,2164
21
+ persistent_function_cache-0.2.0.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
22
+ persistent_function_cache-0.2.0.dist-info/entry_points.txt,sha256=fSZfuL6Wn42mZldojgGyg7dzSPCRVAuZox3w6dLd2ZI,88
23
+ persistent_function_cache-0.2.0.dist-info/top_level.txt,sha256=Jj-Z9wxHRlRyDUxrfC9GeaV5-8hAf8R4LclQFZesxi8,17
24
+ persistent_function_cache-0.2.0.dist-info/RECORD,,