dependency-cache 0.5.0__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.
Files changed (34) hide show
  1. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/PKG-INFO +2 -2
  2. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/README.md +1 -1
  3. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/pyproject.toml +1 -1
  4. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/src/decorator.rs +2 -2
  5. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/src/dependency_cache_base.rs +33 -22
  6. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/src/dependency_graph.rs +2 -0
  7. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/src/metadata_hash_manager.rs +5 -5
  8. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/src/py_introspection_utils.rs +2 -2
  9. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/src/scenario.rs +5 -5
  10. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/uv.lock +1 -1
  11. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/.github/workflows/CI.yml +0 -0
  12. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/.github/workflows/Test.yml +0 -0
  13. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/.gitignore +0 -0
  14. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/Cargo.lock +0 -0
  15. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/Cargo.toml +0 -0
  16. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/LICENSE +0 -0
  17. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/python/dependency_cache/__init__.py +0 -0
  18. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/python/dependency_cache/dependency_cache.pyi +0 -0
  19. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/python/dependency_cache/plot_dependency_graph.py +0 -0
  20. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/python/examples/automagic_dependency_example.py +0 -0
  21. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/python/examples/custom_decorator_example.py +0 -0
  22. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/python/examples/custom_scenario_example.py +0 -0
  23. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/python/examples/example.py +0 -0
  24. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/python/examples/inheritance_example.py +0 -0
  25. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/python/examples/manual_dependency_example.py +0 -0
  26. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/python/examples/runtime_dependencies_example.py +0 -0
  27. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/python/examples/runtime_function_calls_example.py +0 -0
  28. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/python/examples/serialisable_example.py +0 -0
  29. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/python/examples/static_function_calls_example.py +0 -0
  30. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/python/examples/use_cache_example.py +0 -0
  31. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/python/test_python.py +0 -0
  32. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/python/test_python_automagic.py +0 -0
  33. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/src/decorator_factory.rs +0 -0
  34. {dependency_cache-0.5.0 → dependency_cache-0.5.1}/src/lib.rs +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dependency_cache
3
- Version: 0.5.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
@@ -39,6 +39,7 @@ pip install dependency-cache
39
39
  | `dependency_cached` | A footgun-enabled decorator for a method where you **explicitly declare** all direct dependencies. |
40
40
  | `automagically_dependency_cached` | A decorator which **automagically infers** the direct dependencies. |
41
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. |
42
43
 
43
44
  All decorators accept optional parameters like use_cache, dependencies, track_runtime_dependencies, and serialisable. For exact signatures and types, see the
