dependency-cache 0.2.4__tar.gz → 0.3.0__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.
Files changed (29) hide show
  1. {dependency_cache-0.2.4 → dependency_cache-0.3.0}/PKG-INFO +1 -1
  2. {dependency_cache-0.2.4 → dependency_cache-0.3.0}/pyproject.toml +1 -1
  3. {dependency_cache-0.2.4 → dependency_cache-0.3.0}/python/dependency_cache/dependency_cache.pyi +8 -3
  4. {dependency_cache-0.2.4 → dependency_cache-0.3.0}/python/dependency_cache/plot_dependency_graph.py +1 -1
  5. {dependency_cache-0.2.4 → dependency_cache-0.3.0}/python/examples/automagic_dependency_example.py +2 -2
  6. {dependency_cache-0.2.4 → dependency_cache-0.3.0}/python/examples/example.py +3 -3
  7. {dependency_cache-0.2.4 → dependency_cache-0.3.0}/python/examples/inheritance_example.py +1 -1
  8. {dependency_cache-0.2.4 → dependency_cache-0.3.0}/python/examples/manual_dependency_example.py +1 -1
  9. {dependency_cache-0.2.4 → dependency_cache-0.3.0}/python/examples/runtime_dependencies_example.py +1 -1
  10. {dependency_cache-0.2.4 → dependency_cache-0.3.0}/python/examples/runtime_function_calls_example.py +1 -1
  11. {dependency_cache-0.2.4 → dependency_cache-0.3.0}/python/examples/static_function_calls_example.py +1 -1
  12. {dependency_cache-0.2.4 → dependency_cache-0.3.0}/python/examples/use_cache_example.py +2 -2
  13. {dependency_cache-0.2.4 → dependency_cache-0.3.0}/python/test_python.py +22 -22
  14. {dependency_cache-0.2.4 → dependency_cache-0.3.0}/python/test_python_automagic.py +22 -22
  15. {dependency_cache-0.2.4 → dependency_cache-0.3.0}/src/dependency_cache_base.rs +48 -15
  16. {dependency_cache-0.2.4 → dependency_cache-0.3.0}/uv.lock +1 -1
  17. {dependency_cache-0.2.4 → dependency_cache-0.3.0}/.github/workflows/CI.yml +0 -0
  18. {dependency_cache-0.2.4 → dependency_cache-0.3.0}/.github/workflows/Test.yml +0 -0
  19. {dependency_cache-0.2.4 → dependency_cache-0.3.0}/.gitignore +0 -0
  20. {dependency_cache-0.2.4 → dependency_cache-0.3.0}/Cargo.lock +0 -0
  21. {dependency_cache-0.2.4 → dependency_cache-0.3.0}/Cargo.toml +0 -0
  22. {dependency_cache-0.2.4 → dependency_cache-0.3.0}/LICENSE +0 -0
  23. {dependency_cache-0.2.4 → dependency_cache-0.3.0}/README.md +0 -0
  24. {dependency_cache-0.2.4 → dependency_cache-0.3.0}/python/dependency_cache/__init__.py +0 -0
  25. {dependency_cache-0.2.4 → dependency_cache-0.3.0}/src/decorator.rs +0 -0
  26. {dependency_cache-0.2.4 → dependency_cache-0.3.0}/src/decorator_factory.rs +0 -0
  27. {dependency_cache-0.2.4 → dependency_cache-0.3.0}/src/dependency_graph.rs +0 -0
  28. {dependency_cache-0.2.4 → dependency_cache-0.3.0}/src/lib.rs +0 -0
  29. {dependency_cache-0.2.4 → dependency_cache-0.3.0}/src/py_introspection_utils.rs +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dependency_cache
3
- Version: 0.2.4
3
+ Version: 0.3.0
4
4
  Classifier: Programming Language :: Rust
5
5
  Classifier: Programming Language :: Python :: Implementation :: CPython
6
6
  Classifier: Programming Language :: Python :: Implementation :: PyPy
@@ -11,7 +11,7 @@ classifiers = [
11
11
  "Programming Language :: Python :: Implementation :: PyPy",
12
12
  ]
13
13
  keywords = ["caching", "dependency-graph", "invalidation"]
14
- version = "0.2.4"
14
+ version = "0.3.0"
15
15
  description = "A caching library for Python object methods that utilises a dependency graph to track relationships and control invalidation, enabling efficient recomputation"
