dependency-cache 0.4.2__tar.gz → 0.5.1__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.
- {dependency_cache-0.4.2 → dependency_cache-0.5.1}/PKG-INFO +16 -13
- {dependency_cache-0.4.2 → dependency_cache-0.5.1}/README.md +15 -12
- {dependency_cache-0.4.2 → dependency_cache-0.5.1}/pyproject.toml +1 -1
- {dependency_cache-0.4.2 → dependency_cache-0.5.1}/python/dependency_cache/__init__.py +2 -0
- {dependency_cache-0.4.2 → dependency_cache-0.5.1}/python/dependency_cache/dependency_cache.pyi +3 -1
- {dependency_cache-0.4.2 → dependency_cache-0.5.1}/python/examples/automagic_dependency_example.py +4 -5
- dependency_cache-0.5.1/python/examples/custom_decorator_example.py +43 -0
- dependency_cache-0.5.1/python/examples/custom_scenario_example.py +49 -0
- {dependency_cache-0.4.2 → dependency_cache-0.5.1}/python/examples/example.py +3 -4
- {dependency_cache-0.4.2 → dependency_cache-0.5.1}/python/examples/inheritance_example.py +4 -10
- {dependency_cache-0.4.2 → dependency_cache-0.5.1}/python/examples/manual_dependency_example.py +3 -4
- {dependency_cache-0.4.2 → dependency_cache-0.5.1}/python/examples/runtime_dependencies_example.py +4 -4
- {dependency_cache-0.4.2 → dependency_cache-0.5.1}/python/examples/runtime_function_calls_example.py +0 -3
- dependency_cache-0.5.1/python/examples/serialisable_example.py +31 -0
- {dependency_cache-0.4.2 → dependency_cache-0.5.1}/python/examples/static_function_calls_example.py +0 -1
- {dependency_cache-0.4.2 → dependency_cache-0.5.1}/python/examples/use_cache_example.py +0 -3
- {dependency_cache-0.4.2 → dependency_cache-0.5.1}/python/test_python.py +29 -15
- {dependency_cache-0.4.2 → dependency_cache-0.5.1}/python/test_python_automagic.py +35 -16
- {dependency_cache-0.4.2 → dependency_cache-0.5.1}/src/decorator.rs +10 -0
- {dependency_cache-0.4.2 → dependency_cache-0.5.1}/src/decorator_factory.rs +1 -1
- {dependency_cache-0.4.2 → dependency_cache-0.5.1}/src/dependency_cache_base.rs +126 -23
- {dependency_cache-0.4.2 → dependency_cache-0.5.1}/src/dependency_graph.rs +2 -0
- {dependency_cache-0.4.2 → dependency_cache-0.5.1}/src/lib.rs +5 -0
- {dependency_cache-0.4.2 → dependency_cache-0.5.1}/src/metadata_hash_manager.rs +5 -5
- {dependency_cache-0.4.2 → dependency_cache-0.5.1}/src/py_introspection_utils.rs +2 -2
- dependency_cache-0.5.1/src/scenario.rs +61 -0
- {dependency_cache-0.4.2 → dependency_cache-0.5.1}/uv.lock +1 -1
- {dependency_cache-0.4.2 → dependency_cache-0.5.1}/.github/workflows/CI.yml +0 -0
- {dependency_cache-0.4.2 → dependency_cache-0.5.1}/.github/workflows/Test.yml +0 -0
- {dependency_cache-0.4.2 → dependency_cache-0.5.1}/.gitignore +0 -0
- {dependency_cache-0.4.2 → dependency_cache-0.5.1}/Cargo.lock +0 -0
- {dependency_cache-0.4.2 → dependency_cache-0.5.1}/Cargo.toml +0 -0
- {dependency_cache-0.4.2 → dependency_cache-0.5.1}/LICENSE +0 -0
- {dependency_cache-0.4.2 → dependency_cache-0.5.1}/python/dependency_cache/plot_dependency_graph.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: dependency_cache
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.5.1
|
|
4
4
|
Classifier: Programming Language :: Rust
|
|
5
5
|
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
6
6
|
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
@@ -36,9 +36,13 @@ pip install dependency-cache
|
|
|
36
36
|
| Export | What it's for |
|
|
37
37
|
|---|---|
|
|
38
38
|
| `DependencyCacheBase` | Base class to inherit from. Gives your instance a cache and a dependency graph. |
|
|
39
|
-
| `dependency_cached
|
|
40
|
-
| `automagically_dependency_cached
|
|
41
|
-
| `plot_dependency_graph
|
|
39
|
+
| `dependency_cached` | A footgun-enabled decorator for a method where you **explicitly declare** all direct dependencies. |
|
|
40
|
+
| `automagically_dependency_cached` | A decorator which **automagically infers** the direct dependencies. |
|
|
41
|
+
| `plot_dependency_graph` | Visualizes an instance's dependency graph, for inspection or debugging. |
|
|
42
|
+
| `custom_scenario` | A context manager to temporarily run a custom dependency scenario, restoring the original state on exit. |
|
|
43
|
+
|
|
44
|
+
All decorators accept optional parameters like use_cache, dependencies, track_runtime_dependencies, and serialisable. For exact signatures and types, see the
|
|
45
|
+
[generated stubs file](https://github.com/XtdWt/dependency_cache/blob/master/python/dependency_cache/dependency_cache.pyi).
|
|
42
46
|
|
|
43
47
|
### Setup
|
|
44
48
|
This project uses (and requires) [uv](https://github.com/astral-sh/uv) as a package manager and maturin to compile.
|
|
@@ -75,12 +79,11 @@ from dependency_cache import DependencyCacheBase, automagically_dependency_cache
|
|
|
75
79
|
|
|
76
80
|
class ExampleCalculation(DependencyCacheBase):
|
|
77
81
|
""" C
|
|
78
|
-
|
|
79
|
-
A
|
|
82
|
+
/ \
|
|
83
|
+
A B
|
|
80
84
|
"""
|
|
81
85
|
|
|
82
86
|
def __init__(self, x, y):
|
|
83
|
-
super().__init__()
|
|
84
87
|
self.x = x
|
|
85
88
|
self.y = y
|
|
86
89
|
|
|
@@ -101,14 +104,14 @@ class ExampleCalculation(DependencyCacheBase):
|
|
|
101
104
|
|
|
102
105
|
|
|
103
106
|
c = ExampleCalculation(3, 5)
|
|
104
|
-
print(c.C()) # calculates
|
|
105
|
-
print(c.C()) # hits cache
|
|
107
|
+
print(f"Result of C = {c.C()}") # calculates all, prints 8
|
|
108
|
+
print(f"Result of C = {c.C()}") # hits cache, prints 8
|
|
106
109
|
|
|
107
|
-
c.update_cached_value("A", 0) #
|
|
108
|
-
print(c.C()) # recalculates C
|
|
110
|
+
c.update_cached_value("A", 0) # updates A, then invalidates C
|
|
111
|
+
print(f"Result of C = {c.C()}") # recalculates C returns 5
|
|
109
112
|
```
|
|
110
113
|
|
|
111
|
-
This example can be found [
|
|
114
|
+
This example can be found at [python/examples/example.py](https://github.com/XtdWt/dependency_cache/blob/master/python/examples/example.py) with more runnable examples and use cases provided in the [python/examples](https://github.com/XtdWt/dependency_cache/tree/master/python/examples) folder.
|
|
112
115
|
|
|
113
116
|
```bash
|
|
114
117
|
uv run ./python/examples/example.py
|
|
@@ -118,7 +121,7 @@ uv run ./python/examples/example.py
|
|
|
118
121
|
This project is still a work in progress, with everything from API to underlying design subject to change on my whim.
|
|
119
122
|
|
|
120
123
|
Current TODO list (in no particular order):
|
|
121
|
-
-
|
|
124
|
+
- add global_variable to link objects together
|
|
122
125
|
- add validation to static dependencies, ensure methods are of decorator class (?)
|
|
123
126
|
- improve plot_dependency_graph (maybe to GUI?) with interactive nodes
|
|
124
127
|
- add object thread safety for python 3.14+
|
|
@@ -18,9 +18,13 @@ pip install dependency-cache
|
|
|
18
18
|
| Export | What it's for |
|
|
19
19
|
|---|---|
|
|
20
20
|
| `DependencyCacheBase` | Base class to inherit from. Gives your instance a cache and a dependency graph. |
|
|
21
|
-
| `dependency_cached
|
|
22
|
-
| `automagically_dependency_cached
|
|
23
|
-
| `plot_dependency_graph
|
|
21
|
+
| `dependency_cached` | A footgun-enabled decorator for a method where you **explicitly declare** all direct dependencies. |
|
|
22
|
+
| `automagically_dependency_cached` | A decorator which **automagically infers** the direct dependencies. |
|
|
23
|
+
| `plot_dependency_graph` | Visualizes an instance's dependency graph, for inspection or debugging. |
|
|
24
|
+
| `custom_scenario` | A context manager to temporarily run a custom dependency scenario, restoring the original state on exit. |
|
|
25
|
+
|
|
26
|
+
All decorators accept optional parameters like use_cache, dependencies, track_runtime_dependencies, and serialisable. For exact signatures and types, see the
|
|
27
|
+
[generated stubs file](https://github.com/XtdWt/dependency_cache/blob/master/python/dependency_cache/dependency_cache.pyi).
|
|
24
28
|
|
|
25
29
|
### Setup
|
|
26
30
|
This project uses (and requires) [uv](https://github.com/astral-sh/uv) as a package manager and maturin to compile.
|
|
@@ -57,12 +61,11 @@ from dependency_cache import DependencyCacheBase, automagically_dependency_cache
|
|
|
57
61
|
|
|
58
62
|
class ExampleCalculation(DependencyCacheBase):
|
|
59
63
|
""" C
|
|
60
|
-
|
|
61
|
-
A
|
|
64
|
+
/ \
|
|
65
|
+
A B
|
|
62
66
|
"""
|
|
63
67
|
|
|
64
68
|
def __init__(self, x, y):
|
|
65
|
-
super().__init__()
|
|
66
69
|
self.x = x
|
|
67
70
|
self.y = y
|
|
68
71
|
|
|
@@ -83,14 +86,14 @@ class ExampleCalculation(DependencyCacheBase):
|
|
|
83
86
|
|
|
84
87
|
|
|
85
88
|
c = ExampleCalculation(3, 5)
|
|
86
|
-
print(c.C()) # calculates
|
|
87
|
-
print(c.C()) # hits cache
|
|
89
|
+
print(f"Result of C = {c.C()}") # calculates all, prints 8
|
|
90
|
+
print(f"Result of C = {c.C()}") # hits cache, prints 8
|
|
88
91
|
|
|
89
|
-
c.update_cached_value("A", 0) #
|
|
90
|
-
print(c.C()) # recalculates C
|
|
92
|
+
c.update_cached_value("A", 0) # updates A, then invalidates C
|
|
93
|
+
print(f"Result of C = {c.C()}") # recalculates C returns 5
|
|
91
94
|
```
|
|
92
95
|
|
|
93
|
-
This example can be found [
|
|
96
|
+
This example can be found at [python/examples/example.py](https://github.com/XtdWt/dependency_cache/blob/master/python/examples/example.py) with more runnable examples and use cases provided in the [python/examples](https://github.com/XtdWt/dependency_cache/tree/master/python/examples) folder.
|
|
94
97
|
|
|
95
98
|
```bash
|
|
96
99
|
uv run ./python/examples/example.py
|
|
@@ -100,7 +103,7 @@ uv run ./python/examples/example.py
|
|
|
100
103
|
This project is still a work in progress, with everything from API to underlying design subject to change on my whim.
|
|
101
104
|
|
|
102
105
|
Current TODO list (in no particular order):
|
|
103
|
-
-
|
|
106
|
+
- add global_variable to link objects together
|
|
104
107
|
- add validation to static dependencies, ensure methods are of decorator class (?)
|
|
105
108
|
- improve plot_dependency_graph (maybe to GUI?) with interactive nodes
|
|
106
109
|
- add object thread safety for python 3.14+
|
|
@@ -11,7 +11,7 @@ classifiers = [
|
|
|
11
11
|
"Programming Language :: Python :: Implementation :: PyPy",
|
|
12
12
|
]
|
|
13
13
|
keywords = ["caching", "dependency-graph", "invalidation"]
|
|
14
|
-
version = "0.
|
|
14
|
+
version = "0.5.1"
|
|
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" }
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
from .dependency_cache import (
|
|
2
2
|
DependencyCacheBase,
|
|
3
3
|
automagically_dependency_cached,
|
|
4
|
+
custom_scenario,
|
|
4
5
|
dependency_cached,
|
|
5
6
|
)
|
|
6
7
|
from .plot_dependency_graph import plot_dependency_graph
|
|
@@ -10,4 +11,5 @@ __all__ = [
|
|
|
10
11
|
"DependencyCacheBase",
|
|
11
12
|
"automagically_dependency_cached",
|
|
12
13
|
"plot_dependency_graph",
|
|
14
|
+
"custom_scenario",
|
|
13
15
|
]
|
{dependency_cache-0.4.2 → dependency_cache-0.5.1}/python/dependency_cache/dependency_cache.pyi
RENAMED
|
@@ -29,7 +29,7 @@ class automagically_dependency_cached:
|
|
|
29
29
|
/,
|
|
30
30
|
use_cache: bool = True,
|
|
31
31
|
dependencies: Any | None = None,
|
|
32
|
-
track_runtime_dependencies: bool =
|
|
32
|
+
track_runtime_dependencies: bool = False,
|
|
33
33
|
serialisable: bool = False,
|
|
34
34
|
) -> automagically_dependency_cached: ...
|
|
35
35
|
|
|
@@ -44,3 +44,5 @@ class dependency_cached:
|
|
|
44
44
|
track_runtime_dependencies: bool = False,
|
|
45
45
|
serialisable: bool = False,
|
|
46
46
|
) -> dependency_cached: ...
|
|
47
|
+
|
|
48
|
+
def custom_scenario(items: Sequence[Any]) -> Any: ...
|
{dependency_cache-0.4.2 → dependency_cache-0.5.1}/python/examples/automagic_dependency_example.py
RENAMED
|
@@ -3,14 +3,13 @@ from dependency_cache import DependencyCacheBase, automagically_dependency_cache
|
|
|
3
3
|
|
|
4
4
|
class ExampleCalculationMagic(DependencyCacheBase):
|
|
5
5
|
""" E
|
|
6
|
-
|
|
7
|
-
D
|
|
8
|
-
|
|
9
|
-
A B
|
|
6
|
+
/ \
|
|
7
|
+
D \
|
|
8
|
+
/ \ \
|
|
9
|
+
A B C
|
|
10
10
|
"""
|
|
11
11
|
|
|
12
12
|
def __init__(self, x, y, z):
|
|
13
|
-
super().__init__()
|
|
14
13
|
self.x = x
|
|
15
14
|
self.y = y
|
|
16
15
|
self.z = z
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import re
|
|
2
|
+
import inspect
|
|
3
|
+
import functools
|
|
4
|
+
|
|
5
|
+
from dependency_cache import DependencyCacheBase, dependency_cached
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def custom_decorator(func):
|
|
9
|
+
code = inspect.getsource(func)
|
|
10
|
+
# NOTE: this regex parsing is simply an example and NOT in any way recommended
|
|
11
|
+
dependencies = re.findall(r'self\.([A-Za-z_]\w*)\(\)', code)
|
|
12
|
+
@dependency_cached(dependencies=dependencies)
|
|
13
|
+
@functools.wraps(func)
|
|
14
|
+
def wrapper(*call_args, **call_kwargs):
|
|
15
|
+
return func(*call_args, **call_kwargs)
|
|
16
|
+
return wrapper
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class MultipleDecorators(DependencyCacheBase):
|
|
20
|
+
|
|
21
|
+
@dependency_cached()
|
|
22
|
+
def A(self):
|
|
23
|
+
print("calculating A")
|
|
24
|
+
return 1
|
|
25
|
+
|
|
26
|
+
@dependency_cached()
|
|
27
|
+
def B(self):
|
|
28
|
+
print("calculating B")
|
|
29
|
+
return 2
|
|
30
|
+
|
|
31
|
+
@custom_decorator
|
|
32
|
+
def C(self):
|
|
33
|
+
print("calculating C")
|
|
34
|
+
return self.A() + self.B()
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
if __name__ == '__main__':
|
|
38
|
+
print("Wrap the base `dependency_cached` to create custom decorators")
|
|
39
|
+
print("All decorators that are wrapped with dependency_cached will work together")
|
|
40
|
+
obj = MultipleDecorators()
|
|
41
|
+
print(f"Result of C = {obj.C()}") # calculates all, prints 3
|
|
42
|
+
obj.update_cached_value("B", 4) # updates B, then invalidates C
|
|
43
|
+
print(f"Result of C = {obj.C()}") # calculates all, prints 5
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
from dependency_cache import DependencyCacheBase, automagically_dependency_cached, custom_scenario
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class ExampleCalculation(DependencyCacheBase):
|
|
5
|
+
""" C
|
|
6
|
+
// \\
|
|
7
|
+
A B
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
def __init__(self, x, y):
|
|
11
|
+
super().__init__()
|
|
12
|
+
self.x = x
|
|
13
|
+
self.y = y
|
|
14
|
+
|
|
15
|
+
@automagically_dependency_cached()
|
|
16
|
+
def A(self):
|
|
17
|
+
print("calculating A")
|
|
18
|
+
return self.x
|
|
19
|
+
|
|
20
|
+
@automagically_dependency_cached()
|
|
21
|
+
def B(self):
|
|
22
|
+
print("calculating B")
|
|
23
|
+
return self.y
|
|
24
|
+
|
|
25
|
+
@automagically_dependency_cached()
|
|
26
|
+
def C(self):
|
|
27
|
+
print("calculating C")
|
|
28
|
+
return self.B() + self.A()
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
if __name__ == "__main__":
|
|
32
|
+
print("example of using dependency_cache with custom scenarios, restoring state after")
|
|
33
|
+
print(" C")
|
|
34
|
+
print(" / \\")
|
|
35
|
+
print("A B")
|
|
36
|
+
c = ExampleCalculation(1, 2)
|
|
37
|
+
|
|
38
|
+
calcuations = [c]
|
|
39
|
+
print(f"Result of C = {c.C()}") # # calculates all, prints 3
|
|
40
|
+
with custom_scenario(calcuations):
|
|
41
|
+
c.update_cached_value("A", 100)
|
|
42
|
+
print(f"Result of C = {c.C()}") # recalculates C returns 102
|
|
43
|
+
|
|
44
|
+
with custom_scenario(calcuations):
|
|
45
|
+
c.update_cached_value("B", 200)
|
|
46
|
+
print(f"Result of C = {c.C()}") # recalculates C returns 300
|
|
47
|
+
|
|
48
|
+
print(f"Result of C = {c.C()}") # recalculates C returns 102
|
|
49
|
+
print(f"Result of C = {c.C()}") # recalculates C returns 3
|
|
@@ -3,12 +3,11 @@ from dependency_cache import DependencyCacheBase, automagically_dependency_cache
|
|
|
3
3
|
|
|
4
4
|
class ExampleCalculation(DependencyCacheBase):
|
|
5
5
|
""" C
|
|
6
|
-
|
|
7
|
-
A
|
|
6
|
+
/ \
|
|
7
|
+
A B
|
|
8
8
|
"""
|
|
9
9
|
|
|
10
10
|
def __init__(self, x, y):
|
|
11
|
-
super().__init__()
|
|
12
11
|
self.x = x
|
|
13
12
|
self.y = y
|
|
14
13
|
|
|
@@ -38,6 +37,6 @@ if __name__ == "__main__":
|
|
|
38
37
|
print(f"Result of C = {c.C()}") # calculates all, prints 8
|
|
39
38
|
print(f"cache={c.cached_values} graph={c.dependency_graph} validation={c.validation_state}")
|
|
40
39
|
print(f"Result of C = {c.C()}") # hits cache, prints 8
|
|
41
|
-
c.update_cached_value("A", 0) # invalidates C
|
|
40
|
+
c.update_cached_value("A", 0) # updates A, then invalidates C
|
|
42
41
|
print(f"Result of C = {c.C()}") # recalculates C returns 5
|
|
43
42
|
print(f"cache={c.cached_values} graph={c.dependency_graph} validation={c.validation_state}")
|
|
@@ -2,32 +2,26 @@ from dependency_cache import DependencyCacheBase, automagically_dependency_cache
|
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
class ChildClass(DependencyCacheBase):
|
|
5
|
-
def __init__(self):
|
|
6
|
-
super().__init__()
|
|
7
|
-
|
|
8
5
|
@automagically_dependency_cached()
|
|
9
6
|
def A(self):
|
|
10
|
-
print("
|
|
7
|
+
print("calculating A from Child")
|
|
11
8
|
return 1
|
|
12
9
|
|
|
13
10
|
@automagically_dependency_cached()
|
|
14
11
|
def B(self):
|
|
15
|
-
print("
|
|
12
|
+
print("calculating B from Child")
|
|
16
13
|
return 3
|
|
17
14
|
|
|
18
15
|
|
|
19
16
|
class ParentClass(ChildClass):
|
|
20
|
-
def __init__(self):
|
|
21
|
-
super().__init__()
|
|
22
|
-
|
|
23
17
|
@automagically_dependency_cached()
|
|
24
18
|
def A(self):
|
|
25
|
-
print("
|
|
19
|
+
print("calculating A from Parent")
|
|
26
20
|
return 2
|
|
27
21
|
|
|
28
22
|
@automagically_dependency_cached()
|
|
29
23
|
def B(self):
|
|
30
|
-
print("
|
|
24
|
+
print("calculating B from Parent")
|
|
31
25
|
return super().B()
|
|
32
26
|
|
|
33
27
|
|
{dependency_cache-0.4.2 → dependency_cache-0.5.1}/python/examples/manual_dependency_example.py
RENAMED
|
@@ -3,14 +3,13 @@ from dependency_cache import DependencyCacheBase, dependency_cached
|
|
|
3
3
|
|
|
4
4
|
class ExampleCalculationManual(DependencyCacheBase):
|
|
5
5
|
"""D E
|
|
6
|
-
|
|
6
|
+
\ /
|
|
7
7
|
C
|
|
8
|
-
|
|
9
|
-
A
|
|
8
|
+
/ \
|
|
9
|
+
A B
|
|
10
10
|
"""
|
|
11
11
|
|
|
12
12
|
def __init__(self, x, y):
|
|
13
|
-
super().__init__()
|
|
14
13
|
self.x = x
|
|
15
14
|
self.y = y
|
|
16
15
|
|
{dependency_cache-0.4.2 → dependency_cache-0.5.1}/python/examples/runtime_dependencies_example.py
RENAMED
|
@@ -2,10 +2,10 @@ from dependency_cache import DependencyCacheBase, dependency_cached
|
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
class RuntimeGraphExample(DependencyCacheBase):
|
|
5
|
-
"""
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
C
|
|
5
|
+
""" A
|
|
6
|
+
/ | \
|
|
7
|
+
/ | \
|
|
8
|
+
C B D (detached until B changes)
|
|
9
9
|
"""
|
|
10
10
|
|
|
11
11
|
@dependency_cached(track_runtime_dependencies=True)
|
{dependency_cache-0.4.2 → dependency_cache-0.5.1}/python/examples/runtime_function_calls_example.py
RENAMED
|
@@ -2,9 +2,6 @@ from dependency_cache import DependencyCacheBase, dependency_cached
|
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
class FibonacciExample(DependencyCacheBase):
|
|
5
|
-
def __init__(self):
|
|
6
|
-
super().__init__()
|
|
7
|
-
|
|
8
5
|
@dependency_cached(track_runtime_dependencies=True)
|
|
9
6
|
def Calculate(self, n):
|
|
10
7
|
print(f"calculating the {n=} fibonacci number")
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
from dependency_cache import DependencyCacheBase, dependency_cached
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class SerialisableCalculation(DependencyCacheBase):
|
|
5
|
+
|
|
6
|
+
@dependency_cached(serialisable=True)
|
|
7
|
+
def A(self):
|
|
8
|
+
print("calculating A")
|
|
9
|
+
return 1
|
|
10
|
+
|
|
11
|
+
@dependency_cached(serialisable=True)
|
|
12
|
+
def B(self):
|
|
13
|
+
print("calculating B")
|
|
14
|
+
return 1
|
|
15
|
+
|
|
16
|
+
@dependency_cached(serialisable=True, dependencies=["A", "B"])
|
|
17
|
+
def C(self):
|
|
18
|
+
print("calculating C")
|
|
19
|
+
return self.A() + self.B()
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
if __name__ == "__main__":
|
|
23
|
+
obj1 = SerialisableCalculation()
|
|
24
|
+
print(f"Result of C = {obj1.C()}") # calculates all, prints 2
|
|
25
|
+
|
|
26
|
+
saved_state = obj1.dump_cache() # serialises the cache
|
|
27
|
+
print(f"{saved_state=}")
|
|
28
|
+
|
|
29
|
+
obj2 = SerialisableCalculation()
|
|
30
|
+
obj2.load_cache(saved_state) # deserialises the cache
|
|
31
|
+
print(f"Result of C = {obj2.C()}") # prints 2, cached value is reused
|
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
import pytest
|
|
2
|
-
from dependency_cache import DependencyCacheBase, dependency_cached, plot_dependency_graph
|
|
2
|
+
from dependency_cache import DependencyCacheBase, custom_scenario, dependency_cached, plot_dependency_graph
|
|
3
3
|
|
|
4
4
|
|
|
5
5
|
class IncorrectInheritance:
|
|
6
|
-
def __init__(self):
|
|
7
|
-
pass
|
|
8
|
-
|
|
9
6
|
@dependency_cached()
|
|
10
7
|
def A(self):
|
|
11
8
|
return 1
|
|
@@ -13,7 +10,6 @@ class IncorrectInheritance:
|
|
|
13
10
|
|
|
14
11
|
class SimpleTestObj(DependencyCacheBase):
|
|
15
12
|
def __init__(self, x, y):
|
|
16
|
-
super().__init__()
|
|
17
13
|
self.x = x
|
|
18
14
|
self.y = y
|
|
19
15
|
|
|
@@ -32,7 +28,6 @@ class SimpleTestObj(DependencyCacheBase):
|
|
|
32
28
|
|
|
33
29
|
class HardTestObj(DependencyCacheBase):
|
|
34
30
|
def __init__(self, x, y):
|
|
35
|
-
super().__init__()
|
|
36
31
|
self.x = x
|
|
37
32
|
self.y = y
|
|
38
33
|
|
|
@@ -59,7 +54,6 @@ class HardTestObj(DependencyCacheBase):
|
|
|
59
54
|
|
|
60
55
|
class HarderTestObj(DependencyCacheBase):
|
|
61
56
|
def __init__(self, x, y):
|
|
62
|
-
super().__init__()
|
|
63
57
|
self.x = x
|
|
64
58
|
self.y = y
|
|
65
59
|
|
|
@@ -90,7 +84,6 @@ class HarderTestObj(DependencyCacheBase):
|
|
|
90
84
|
|
|
91
85
|
class UseCacheObj(DependencyCacheBase):
|
|
92
86
|
def __init__(self, x):
|
|
93
|
-
super().__init__()
|
|
94
87
|
self.x = x
|
|
95
88
|
|
|
96
89
|
@dependency_cached(use_cache=False)
|
|
@@ -105,7 +98,6 @@ class UseCacheObj(DependencyCacheBase):
|
|
|
105
98
|
|
|
106
99
|
class FunctionCallObj(DependencyCacheBase):
|
|
107
100
|
def __init__(self, x):
|
|
108
|
-
super().__init__()
|
|
109
101
|
self.x = x
|
|
110
102
|
|
|
111
103
|
@dependency_cached()
|
|
@@ -155,9 +147,6 @@ def test_incorrect_method_raises_valueerror():
|
|
|
155
147
|
with pytest.raises(TypeError):
|
|
156
148
|
|
|
157
149
|
class IncorrectMethod(DependencyCacheBase):
|
|
158
|
-
def __init__(self):
|
|
159
|
-
super().__init__()
|
|
160
|
-
|
|
161
150
|
@dependency_cached()
|
|
162
151
|
def A():
|
|
163
152
|
return 1
|
|
@@ -322,9 +311,6 @@ def test_incorrect_decorator_format():
|
|
|
322
311
|
with pytest.raises(ValueError):
|
|
323
312
|
|
|
324
313
|
class IncorrectDependencyType(DependencyCacheBase):
|
|
325
|
-
def __init__(self):
|
|
326
|
-
pass
|
|
327
|
-
|
|
328
314
|
@dependency_cached(dependencies=[3, ("B", [1])])
|
|
329
315
|
def A(self):
|
|
330
316
|
return 1
|
|
@@ -373,3 +359,31 @@ def test_strict_get_raises():
|
|
|
373
359
|
obj.get_cached_value("A")
|
|
374
360
|
|
|
375
361
|
assert obj.get_cached_value("A", strict=False) is None
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
@pytest.mark.parametrize(
|
|
365
|
+
("x", "y"),
|
|
366
|
+
[
|
|
367
|
+
(1, 2),
|
|
368
|
+
(100.1, 200.2),
|
|
369
|
+
(-55, -101),
|
|
370
|
+
],
|
|
371
|
+
)
|
|
372
|
+
def test_custom_scenario(x, y):
|
|
373
|
+
obj = SimpleTestObj(x, y)
|
|
374
|
+
|
|
375
|
+
assert obj.C() == x + y
|
|
376
|
+
|
|
377
|
+
with custom_scenario([obj]):
|
|
378
|
+
obj.update_cached_value("A", x * 10)
|
|
379
|
+
|
|
380
|
+
assert obj.C() == x * 10 + y
|
|
381
|
+
|
|
382
|
+
with custom_scenario([obj]):
|
|
383
|
+
obj.update_cached_value("B", y + 3000)
|
|
384
|
+
|
|
385
|
+
assert obj.C() == x * 10 + (y + 3000)
|
|
386
|
+
|
|
387
|
+
assert obj.C() == x * 10 + y
|
|
388
|
+
|
|
389
|
+
assert obj.C() == x + y
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import pytest
|
|
2
|
-
from dependency_cache import
|
|
2
|
+
from dependency_cache import (
|
|
3
|
+
DependencyCacheBase,
|
|
4
|
+
automagically_dependency_cached,
|
|
5
|
+
custom_scenario,
|
|
6
|
+
plot_dependency_graph,
|
|
7
|
+
)
|
|
3
8
|
|
|
4
9
|
|
|
5
10
|
class IncorrectInheritance:
|
|
6
|
-
def __init__(self):
|
|
7
|
-
pass
|
|
8
|
-
|
|
9
11
|
@automagically_dependency_cached()
|
|
10
12
|
def A(self):
|
|
11
13
|
return 1
|
|
@@ -13,7 +15,6 @@ class IncorrectInheritance:
|
|
|
13
15
|
|
|
14
16
|
class SimpleTestObj(DependencyCacheBase):
|
|
15
17
|
def __init__(self, x, y):
|
|
16
|
-
super().__init__()
|
|
17
18
|
self.x = x
|
|
18
19
|
self.y = y
|
|
19
20
|
|
|
@@ -32,7 +33,6 @@ class SimpleTestObj(DependencyCacheBase):
|
|
|
32
33
|
|
|
33
34
|
class HardTestObj(DependencyCacheBase):
|
|
34
35
|
def __init__(self, x, y):
|
|
35
|
-
super().__init__()
|
|
36
36
|
self.x = x
|
|
37
37
|
self.y = y
|
|
38
38
|
|
|
@@ -59,7 +59,6 @@ class HardTestObj(DependencyCacheBase):
|
|
|
59
59
|
|
|
60
60
|
class HarderTestObj(DependencyCacheBase):
|
|
61
61
|
def __init__(self, x, y):
|
|
62
|
-
super().__init__()
|
|
63
62
|
self.x = x
|
|
64
63
|
self.y = y
|
|
65
64
|
|
|
@@ -90,7 +89,6 @@ class HarderTestObj(DependencyCacheBase):
|
|
|
90
89
|
|
|
91
90
|
class UseCacheObj(DependencyCacheBase):
|
|
92
91
|
def __init__(self, x):
|
|
93
|
-
super().__init__()
|
|
94
92
|
self.x = x
|
|
95
93
|
|
|
96
94
|
@automagically_dependency_cached(use_cache=False)
|
|
@@ -105,7 +103,6 @@ class UseCacheObj(DependencyCacheBase):
|
|
|
105
103
|
|
|
106
104
|
class FunctionCallObj(DependencyCacheBase):
|
|
107
105
|
def __init__(self, x):
|
|
108
|
-
super().__init__()
|
|
109
106
|
self.x = x
|
|
110
107
|
|
|
111
108
|
@automagically_dependency_cached()
|
|
@@ -113,7 +110,7 @@ class FunctionCallObj(DependencyCacheBase):
|
|
|
113
110
|
print("calculating A!")
|
|
114
111
|
return x + self.x
|
|
115
112
|
|
|
116
|
-
@automagically_dependency_cached()
|
|
113
|
+
@automagically_dependency_cached(track_runtime_dependencies=True)
|
|
117
114
|
def B(self):
|
|
118
115
|
total = 0
|
|
119
116
|
for i in range(2):
|
|
@@ -131,9 +128,6 @@ def test_incorrect_method_raises_valueerror():
|
|
|
131
128
|
with pytest.raises(TypeError):
|
|
132
129
|
|
|
133
130
|
class IncorrectMethod(DependencyCacheBase):
|
|
134
|
-
def __init__(self):
|
|
135
|
-
super().__init__()
|
|
136
|
-
|
|
137
131
|
@automagically_dependency_cached()
|
|
138
132
|
def A():
|
|
139
133
|
return 1
|
|
@@ -298,9 +292,6 @@ def test_incorrect_decorator_format():
|
|
|
298
292
|
with pytest.raises(ValueError):
|
|
299
293
|
|
|
300
294
|
class IncorrectDependencyType(DependencyCacheBase):
|
|
301
|
-
def __init__(self):
|
|
302
|
-
pass
|
|
303
|
-
|
|
304
295
|
@automagically_dependency_cached(dependencies=[3, ("B", [1])])
|
|
305
296
|
def A(self):
|
|
306
297
|
return 1
|
|
@@ -308,3 +299,31 @@ def test_incorrect_decorator_format():
|
|
|
308
299
|
@automagically_dependency_cached()
|
|
309
300
|
def B(self):
|
|
310
301
|
return 2
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
@pytest.mark.parametrize(
|
|
305
|
+
("x", "y"),
|
|
306
|
+
[
|
|
307
|
+
(1, 2),
|
|
308
|
+
(100.1, 200.2),
|
|
309
|
+
(-55, -101),
|
|
310
|
+
],
|
|
311
|
+
)
|
|
312
|
+
def test_custom_scenario(x, y):
|
|
313
|
+
obj = SimpleTestObj(x, y)
|
|
314
|
+
|
|
315
|
+
assert obj.C() == x + y
|
|
316
|
+
|
|
317
|
+
with custom_scenario([obj]):
|
|
318
|
+
obj.update_cached_value("A", x * 10)
|
|
319
|
+
|
|
320
|
+
assert obj.C() == x * 10 + y
|
|
321
|
+
|
|
322
|
+
with custom_scenario([obj]):
|
|
323
|
+
obj.update_cached_value("B", y + 3000)
|
|
324
|
+
|
|
325
|
+
assert obj.C() == x * 10 + (y + 3000)
|
|
326
|
+
|
|
327
|
+
assert obj.C() == x * 10 + y
|
|
328
|
+
|
|
329
|
+
assert obj.C() == x + y
|
|
@@ -21,6 +21,16 @@ impl DependencyCacheDecorator {
|
|
|
21
21
|
return Err(PyTypeError::new_err("Cannot assign to decorated method"));
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
#[getter]
|
|
25
|
+
fn __name__(&self) -> &str {
|
|
26
|
+
return &self.method_name;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
#[getter]
|
|
30
|
+
fn __qualname__(&self) -> &str {
|
|
31
|
+
return &self.method_name;
|
|
32
|
+
}
|
|
33
|
+
|
|
24
34
|
fn __get__(
|
|
25
35
|
slf: &Bound<'_, Self>,
|
|
26
36
|
obj: Bound<'_, PyAny>,
|
|
@@ -85,7 +85,7 @@ pub struct AutomagicDependencyCacheDecoratorFactory {
|
|
|
85
85
|
#[pymethods]
|
|
86
86
|
impl AutomagicDependencyCacheDecoratorFactory {
|
|
87
87
|
#[new]
|
|
88
|
-
#[pyo3(signature = (use_cache=true, dependencies=None, track_runtime_dependencies=
|
|
88
|
+
#[pyo3(signature = (use_cache=true, dependencies=None, track_runtime_dependencies=false, serialisable=false))]
|
|
89
89
|
fn new(
|
|
90
90
|
py: Python<'_>,
|
|
91
91
|
use_cache: bool,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
use pyo3::prelude::*;
|
|
2
2
|
use pyo3::types::{PyDict, PyTuple, PyString, PySet, PyType};
|
|
3
|
-
use pyo3::exceptions::{
|
|
3
|
+
use pyo3::exceptions::{PyKeyError, PyRuntimeError, PyValueError};
|
|
4
4
|
|
|
5
5
|
use rand::rng;
|
|
6
6
|
use rand::seq::SliceRandom;
|
|
@@ -13,12 +13,19 @@ use crate::decorator::DependencyCacheDecorator;
|
|
|
13
13
|
use crate::metadata_hash_manager::{CacheKey, MetadataHashManager};
|
|
14
14
|
|
|
15
15
|
|
|
16
|
+
pub struct CacheLayer {
|
|
17
|
+
pub cache: HashMap<CacheKey, Py<PyAny>>,
|
|
18
|
+
pub graph_snapshot: MethodDependencyGraph<CacheKey>, //TODO: maybe less expensive way to handling graph snapshot...
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
|
|
16
22
|
#[pyclass(subclass)]
|
|
17
23
|
pub struct DependencyCacheBase {
|
|
18
24
|
pub cache: HashMap<CacheKey, Py<PyAny>>,
|
|
19
25
|
pub method_dependency_graph: MethodDependencyGraph<CacheKey>,
|
|
20
26
|
pub metadata_hash_manager: MetadataHashManager,
|
|
21
27
|
pub call_stack: Vec<(CacheKey, bool)>,
|
|
28
|
+
pub cache_stack: Vec<CacheLayer>,
|
|
22
29
|
}
|
|
23
30
|
|
|
24
31
|
impl DependencyCacheBase {
|
|
@@ -27,7 +34,11 @@ impl DependencyCacheBase {
|
|
|
27
34
|
if !self.method_dependency_graph.is_valid(key) {
|
|
28
35
|
return ();
|
|
29
36
|
}
|
|
30
|
-
self.
|
|
37
|
+
if let Some(layer) = self.cache_stack.last_mut() {
|
|
38
|
+
layer.cache.insert(key, value);
|
|
39
|
+
} else {
|
|
40
|
+
self.cache.insert(key, value);
|
|
41
|
+
};
|
|
31
42
|
}
|
|
32
43
|
|
|
33
44
|
pub fn validate_current_method(&mut self, key: CacheKey, use_cache: bool) {
|
|
@@ -64,7 +75,7 @@ impl DependencyCacheBase {
|
|
|
64
75
|
}
|
|
65
76
|
|
|
66
77
|
pub fn current_call_stack_top(&self) -> Option<(CacheKey, bool)> {
|
|
67
|
-
self.call_stack.last().cloned()
|
|
78
|
+
return self.call_stack.last().cloned();
|
|
68
79
|
}
|
|
69
80
|
|
|
70
81
|
pub fn push_call_stack(&mut self, key: CacheKey, add_parent_dependencies: bool) {
|
|
@@ -87,6 +98,9 @@ impl DependencyCacheBase {
|
|
|
87
98
|
|
|
88
99
|
pub fn get_cached_value_by_key(&self, py: Python<'_>, key: CacheKey) -> Option<Py<PyAny>> {
|
|
89
100
|
if self.method_dependency_graph.is_valid(key) {
|
|
101
|
+
if let Some(temp_cache_value) = self.get_from_temporary_cache(py, &key) {
|
|
102
|
+
return Some(temp_cache_value);
|
|
103
|
+
}
|
|
90
104
|
return self.cache.get(&key).map(|obj| obj.clone_ref(py));
|
|
91
105
|
}
|
|
92
106
|
return None;
|
|
@@ -107,6 +121,79 @@ impl DependencyCacheBase {
|
|
|
107
121
|
let key = self.metadata_hash_manager.get_cache_key(py, &hash, &sig);
|
|
108
122
|
return key;
|
|
109
123
|
}
|
|
124
|
+
|
|
125
|
+
pub fn push_cache_stack(&mut self) {
|
|
126
|
+
return self.cache_stack.push(CacheLayer {
|
|
127
|
+
cache: HashMap::new(),
|
|
128
|
+
graph_snapshot: self.method_dependency_graph.clone(),
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
pub fn pop_cache_stack(&mut self) -> PyResult<()> {
|
|
133
|
+
let layer = self
|
|
134
|
+
.cache_stack
|
|
135
|
+
.pop()
|
|
136
|
+
.ok_or_else(|| PyRuntimeError::new_err("Attempted to remove non existant layer"))?;
|
|
137
|
+
|
|
138
|
+
self.method_dependency_graph = layer.graph_snapshot;
|
|
139
|
+
return Ok(());
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
pub fn get_from_temporary_cache(&self, py: Python<'_>, key: &CacheKey) -> Option<Py<PyAny>> {
|
|
143
|
+
for layer in self.cache_stack.iter().rev() {
|
|
144
|
+
if let Some(v) = layer.cache.get(key) {
|
|
145
|
+
return Some(v.clone_ref(py));
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return None;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
pub fn contains_cached_value(&self, key: &CacheKey) -> bool {
|
|
152
|
+
if !self.method_dependency_graph.is_valid(*key) {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
return self.cache_stack
|
|
156
|
+
.iter()
|
|
157
|
+
.rev()
|
|
158
|
+
.any(|layer| layer.cache.contains_key(key))
|
|
159
|
+
|| self.cache.contains_key(key);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
pub fn remove_cached_value(&mut self, key: &CacheKey) -> Option<Py<PyAny>> {
|
|
163
|
+
for layer in self.cache_stack.iter_mut().rev() {
|
|
164
|
+
if let Some(v) = layer.cache.remove(key) {
|
|
165
|
+
return Some(v);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return self.cache.remove(key);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
pub fn effective_cache(&self) -> HashMap<&CacheKey, &Py<PyAny>> {
|
|
172
|
+
let mut merged: HashMap<&CacheKey, &Py<PyAny>> = self.cache.iter().collect();
|
|
173
|
+
for layer in &self.cache_stack {
|
|
174
|
+
for (k, v) in &layer.cache {
|
|
175
|
+
merged.insert(k, v);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return merged;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
pub fn clear_effective_cache(&mut self) -> () {
|
|
182
|
+
if let Some(layer) = self.cache_stack.last_mut() {
|
|
183
|
+
let removed_keys: Vec<CacheKey> = {
|
|
184
|
+
let keys = layer.cache.keys().cloned().collect();
|
|
185
|
+
layer.cache.clear();
|
|
186
|
+
keys
|
|
187
|
+
};
|
|
188
|
+
for key in removed_keys {
|
|
189
|
+
self.method_dependency_graph.temporarily_invalidate(key);
|
|
190
|
+
}
|
|
191
|
+
layer.cache.clear();
|
|
192
|
+
} else {
|
|
193
|
+
self.method_dependency_graph.invalidate_all();
|
|
194
|
+
self.cache.clear();
|
|
195
|
+
}
|
|
196
|
+
}
|
|
110
197
|
}
|
|
111
198
|
|
|
112
199
|
#[pymethods]
|
|
@@ -119,17 +206,22 @@ impl DependencyCacheBase {
|
|
|
119
206
|
method_dependency_graph: MethodDependencyGraph::new(),
|
|
120
207
|
metadata_hash_manager: MetadataHashManager::new(),
|
|
121
208
|
call_stack: Vec::new(),
|
|
209
|
+
cache_stack: Vec::new(),
|
|
122
210
|
};
|
|
123
211
|
}
|
|
124
212
|
|
|
125
213
|
#[pyo3(signature = (method_name, strict=true, **kwargs))]
|
|
126
|
-
pub fn get_cached_value(
|
|
214
|
+
pub fn get_cached_value(
|
|
215
|
+
&mut self,
|
|
216
|
+
py: Python<'_>,
|
|
217
|
+
method_name: String,
|
|
218
|
+
strict: bool,
|
|
219
|
+
kwargs: Option<Py<PyDict>>,
|
|
220
|
+
) -> PyResult<Option<Py<PyAny>>> {
|
|
127
221
|
let key = self.get_key(py, &method_name, &kwargs)?;
|
|
128
222
|
|
|
129
|
-
if self.
|
|
130
|
-
|
|
131
|
-
return Ok(Some(obj.clone_ref(py)));
|
|
132
|
-
}
|
|
223
|
+
if let Some(value) = self.get_cached_value_by_key(py, key) {
|
|
224
|
+
return Ok(Some(value));
|
|
133
225
|
}
|
|
134
226
|
|
|
135
227
|
if strict {
|
|
@@ -148,47 +240,58 @@ impl DependencyCacheBase {
|
|
|
148
240
|
kwargs: Option<Py<PyDict>>,
|
|
149
241
|
) -> PyResult<bool> {
|
|
150
242
|
let key = self.get_key(py, &method_name, &kwargs)?;
|
|
151
|
-
Ok(self.
|
|
243
|
+
return Ok(self.contains_cached_value(&key));
|
|
152
244
|
}
|
|
153
245
|
|
|
154
246
|
|
|
155
247
|
#[pyo3(signature = (method_name, value, **kwargs))]
|
|
156
|
-
pub fn update_cached_value(
|
|
248
|
+
pub fn update_cached_value(
|
|
249
|
+
&mut self,
|
|
250
|
+
py: Python<'_>,
|
|
251
|
+
method_name: String,
|
|
252
|
+
value: Py<PyAny>,
|
|
253
|
+
kwargs: Option<Py<PyDict>>,
|
|
254
|
+
) -> PyResult<()> {
|
|
157
255
|
let key = self.get_key(py, &method_name, &kwargs)?;
|
|
158
256
|
|
|
159
|
-
|
|
257
|
+
if !self.contains_cached_value(&key) {
|
|
160
258
|
return Err(PyKeyError::new_err(format!(
|
|
161
259
|
"Cannot update cached value for '{method_name}'"
|
|
162
260
|
)));
|
|
163
261
|
};
|
|
164
|
-
|
|
262
|
+
self.set_cached_value_by_key(key, value);
|
|
165
263
|
self.method_dependency_graph.temporarily_invalidate(key);
|
|
166
264
|
self.method_dependency_graph.validate(key);
|
|
167
265
|
return Ok(());
|
|
168
266
|
}
|
|
169
267
|
|
|
170
268
|
pub fn clear_cache(&mut self) {
|
|
171
|
-
self.
|
|
172
|
-
self.cache.clear();
|
|
269
|
+
self.clear_effective_cache();
|
|
173
270
|
}
|
|
174
271
|
|
|
175
272
|
#[pyo3(signature = (method_name, **kwargs))]
|
|
176
|
-
pub fn clear_cached_value(
|
|
273
|
+
pub fn clear_cached_value(
|
|
274
|
+
&mut self,
|
|
275
|
+
py: Python<'_>,
|
|
276
|
+
method_name: String,
|
|
277
|
+
kwargs: Option<Py<PyDict>>,
|
|
278
|
+
) -> PyResult<()> {
|
|
177
279
|
let key = self.get_key(py, &method_name, &kwargs)?;
|
|
178
280
|
|
|
179
|
-
if self.
|
|
281
|
+
if self.remove_cached_value(&key).is_none() {
|
|
180
282
|
return Err(PyKeyError::new_err(format!(
|
|
181
283
|
"Cannot clear cached value for '{method_name}'"
|
|
182
284
|
)));
|
|
183
285
|
}
|
|
184
286
|
self.method_dependency_graph.temporarily_invalidate(key);
|
|
185
|
-
Ok(())
|
|
287
|
+
return Ok(());
|
|
186
288
|
}
|
|
187
289
|
|
|
188
290
|
#[getter]
|
|
189
291
|
pub fn get_cached_values<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyDict>> {
|
|
190
292
|
let dict = PyDict::new(py);
|
|
191
|
-
|
|
293
|
+
let cache_state = self.effective_cache();
|
|
294
|
+
for (key, value) in &cache_state {
|
|
192
295
|
let Some(bound) = self.metadata_hash_manager.get_signature(py, key) else {
|
|
193
296
|
continue;
|
|
194
297
|
};
|
|
@@ -206,7 +309,7 @@ impl DependencyCacheBase {
|
|
|
206
309
|
|
|
207
310
|
dict.set_item(key, value.clone_ref(py))?;
|
|
208
311
|
}
|
|
209
|
-
Ok(dict)
|
|
312
|
+
return Ok(dict);
|
|
210
313
|
}
|
|
211
314
|
|
|
212
315
|
#[getter]
|
|
@@ -250,7 +353,7 @@ impl DependencyCacheBase {
|
|
|
250
353
|
let list = PySet::new(py, parent_bounds)?;
|
|
251
354
|
dict.set_item(child_key, list)?;
|
|
252
355
|
}
|
|
253
|
-
Ok(dict)
|
|
356
|
+
return Ok(dict);
|
|
254
357
|
}
|
|
255
358
|
|
|
256
359
|
#[getter]
|
|
@@ -274,7 +377,7 @@ impl DependencyCacheBase {
|
|
|
274
377
|
|
|
275
378
|
dict.set_item(key, state)?;
|
|
276
379
|
}
|
|
277
|
-
Ok(dict)
|
|
380
|
+
return Ok(dict);
|
|
278
381
|
}
|
|
279
382
|
|
|
280
383
|
#[pyo3(signature = (order=None))]
|
|
@@ -324,7 +427,7 @@ impl DependencyCacheBase {
|
|
|
324
427
|
result.set_item(method_name, value)?;
|
|
325
428
|
}
|
|
326
429
|
|
|
327
|
-
Ok(result)
|
|
430
|
+
return Ok(result);
|
|
328
431
|
}
|
|
329
432
|
|
|
330
433
|
#[pyo3(signature = (data, order=None))]
|
|
@@ -403,6 +506,6 @@ impl DependencyCacheBase {
|
|
|
403
506
|
load_method(&name)?;
|
|
404
507
|
}
|
|
405
508
|
|
|
406
|
-
Ok(())
|
|
509
|
+
return Ok(());
|
|
407
510
|
}
|
|
408
511
|
}
|
|
@@ -9,6 +9,8 @@ pub enum ValidationState {
|
|
|
9
9
|
PermanentlyInvalid,
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
+
|
|
13
|
+
#[derive(Clone)]
|
|
12
14
|
pub struct MethodDependencyGraph<T> {
|
|
13
15
|
validation_state: HashMap<T, ValidationState>,
|
|
14
16
|
dependency_graph: HashMap<T, HashSet<T>>, // maps child_name to set[parents_name], for ease of traversal
|
|
@@ -4,11 +4,13 @@ mod dependency_cache_base;
|
|
|
4
4
|
mod decorator_factory;
|
|
5
5
|
mod py_introspection_utils;
|
|
6
6
|
mod metadata_hash_manager;
|
|
7
|
+
mod scenario;
|
|
7
8
|
|
|
8
9
|
use pyo3::prelude::*;
|
|
9
10
|
|
|
10
11
|
use dependency_cache_base::DependencyCacheBase;
|
|
11
12
|
use decorator_factory::{ManualDependencyCacheDecoratorFactory, AutomagicDependencyCacheDecoratorFactory};
|
|
13
|
+
use scenario::custom_scenario;
|
|
12
14
|
|
|
13
15
|
|
|
14
16
|
#[pymodule]
|
|
@@ -22,4 +24,7 @@ mod dependency_cache {
|
|
|
22
24
|
|
|
23
25
|
#[pymodule_export]
|
|
24
26
|
use super::AutomagicDependencyCacheDecoratorFactory;
|
|
27
|
+
|
|
28
|
+
#[pymodule_export]
|
|
29
|
+
use super::custom_scenario;
|
|
25
30
|
}
|
|
@@ -32,7 +32,7 @@ impl MetadataHashManager {
|
|
|
32
32
|
return Ok(Some(key));
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
|
-
Ok(None)
|
|
35
|
+
return Ok(None);
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
pub fn get_cache_key(
|
|
@@ -41,8 +41,8 @@ impl MetadataHashManager {
|
|
|
41
41
|
hash: &isize,
|
|
42
42
|
sig: &Bound<'_, PyTuple>,
|
|
43
43
|
) -> PyResult<CacheKey> {
|
|
44
|
-
self.find_cache_key(py, hash, sig)?
|
|
45
|
-
.ok_or_else(|| PyKeyError::new_err("No cache key found for provided signature"))
|
|
44
|
+
return self.find_cache_key(py, hash, sig)?
|
|
45
|
+
.ok_or_else(|| PyKeyError::new_err("No cache key found for provided signature"));
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
pub fn create_cache_key(
|
|
@@ -58,10 +58,10 @@ impl MetadataHashManager {
|
|
|
58
58
|
self.next_id += 1;
|
|
59
59
|
self.buckets.entry(hash).or_default().push(key);
|
|
60
60
|
self.signatures.insert(key, sig);
|
|
61
|
-
Ok(key)
|
|
61
|
+
return Ok(key);
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
pub fn get_signature<'py>(&self, py: Python<'py>, id: &CacheKey) -> Option<Bound<'py, PyTuple>> {
|
|
65
|
-
self.signatures.get(id).map(|s| s.bind(py).clone())
|
|
65
|
+
return self.signatures.get(id).map(|s| s.bind(py).clone());
|
|
66
66
|
}
|
|
67
67
|
}
|
|
@@ -81,7 +81,7 @@ pub fn validate_self_only_method(py: Python<'_>, func: &Bound<'_, PyAny>) -> PyR
|
|
|
81
81
|
);
|
|
82
82
|
return Err(PyValueError::new_err(msg));
|
|
83
83
|
}
|
|
84
|
-
Ok(())
|
|
84
|
+
return Ok(());
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
|
|
@@ -165,7 +165,7 @@ fn visit(
|
|
|
165
165
|
visit(py, &child?, ast_module, dependencies, visited)?;
|
|
166
166
|
}
|
|
167
167
|
|
|
168
|
-
Ok(())
|
|
168
|
+
return Ok(());
|
|
169
169
|
}
|
|
170
170
|
|
|
171
171
|
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
use pyo3::prelude::*;
|
|
2
|
+
use pyo3::exceptions::PyTypeError;
|
|
3
|
+
|
|
4
|
+
use crate::DependencyCacheBase;
|
|
5
|
+
|
|
6
|
+
#[pyclass]
|
|
7
|
+
pub struct Scenario {
|
|
8
|
+
items: Vec<Py<PyAny>>,
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
#[pymethods]
|
|
13
|
+
impl Scenario {
|
|
14
|
+
fn __enter__<'py>(slf: &Bound<'py, Self>, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
|
|
15
|
+
{
|
|
16
|
+
let scenario = slf.borrow();
|
|
17
|
+
for item in &scenario.items {
|
|
18
|
+
let base = item
|
|
19
|
+
.bind(py)
|
|
20
|
+
.cast::<DependencyCacheBase>()
|
|
21
|
+
.map_err(|_| {
|
|
22
|
+
PyTypeError::new_err(
|
|
23
|
+
"custom_scenario() expects items to inherit from DependencyCacheBase",
|
|
24
|
+
)
|
|
25
|
+
})?;
|
|
26
|
+
base.borrow_mut().push_cache_stack();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return Ok(slf.clone().into_any());
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
fn __exit__<'py>(
|
|
33
|
+
slf: &Bound<'py, Self>,
|
|
34
|
+
py: Python<'py>,
|
|
35
|
+
_exc_type: Option<Bound<'_, PyAny>>,
|
|
36
|
+
_exc_value: Option<Bound<'_, PyAny>>,
|
|
37
|
+
_traceback: Option<Bound<'_, PyAny>>,
|
|
38
|
+
) -> PyResult<bool> {
|
|
39
|
+
{
|
|
40
|
+
let scenario = slf.borrow();
|
|
41
|
+
for item in scenario.items.iter() {
|
|
42
|
+
let base = item
|
|
43
|
+
.bind(py)
|
|
44
|
+
.cast::<DependencyCacheBase>()
|
|
45
|
+
.map_err(|_| {
|
|
46
|
+
PyTypeError::new_err(
|
|
47
|
+
"custom_scenario() expects items to inherit from DependencyCacheBase",
|
|
48
|
+
)
|
|
49
|
+
})?;
|
|
50
|
+
base.borrow_mut().pop_cache_stack()?;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return Ok(false);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
#[pyfunction]
|
|
59
|
+
pub fn custom_scenario(items: Vec<Py<PyAny>>) -> PyResult<Scenario> {
|
|
60
|
+
return Ok(Scenario { items });
|
|
61
|
+
}
|
|
@@ -329,7 +329,7 @@ wheels = [
|
|
|
329
329
|
|
|
330
330
|
[[package]]
|
|
331
331
|
name = "dependency-cache"
|
|
332
|
-
version = "0.
|
|
332
|
+
version = "0.5.1"
|
|
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'" },
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{dependency_cache-0.4.2 → dependency_cache-0.5.1}/python/dependency_cache/plot_dependency_graph.py
RENAMED
|
File without changes
|