44
45
  [generated stubs file](https://github.com/XtdWt/dependency_cache/blob/master/python/dependency_cache/dependency_cache.pyi).
@@ -120,7 +121,6 @@ uv run ./python/examples/example.py
120
121
  This project is still a work in progress, with everything from API to underlying design subject to change on my whim.
121
122
 
122
123
  Current TODO list (in no particular order):
123
- - scenario analysis capability (add temporary cache to base)
124
124
  - add global_variable to link objects together
125
125
  - add validation to static dependencies, ensure methods are of decorator class (?)
126
126
  - improve plot_dependency_graph (maybe to GUI?) with interactive nodes
@@ -21,6 +21,7 @@ pip install dependency-cache
21
21
  | `dependency_cached` | A footgun-enabled decorator for a method where you **explicitly declare** all direct dependencies. |
22
22
  | `automagically_dependency_cached` | A decorator which **automagically infers** the direct dependencies. |
23
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. |
24
25
 
25
26
  All decorators accept optional parameters like use_cache, dependencies, track_runtime_dependencies, and serialisable. For exact signatures and types, see the
26
27
  [generated stubs file](https://github.com/XtdWt/dependency_cache/blob/master/python/dependency_cache/dependency_cache.pyi).
@@ -102,7 +103,6 @@ uv run ./python/examples/example.py
102
103
  This project is still a work in progress, with everything from API to underlying design subject to change on my whim.
103
104
 
104
105
  Current TODO list (in no particular order):
105
- - scenario analysis capability (add temporary cache to base)
106
106
  - add global_variable to link objects together
107
107
  - add validation to static dependencies, ensure methods are of decorator class (?)
108
108
  - improve plot_dependency_graph (maybe to GUI?) with interactive nodes
@@ -11,7 +11,7 @@ classifiers = [
11
11
  "Programming Language :: Python :: Implementation :: PyPy",
12
12
  ]
13
13
  keywords = ["caching", "dependency-graph", "invalidation"]
14
- version = "0.5.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" }
@@ -23,12 +23,12 @@ impl DependencyCacheDecorator {
23
23
 
24
24
  #[getter]
25
25
  fn __name__(&self) -> &str {
26
- &self.method_name
26
+ return &self.method_name;
27
27
  }
28
28
 
29
29
  #[getter]
30
30
  fn __qualname__(&self) -> &str {
31
- &self.method_name
31
+ return &self.method_name;
32
32
  }
33
33
 
34
34
  fn __get__(
@@ -13,13 +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)>,
22
- pub cache_stack: Vec<HashMap<CacheKey, Py<PyAny>>>,
28
+ pub cache_stack: Vec<CacheLayer>,
23
29
  }
24
30
 
25
31
  impl DependencyCacheBase {
@@ -28,8 +34,8 @@ impl DependencyCacheBase {
28
34
  if !self.method_dependency_graph.is_valid(key) {
29
35
  return ();
30
36
  }
31
- if let Some(top) = self.cache_stack.last_mut() {
32
- top.insert(key, value);
37
+ if let Some(layer) = self.cache_stack.last_mut() {
38
+ layer.cache.insert(key, value);
33
39
  } else {
34
40
  self.cache.insert(key, value);
35
41
  };
@@ -69,7 +75,7 @@ impl DependencyCacheBase {
69
75
  }
70
76
 
71
77
  pub fn current_call_stack_top(&self) -> Option<(CacheKey, bool)> {
72
- self.call_stack.last().cloned()
78
+ return self.call_stack.last().cloned();
73
79
  }
74
80
 
75
81
  pub fn push_call_stack(&mut self, key: CacheKey, add_parent_dependencies: bool) {
@@ -117,20 +123,25 @@ impl DependencyCacheBase {
117
123
  }
118
124
 
119
125
  pub fn push_cache_stack(&mut self) {
120
- return self.cache_stack.push(HashMap::new());
126
+ return self.cache_stack.push(CacheLayer {
127
+ cache: HashMap::new(),
128
+ graph_snapshot: self.method_dependency_graph.clone(),
129
+ });
121
130
  }
122
131
 
123
132
  pub fn pop_cache_stack(&mut self) -> PyResult<()> {
124
- let cache_layer = self.cache_stack.pop().ok_or_else(|| PyRuntimeError::new_err("Attempted to remove non existant layer"))?;
125
- for (key, _) in cache_layer {
126
- self.method_dependency_graph.temporarily_invalidate(key);
127
- }
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;
128
139
  return Ok(());
129
140
  }
130
141
 
131
142
  pub fn get_from_temporary_cache(&self, py: Python<'_>, key: &CacheKey) -> Option<Py<PyAny>> {
132
143
  for layer in self.cache_stack.iter().rev() {
133
- if let Some(v) = layer.get(key) {
144
+ if let Some(v) = layer.cache.get(key) {
134
145
  return Some(v.clone_ref(py));
135
146
  }
136
147
  }
@@ -144,23 +155,23 @@ impl DependencyCacheBase {
144
155
  return self.cache_stack
145
156
  .iter()
146
157
  .rev()
147
- .any(|layer| layer.contains_key(key))
158
+ .any(|layer| layer.cache.contains_key(key))
148
159
  || self.cache.contains_key(key);
149
160
  }
150
161
 
151
162
  pub fn remove_cached_value(&mut self, key: &CacheKey) -> Option<Py<PyAny>> {
152
163
  for layer in self.cache_stack.iter_mut().rev() {
153
- if let Some(v) = layer.remove(key) {
164
+ if let Some(v) = layer.cache.remove(key) {
154
165
  return Some(v);
155
166
  }
156
167
  }
157
- self.cache.remove(key)
168
+ return self.cache.remove(key);
158
169
  }
159
170
 
160
171
  pub fn effective_cache(&self) -> HashMap<&CacheKey, &Py<PyAny>> {
161
172
  let mut merged: HashMap<&CacheKey, &Py<PyAny>> = self.cache.iter().collect();
162
173
  for layer in &self.cache_stack {
163
- for (k, v) in layer {
174
+ for (k, v) in &layer.cache {
164
175
  merged.insert(k, v);
165
176
  }
166
177
  }
@@ -170,14 +181,14 @@ impl DependencyCacheBase {
170
181
  pub fn clear_effective_cache(&mut self) -> () {
171
182
  if let Some(layer) = self.cache_stack.last_mut() {
172
183
  let removed_keys: Vec<CacheKey> = {
173
- let keys = layer.keys().cloned().collect();
174
- layer.clear();
184
+ let keys = layer.cache.keys().cloned().collect();
185
+ layer.cache.clear();
175
186
  keys
176
187
  };
177
188
  for key in removed_keys {
178
189
  self.method_dependency_graph.temporarily_invalidate(key);
179
190
  }
180
- layer.clear();
191
+ layer.cache.clear();
181
192
  } else {
182
193
  self.method_dependency_graph.invalidate_all();
183
194
  self.cache.clear();
@@ -298,7 +309,7 @@ impl DependencyCacheBase {
298
309
 
299
310
  dict.set_item(key, value.clone_ref(py))?;
300
311
  }
301
- Ok(dict)
312
+ return Ok(dict);
302
313
  }
303
314
 
304
315
  #[getter]
@@ -342,7 +353,7 @@ impl DependencyCacheBase {
342
353
  let list = PySet::new(py, parent_bounds)?;
343
354
  dict.set_item(child_key, list)?;
344
355
  }
345
- Ok(dict)
356
+ return Ok(dict);
346
357
  }
347
358
 
348
359
  #[getter]
@@ -366,7 +377,7 @@ impl DependencyCacheBase {
366
377
 
367
378
  dict.set_item(key, state)?;
368
379
  }
369
- Ok(dict)
380
+ return Ok(dict);
370
381
  }
371
382
 
372
383
  #[pyo3(signature = (order=None))]
@@ -416,7 +427,7 @@ impl DependencyCacheBase {
416
427
  result.set_item(method_name, value)?;
417
428
  }
418
429
 
419
- Ok(result)
430
+ return Ok(result);
420
431
  }
421
432
 
422
433
  #[pyo3(signature = (data, order=None))]
@@ -495,6 +506,6 @@ impl DependencyCacheBase {
495
506
  load_method(&name)?;
496
507
  }
497
508
 
498
- Ok(())
509
+ return Ok(());
499
510
  }
500
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
@@ -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
 
@@ -20,13 +20,13 @@ impl Scenario {
20
20
  .cast::<DependencyCacheBase>()
21
21
  .map_err(|_| {
22
22
  PyTypeError::new_err(
23
- "The decorated method's class must inherit from DependencyCacheBase",
23
+ "custom_scenario() expects items to inherit from DependencyCacheBase",
24
24
  )
25
25
  })?;
26
26
  base.borrow_mut().push_cache_stack();
27
27
  }
28
28
  }
29
- Ok(slf.clone().into_any())
29
+ return Ok(slf.clone().into_any());
30
30
  }
31
31
 
32
32
  fn __exit__<'py>(
@@ -44,18 +44,18 @@ impl Scenario {
44
44
  .cast::<DependencyCacheBase>()
45
45
  .map_err(|_| {
46
46
  PyTypeError::new_err(
47
- "The decorated method's class must inherit from DependencyCacheBase",
47
+ "custom_scenario() expects items to inherit from DependencyCacheBase",
48
48
  )
49
49
  })?;
50
50
  base.borrow_mut().pop_cache_stack()?;
51
51
  }
52
52
  }
53
- Ok(false)
53
+ return Ok(false);
54
54
  }
55
55
  }
56
56
 
57
57
 
58
58
  #[pyfunction]
59
59
  pub fn custom_scenario(items: Vec<Py<PyAny>>) -> PyResult<Scenario> {
60
- Ok(Scenario { items })
60
+ return Ok(Scenario { items });
61
61
  }
@@ -329,7 +329,7 @@ wheels = [
329
329
 
330
330
  [[package]]
331
331
  name = "dependency-cache"
332
- version = "0.5.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'" },