16
16
  readme = "README.md"
17
17
  license = { text = "MIT" }
@@ -3,16 +3,21 @@ from typing import Any, final
3
3
 
4
4
  class DependencyCacheBase:
5
5
  def __new__(cls, /, *_args, **_kwargs) -> DependencyCacheBase: ...
6
+ @property
7
+ def cached_values(self, /) -> dict: ...
6
8
  def clear_cache(self, /) -> None: ...
9
+ def clear_cached_value(self, /, method_name: str, **kwargs) -> None: ...
10
+ @property
11
+ def dependency_graph(self, /) -> dict: ...
7
12
  def dump_cache(self, /, order: Sequence[str] | None = None) -> dict: ...
8
13
  def get_cached_value(self, /, method_name: str, **kwargs) -> Any | None: ...
9
- def get_cached_values(self, /) -> dict: ...
10
- def get_dependency_graph(self, /) -> dict: ...
11
- def get_validation_state(self, /) -> dict: ...
14
+ def is_cached(self, /, method_name: str, kwargs: dict | None) -> bool: ...
12
15
  def load_cache(self, /, data: dict, order: Sequence[str] | None = None) -> None: ...
13
16
  def update_cached_value(
14
17
  self, /, method_name: str, value: Any, **kwargs
15
18
  ) -> None: ...
19
+ @property
20
+ def validation_state(self, /) -> dict: ...
16
21
 
17
22
  @final
18
23
  class automagically_dependency_cached:
@@ -7,7 +7,7 @@ from .dependency_cache import DependencyCacheBase
7
7
  def plot_dependency_graph(obj, **kwargs) -> None:
8
8
  if not isinstance(obj, DependencyCacheBase):
9
9
  raise TypeError("provided object must inherit from DependencyCacheBase")
10
- graph_data = obj.get_dependency_graph()
10
+ graph_data = obj.dependency_graph
11
11
  g = nx.DiGraph()
12
12
  for child_node, parents in graph_data.items():
13
13
  if isinstance(child_node, tuple):
@@ -50,8 +50,8 @@ if __name__ == "__main__":
50
50
  print(" A B C")
51
51
  c = ExampleCalculationMagic(1, 2, 3)
52
52
  print(f"Result of E = {c.E()}") # calculates all, prints 6
53
- print(c.get_cached_values(), c.get_dependency_graph(), c.get_validation_state())
53
+ print(c.cached_values, c.dependency_graph, c.validation_state)
54
54
  print(f"Result of E = {c.E()}") # no calculation, returns cached 6
55
55
  c.update_cached_value("A", 2) # invalidates D, E
56
- print(c.get_cached_values(), c.get_dependency_graph(), c.get_validation_state())
56
+ print(c.cached_values, c.dependency_graph, c.validation_state)
57
57
  print(f"Result of E = {c.E()}") # recalculates D, E returns 7
@@ -34,10 +34,10 @@ if __name__ == "__main__":
34
34
  print(" / \\")
35
35
  print("A B")
36
36
  c = ExampleCalculation(3, 5)
37
- print(f"cache={c.get_cached_values()} graph={c.get_dependency_graph()} validation={c.get_validation_state()}")
37
+ print(f"cache={c.cached_values} graph={c.dependency_graph} validation={c.validation_state}")
38
38
  print(f"Result of C = {c.C()}") # calculates all, prints 8
39
- print(f"cache={c.get_cached_values()} graph={c.get_dependency_graph()} validation={c.get_validation_state()}")
39
+ print(f"cache={c.cached_values} graph={c.dependency_graph} validation={c.validation_state}")
40
40
  print(f"Result of C = {c.C()}") # hits cache, prints 8
41
41
  c.update_cached_value("A", 0) # invalidates C
42
42
  print(f"Result of C = {c.C()}") # recalculates C returns 5
43
- print(f"cache={c.get_cached_values()} graph={c.get_dependency_graph()} validation={c.get_validation_state()}")
43
+ print(f"cache={c.cached_values} graph={c.dependency_graph} validation={c.validation_state}")
@@ -41,4 +41,4 @@ if __name__ == "__main__":
41
41
 
42
42
  print(f"Result of A {c.A()}")
43
43
  print(f"Result of B {c.B()}") # cache works for both, no more prints
44
- print(c.get_cached_values(), c.get_dependency_graph(), c.get_validation_state())
44
+ print(c.cached_values, c.dependency_graph, c.validation_state)
@@ -50,6 +50,6 @@ if __name__ == "__main__":
50
50
  c = ExampleCalculationManual(6, 7)
51
51
  print(f"Result of E = {c.E()}") # calculates all, prints 26
52
52
  c.update_cached_value("A", 0) # invalidates C, E
53
- print(c.get_cached_values(), c.get_dependency_graph(), c.get_validation_state())
53
+ print(c.cached_values, c.dependency_graph, c.validation_state)
54
54
  print(f"Result of E = {c.E()}") # recalculates C, E, returns 14
55
55
  print(f"Result of D = {c.D()}") # recalculates D, returns 3.5
@@ -57,4 +57,4 @@ if __name__ == "__main__":
57
57
  c.update_cached_value("B", False)
58
58
  print(c.A()) # calculates A, prints 3
59
59
 
60
- print(c.get_cached_values(), c.get_dependency_graph(), c.get_validation_state())
60
+ print(c.cached_values, c.dependency_graph, c.validation_state)
@@ -31,4 +31,4 @@ if __name__ == "__main__":
31
31
  print(f"Result of Fib(2) {c.Calculate(2)}") # cache works for Calculate(2), no more prints
32
32
  print(f"Result of Fib(1) {c.Calculate(1)}") # cache works for Calculate(2), no more prints
33
33
  print(f"Result of Fib(4) {c.Calculate(4)}") # calculates Calculate(4), rest cached
34
- print(c.get_cached_values(), c.get_dependency_graph(), c.get_validation_state())
34
+ print(c.cached_values, c.dependency_graph, c.validation_state)
@@ -34,4 +34,4 @@ if __name__ == "__main__":
34
34
  print(f"Result of B {c.B()}") # cache works for B, no more prints
35
35
  print(f"Result of A {c.A(0)}")
36
36
  print(f"Result of A {c.A(1)}") # cache works for A, with args, no more prints
37
- print(c.get_cached_values(), c.get_dependency_graph(), c.get_validation_state())
37
+ print(c.cached_values, c.dependency_graph, c.validation_state)
@@ -25,6 +25,6 @@ if __name__ == "__main__":
25
25
  print("A (always recalcs)")
26
26
  c = UseCacheExample()
27
27
  print(f"Result of B = {c.B()}") # calculates all
28
- print(c.get_cached_values(), c.get_dependency_graph(), c.get_validation_state())
28
+ print(c.cached_values, c.dependency_graph, c.validation_state)
29
29
  print(f"Result of B = {c.B()}") # still calculates all
30
- print(c.get_cached_values(), c.get_dependency_graph(), c.get_validation_state())
30
+ print(c.cached_values, c.dependency_graph, c.validation_state)
@@ -178,8 +178,8 @@ def test_init(x_value, y_value):
178
178
  assert a is not None
179
179
  assert a.x == x_value
180
180
  assert a.y == y_value
181
- assert a.get_dependency_graph() == {}
182
- assert a.get_validation_state() == {}
181
+ assert a.dependency_graph == {}
182
+ assert a.validation_state == {}
183
183
 
184
184
 
185
185
  @pytest.mark.parametrize(
@@ -198,12 +198,12 @@ def test_calculation_simple(x_value, y_value, result):
198
198
 
199
199
  assert a.C() == result
200
200
 
201
- assert a.get_dependency_graph() == {"A": {"C"}, "B": {"C"}, "C": set()}
202
- assert a.get_validation_state() == {"A": "valid", "B": "valid", "C": "valid"}
201
+ assert a.dependency_graph == {"A": {"C"}, "B": {"C"}, "C": set()}
202
+ assert a.validation_state == {"A": "valid", "B": "valid", "C": "valid"}
203
203
 
204
204
  a.update_cached_value("B", None)
205
- assert a.get_dependency_graph() == {"A": {"C"}, "B": {"C"}, "C": set()}
206
- assert a.get_validation_state() == {"A": "valid", "B": "valid", "C": "invalid"}
205
+ assert a.dependency_graph == {"A": {"C"}, "B": {"C"}, "C": set()}
206
+ assert a.validation_state == {"A": "valid", "B": "valid", "C": "invalid"}
207
207
 
208
208
 
209
209
  @pytest.mark.parametrize(
@@ -223,12 +223,12 @@ def test_calculation_hard(x_value, y_value, result1, result2):
223
223
  assert a.D() == result1
224
224
  assert a.E() == result2
225
225
 
226
- assert a.get_dependency_graph() == {"A": {"C"}, "B": {"C"}, "C": {"D", "E"}, "D": set(), "E": set()}
227
- assert a.get_validation_state() == {"A": "valid", "B": "valid", "C": "valid", "D": "valid", "E": "valid"}
226
+ assert a.dependency_graph == {"A": {"C"}, "B": {"C"}, "C": {"D", "E"}, "D": set(), "E": set()}
227
+ assert a.validation_state == {"A": "valid", "B": "valid", "C": "valid", "D": "valid", "E": "valid"}
228
228
 
229
229
  a.update_cached_value("B", None)
230
- assert a.get_dependency_graph() == {"A": {"C"}, "B": {"C"}, "C": {"D", "E"}, "D": set(), "E": set()}
231
- assert a.get_validation_state() == {"A": "valid", "B": "valid", "C": "invalid", "D": "invalid", "E": "invalid"}
230
+ assert a.dependency_graph == {"A": {"C"}, "B": {"C"}, "C": {"D", "E"}, "D": set(), "E": set()}
231
+ assert a.validation_state == {"A": "valid", "B": "valid", "C": "invalid", "D": "invalid", "E": "invalid"}
232
232
 
233
233
 
234
234
  @pytest.mark.parametrize(
@@ -247,7 +247,7 @@ def test_calculation_harder(x_value, y_value, result):
247
247
 
248
248
  assert a.F() == result
249
249
 
250
- assert a.get_dependency_graph() == {
250
+ assert a.dependency_graph == {
251
251
  "A": {"C"},
252
252
  "B": {"C"},
253
253
  "C": {"D", "E"},
@@ -255,7 +255,7 @@ def test_calculation_harder(x_value, y_value, result):
255
255
  "E": {"F"},
256
256
  "F": set(),
257
257
  }
258
- assert a.get_validation_state() == {
258
+ assert a.validation_state == {
259
259
  "A": "valid",
260
260
  "B": "valid",
261
261
  "C": "valid",
@@ -265,7 +265,7 @@ def test_calculation_harder(x_value, y_value, result):
265
265
  }
266
266
 
267
267
  a.update_cached_value("B", None)
268
- assert a.get_dependency_graph() == {
268
+ assert a.dependency_graph == {
269
269
  "A": {"C"},
270
270
  "B": {"C"},
271
271
  "C": {"D", "E"},
@@ -273,7 +273,7 @@ def test_calculation_harder(x_value, y_value, result):
273
273
  "E": {"F"},
274
274
  "F": set(),
275
275
  }
276
- assert a.get_validation_state() == {
276
+ assert a.validation_state == {
277
277
  "A": "valid",
278
278
  "B": "valid",
279
279
  "C": "invalid",
@@ -285,11 +285,11 @@ def test_calculation_harder(x_value, y_value, result):
285
285
 
286
286
  def test_permanently_invalid():
287
287
  c = UseCacheObj(2)
288
- assert c.get_dependency_graph() == {}
289
- assert c.get_validation_state() == {}
288
+ assert c.dependency_graph == {}
289
+ assert c.validation_state == {}
290
290
  c.B()
291
- assert c.get_dependency_graph() == {"A": {"B"}, "B": set()}
292
- assert c.get_validation_state() == {"A": "permanently invalid", "B": "permanently invalid"}
291
+ assert c.dependency_graph == {"A": {"B"}, "B": set()}
292
+ assert c.validation_state == {"A": "permanently invalid", "B": "permanently invalid"}
293
293
 
294
294
 
295
295
  def test_plot_dependency_graph_raises():
@@ -302,14 +302,14 @@ def test_plot_dependency_graph_raises():
302
302
  def test_function_calls():
303
303
  c = FunctionCallObj(3)
304
304
 
305
- assert c.get_dependency_graph() == {}
306
- assert c.get_validation_state() == {}
305
+ assert c.dependency_graph == {}
306
+ assert c.validation_state == {}
307
307
 
308
308
  result = c.B()
309
309
  assert result == 7
310
310
 
311
- assert c.get_dependency_graph() == {"B": set(), ("A", (("x", 0),)): {"B"}, ("A", (("x", 1),)): {"B"}}
312
- assert c.get_validation_state() == {"B": "valid", ("A", (("x", 0),)): "valid", ("A", (("x", 1),)): "valid"}
311
+ assert c.dependency_graph == {"B": set(), ("A", (("x", 0),)): {"B"}, ("A", (("x", 1),)): {"B"}}
312
+ assert c.validation_state == {"B": "valid", ("A", (("x", 0),)): "valid", ("A", (("x", 1),)): "valid"}
313
313
 
314
314
 
315
315
  def test_incorrect_decorator_format():
@@ -160,8 +160,8 @@ def test_init(x_value, y_value):
160
160
  assert a is not None
161
161
  assert a.x == x_value
162
162
  assert a.y == y_value
163
- assert a.get_dependency_graph() == {}
164
- assert a.get_validation_state() == {}
163
+ assert a.dependency_graph == {}
164
+ assert a.validation_state == {}
165
165
 
166
166
 
167
167
  @pytest.mark.parametrize(
@@ -180,12 +180,12 @@ def test_calculation_simple(x_value, y_value, result):
180
180
 
181
181
  assert a.C() == result
182
182
 
183
- assert a.get_dependency_graph() == {"A": {"C"}, "B": {"C"}, "C": set()}
184
- assert a.get_validation_state() == {"A": "valid", "B": "valid", "C": "valid"}
183
+ assert a.dependency_graph == {"A": {"C"}, "B": {"C"}, "C": set()}
184
+ assert a.validation_state == {"A": "valid", "B": "valid", "C": "valid"}
185
185
 
186
186
  a.update_cached_value("B", None)
187
- assert a.get_dependency_graph() == {"A": {"C"}, "B": {"C"}, "C": set()}
188
- assert a.get_validation_state() == {"A": "valid", "B": "valid", "C": "invalid"}
187
+ assert a.dependency_graph == {"A": {"C"}, "B": {"C"}, "C": set()}
188
+ assert a.validation_state == {"A": "valid", "B": "valid", "C": "invalid"}
189
189
 
190
190
 
191
191
  @pytest.mark.parametrize(
@@ -205,12 +205,12 @@ def test_calculation_hard(x_value, y_value, result1, result2):
205
205
  assert a.D() == result1
206
206
  assert a.E() == result2
207
207
 
208
- assert a.get_dependency_graph() == {"A": {"C"}, "B": {"C"}, "C": {"D", "E"}, "D": set(), "E": set()}
209
- assert a.get_validation_state() == {"A": "valid", "B": "valid", "C": "valid", "D": "valid", "E": "valid"}
208
+ assert a.dependency_graph == {"A": {"C"}, "B": {"C"}, "C": {"D", "E"}, "D": set(), "E": set()}
209
+ assert a.validation_state == {"A": "valid", "B": "valid", "C": "valid", "D": "valid", "E": "valid"}
210
210
 
211
211
  a.update_cached_value("B", None)
212
- assert a.get_dependency_graph() == {"A": {"C"}, "B": {"C"}, "C": {"D", "E"}, "D": set(), "E": set()}
213
- assert a.get_validation_state() == {"A": "valid", "B": "valid", "C": "invalid", "D": "invalid", "E": "invalid"}
212
+ assert a.dependency_graph == {"A": {"C"}, "B": {"C"}, "C": {"D", "E"}, "D": set(), "E": set()}
213
+ assert a.validation_state == {"A": "valid", "B": "valid", "C": "invalid", "D": "invalid", "E": "invalid"}
214
214
 
215
215
 
216
216
  @pytest.mark.parametrize(
@@ -229,7 +229,7 @@ def test_calculation_harder(x_value, y_value, result):
229
229
 
230
230
  assert a.F() == result
231
231
 
232
- assert a.get_dependency_graph() == {
232
+ assert a.dependency_graph == {
233
233
  "A": {"C"},
234
234
  "B": {"C"},
235
235
  "C": {"D", "E"},
@@ -237,7 +237,7 @@ def test_calculation_harder(x_value, y_value, result):
237
237
  "E": {"F"},
238
238
  "F": set(),
239
239
  }
240
- assert a.get_validation_state() == {
240
+ assert a.validation_state == {
241
241
  "A": "valid",
242
242
  "B": "valid",
243
243
  "C": "valid",
@@ -247,7 +247,7 @@ def test_calculation_harder(x_value, y_value, result):
247
247
  }
248
248
 
249
249
  a.update_cached_value("B", None)
250
- assert a.get_dependency_graph() == {
250
+ assert a.dependency_graph == {
251
251
  "A": {"C"},
252
252
  "B": {"C"},
253
253
  "C": {"D", "E"},
@@ -255,7 +255,7 @@ def test_calculation_harder(x_value, y_value, result):
255
255
  "E": {"F"},
256
256
  "F": set(),
257
257
  }
258
- assert a.get_validation_state() == {
258
+ assert a.validation_state == {
259
259
  "A": "valid",
260
260
  "B": "valid",
261
261
  "C": "invalid",
@@ -267,11 +267,11 @@ def test_calculation_harder(x_value, y_value, result):
267
267
 
268
268
  def test_permanently_invalid():
269
269
  c = UseCacheObj(2)
270
- assert c.get_dependency_graph() == {}
271
- assert c.get_validation_state() == {}
270
+ assert c.dependency_graph == {}
271
+ assert c.validation_state == {}
272
272
  c.B()
273
- assert c.get_dependency_graph() == {"A": {"B"}, "B": set()}
274
- assert c.get_validation_state() == {"A": "permanently invalid", "B": "permanently invalid"}
273
+ assert c.dependency_graph == {"A": {"B"}, "B": set()}
274
+ assert c.validation_state == {"A": "permanently invalid", "B": "permanently invalid"}
275
275
 
276
276
 
277
277
  def test_plot_dependency_graph_raises():
@@ -284,14 +284,14 @@ def test_plot_dependency_graph_raises():
284
284
  def test_function_calls():
285
285
  c = FunctionCallObj(3)
286
286
 
287
- assert c.get_dependency_graph() == {}
288
- assert c.get_validation_state() == {}
287
+ assert c.dependency_graph == {}
288
+ assert c.validation_state == {}
289
289
 
290
290
  result = c.B()
291
291
  assert result == 7
292
292
 
293
- assert c.get_dependency_graph() == {"B": set(), ("A", (("x", 0),)): {"B"}, ("A", (("x", 1),)): {"B"}}
294
- assert c.get_validation_state() == {"B": "valid", ("A", (("x", 0),)): "valid", ("A", (("x", 1),)): "valid"}
293
+ assert c.dependency_graph == {"B": set(), ("A", (("x", 0),)): {"B"}, ("A", (("x", 1),)): {"B"}}
294
+ assert c.validation_state == {"B": "valid", ("A", (("x", 0),)): "valid", ("A", (("x", 1),)): "valid"}
295
295
 
296
296
 
297
297
  def test_incorrect_decorator_format():
@@ -1,6 +1,6 @@
1
1
  use pyo3::prelude::*;
2
2
  use pyo3::types::{PyDict, PyTuple, PyString, PySet, PyType};
3
- use pyo3::exceptions::PyValueError;
3
+ use pyo3::exceptions::{PyValueError, PyKeyError};
4
4
 
5
5
  use rand::rng;
6
6
  use rand::seq::SliceRandom;
@@ -89,6 +89,13 @@ impl DependencyCacheBase {
89
89
  }
90
90
  return None;
91
91
  }
92
+
93
+ fn create_hash(&self, py: Python<'_>, method_name: &String, kwargs: &Option<Py<PyDict>>) -> PyResult<isize> {
94
+ let empty_args = PyTuple::empty(py);
95
+ let kwargs_bound = kwargs.as_ref().map(|k| k.bind(py));
96
+ let (hash, _) = normalise_function_signature_and_hash(py, &method_name, None, &empty_args, kwargs_bound)?;
97
+ return Ok(hash);
98
+ }
92
99
  }
93
100
 
94
101
  #[pymethods]
@@ -104,29 +111,39 @@ impl DependencyCacheBase {
104
111
  }
105
112
 
106
113
  #[pyo3(signature = (method_name, **kwargs))]
107
- pub fn get_cached_value(&self, py: Python<'_>, method_name: String, kwargs: Option<Py<PyDict>>) -> Option<Py<PyAny>> {
108
- let empty_args = PyTuple::empty(py);
109
- let kwargs_bound = kwargs.as_ref().map(|k| k.bind(py));
110
- let (hash, _) = normalise_function_signature_and_hash(py, &method_name, None, &empty_args, kwargs_bound).ok()?;
114
+ pub fn get_cached_value(&self, py: Python<'_>, method_name: String, kwargs: Option<Py<PyDict>>) -> PyResult<Option<Py<PyAny>>> {
115
+ let hash = self.create_hash(py, &method_name, &kwargs)?;
111
116
 
112
117
  if self.method_dependency_graph.is_valid(hash) {
113
- self.cache.get(&hash).map(|obj| obj.clone_ref(py))
114
- } else {
115
- None
118
+ return Ok(self.cache.get(&hash).map(|obj| obj.clone_ref(py)));
116
119
  }
120
+ return Ok(None);
117
121
  }
118
122
 
123
+ pub fn is_cached(
124
+ &self,
125
+ py: Python<'_>,
126
+ method_name: String,
127
+ kwargs: Option<Py<PyDict>>,
128
+ ) -> PyResult<bool> {
129
+ let hash = self.create_hash(py, &method_name, &kwargs)?;
130
+ Ok(self.method_dependency_graph.is_valid(hash) && self.cache.contains_key(&hash))
131
+ }
132
+
133
+
119
134
  #[pyo3(signature = (method_name, value, **kwargs))]
120
- pub fn update_cached_value(&mut self, py: Python<'_>, method_name: String, value: Py<PyAny>, kwargs: Option<Py<PyDict>>) {
121
- let empty_args = PyTuple::empty(py);
122
- let kwargs_bound = kwargs.as_ref().map(|k| k.bind(py));
123
- let Ok((hash, _)) = normalise_function_signature_and_hash(py, &method_name, None, &empty_args, kwargs_bound) else {
124
- return;
125
- };
135
+ pub fn update_cached_value(&mut self, py: Python<'_>, method_name: String, value: Py<PyAny>, kwargs: Option<Py<PyDict>>) -> PyResult<()> {
136
+ let hash = self.create_hash(py, &method_name, &kwargs)?;
126
137
 
138
+ let Some(v) = self.cache.get_mut(&hash) else {
139
+ return Err(PyKeyError::new_err(format!(
140
+ "Cannot update cached value for '{method_name}'"
141
+ )));
142
+ };
143
+ *v = value;
127
144
  self.method_dependency_graph.temporarily_invalidate(hash);
128
- self.cache.insert(hash, value);
129
145
  self.method_dependency_graph.validate(hash);
146
+ return Ok(());
130
147
  }
131
148
 
132
149
  pub fn clear_cache(&mut self) {
@@ -134,6 +151,20 @@ impl DependencyCacheBase {
134
151
  self.cache.clear();
135
152
  }
136
153
 
154
+ #[pyo3(signature = (method_name, **kwargs))]
155
+ pub fn clear_cached_value(&mut self, py: Python<'_>, method_name: String, kwargs: Option<Py<PyDict>>) -> PyResult<()> {
156
+ let hash = self.create_hash(py, &method_name, &kwargs)?;
157
+
158
+ if self.cache.remove(&hash).is_none() {
159
+ return Err(PyKeyError::new_err(format!(
160
+ "Cannot clear cached value for '{method_name}'"
161
+ )));
162
+ }
163
+ self.method_dependency_graph.temporarily_invalidate(hash);
164
+ Ok(())
165
+ }
166
+
167
+ #[getter]
137
168
  pub fn get_cached_values<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyDict>> {
138
169
  let dict = PyDict::new(py);
139
170
  for (hash, value) in &self.cache {
@@ -158,6 +189,7 @@ impl DependencyCacheBase {
158
189
  Ok(dict)
159
190
  }
160
191
 
192
+ #[getter]
161
193
  pub fn get_dependency_graph<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyDict>> {
162
194
  let dict = PyDict::new(py);
163
195
 
@@ -203,6 +235,7 @@ impl DependencyCacheBase {
203
235
  Ok(dict)
204
236
  }
205
237
 
238
+ #[getter]
206
239
  pub fn get_validation_state<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyDict>> {
207
240
  let dict = PyDict::new(py);
208
241
  for (hash, state) in &self.method_dependency_graph.clone_state() {
@@ -329,7 +329,7 @@ wheels = [
329
329
 
330
330
  [[package]]
331
331
  name = "dependency-cache"
332
- version = "0.2.4"
332
+ version = "0.3.0"
333
333
  source = { editable = "." }
334
334
  dependencies = [
335
335
  { name = "matplotlib", version = "3.7.5", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.9'" },