dependency-cache 0.4.0__tar.gz → 0.4.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 (30) hide show
  1. {dependency_cache-0.4.0 → dependency_cache-0.4.1}/PKG-INFO +1 -1
  2. {dependency_cache-0.4.0 → dependency_cache-0.4.1}/pyproject.toml +1 -1
  3. {dependency_cache-0.4.0 → dependency_cache-0.4.1}/src/decorator.rs +6 -6
  4. {dependency_cache-0.4.0 → dependency_cache-0.4.1}/src/dependency_cache_base.rs +49 -41
  5. {dependency_cache-0.4.0 → dependency_cache-0.4.1}/src/metadata_hash_manager.rs +17 -10
  6. {dependency_cache-0.4.0 → dependency_cache-0.4.1}/src/py_introspection_utils.rs +1 -1
  7. {dependency_cache-0.4.0 → dependency_cache-0.4.1}/uv.lock +1 -1
  8. {dependency_cache-0.4.0 → dependency_cache-0.4.1}/.github/workflows/CI.yml +0 -0
  9. {dependency_cache-0.4.0 → dependency_cache-0.4.1}/.github/workflows/Test.yml +0 -0
  10. {dependency_cache-0.4.0 → dependency_cache-0.4.1}/.gitignore +0 -0
  11. {dependency_cache-0.4.0 → dependency_cache-0.4.1}/Cargo.lock +0 -0
  12. {dependency_cache-0.4.0 → dependency_cache-0.4.1}/Cargo.toml +0 -0
  13. {dependency_cache-0.4.0 → dependency_cache-0.4.1}/LICENSE +0 -0
  14. {dependency_cache-0.4.0 → dependency_cache-0.4.1}/README.md +0 -0
  15. {dependency_cache-0.4.0 → dependency_cache-0.4.1}/python/dependency_cache/__init__.py +0 -0
  16. {dependency_cache-0.4.0 → dependency_cache-0.4.1}/python/dependency_cache/dependency_cache.pyi +0 -0
  17. {dependency_cache-0.4.0 → dependency_cache-0.4.1}/python/dependency_cache/plot_dependency_graph.py +0 -0
  18. {dependency_cache-0.4.0 → dependency_cache-0.4.1}/python/examples/automagic_dependency_example.py +0 -0
  19. {dependency_cache-0.4.0 → dependency_cache-0.4.1}/python/examples/example.py +0 -0
  20. {dependency_cache-0.4.0 → dependency_cache-0.4.1}/python/examples/inheritance_example.py +0 -0
  21. {dependency_cache-0.4.0 → dependency_cache-0.4.1}/python/examples/manual_dependency_example.py +0 -0
  22. {dependency_cache-0.4.0 → dependency_cache-0.4.1}/python/examples/runtime_dependencies_example.py +0 -0
  23. {dependency_cache-0.4.0 → dependency_cache-0.4.1}/python/examples/runtime_function_calls_example.py +0 -0
  24. {dependency_cache-0.4.0 → dependency_cache-0.4.1}/python/examples/static_function_calls_example.py +0 -0
  25. {dependency_cache-0.4.0 → dependency_cache-0.4.1}/python/examples/use_cache_example.py +0 -0
  26. {dependency_cache-0.4.0 → dependency_cache-0.4.1}/python/test_python.py +0 -0
  27. {dependency_cache-0.4.0 → dependency_cache-0.4.1}/python/test_python_automagic.py +0 -0
  28. {dependency_cache-0.4.0 → dependency_cache-0.4.1}/src/decorator_factory.rs +0 -0
  29. {dependency_cache-0.4.0 → dependency_cache-0.4.1}/src/dependency_graph.rs +0 -0
  30. {dependency_cache-0.4.0 → dependency_cache-0.4.1}/src/lib.rs +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dependency_cache
3
- Version: 0.4.0
3
+ Version: 0.4.1
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.4.0"
14
+ version = "0.4.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" }
@@ -18,7 +18,7 @@ pub struct DependencyCacheDecorator {
18
18
  #[pymethods]
19
19
  impl DependencyCacheDecorator {
20
20
  fn __set__(&self, _obj: Py<PyAny>, _value: Py<PyAny>) -> PyResult<()> {
21
- return Err(PyTypeError::new_err("cannot assign to decorated method"));
21
+ return Err(PyTypeError::new_err("Cannot assign to decorated method"));
22
22
  }
23
23
 
24
24
  fn __get__(
@@ -57,18 +57,18 @@ impl DependencyCacheDecorator {
57
57
  let instance = args.get_item(0)?;
58
58
  let base = instance.cast::<DependencyCacheBase>().map_err(|_| {
59
59
  PyTypeError::new_err(
60
- "the decorated method's class must inherit from DependencyCacheBase",
60
+ "The decorated method's class must inherit from DependencyCacheBase",
61
61
  )
62
62
  })?;
63
63
 
64
64
  let func = self.func.bind(py);
65
65
  let (hash, metadata) = normalise_function_signature_and_hash(py, &self.method_name, Some(func), args, kwargs)?;
66
66
 
67
- let key = base.borrow_mut().metadata_hash_manager.create_cache_key(py, hash, metadata.unbind())?;
67
+ let key = base.try_borrow_mut()?.metadata_hash_manager.create_cache_key(py, hash, metadata.unbind())?;
68
68
 
69
69
  // 1. add/check child dependencies
70
70
  let mut dependency_keys = Vec::with_capacity(self.dependencies.len());
71
- if self.dependencies.len() != 0 {
71
+ if !self.dependencies.is_empty() {
72
72
  for (hash, metadata) in &self.dependencies {
73
73
  let dependency_key = base.borrow_mut()
74
74
  .metadata_hash_manager
@@ -90,7 +90,7 @@ impl DependencyCacheDecorator {
90
90
 
91
91
  let outcome = (|| -> PyResult<Py<PyAny>> {
92
92
  // 3. check cache for value
93
- let cached = base.borrow().get_cached_value_by_hash(py, key);
93
+ let cached = base.borrow().get_cached_value_by_key(py, key);
94
94
  if let Some(cached) = cached {
95
95
  return Ok(cached);
96
96
  }
@@ -102,7 +102,7 @@ impl DependencyCacheDecorator {
102
102
  base.borrow_mut()
103
103
  .validate_current_method(key, self.use_cache);
104
104
  base.borrow_mut()
105
- .set_cached_value_by_hash(key, result.clone_ref(py));
105
+ .set_cached_value_by_key(key, result.clone_ref(py));
106
106
  Ok(result)
107
107
  })();
108
108
 
@@ -23,19 +23,19 @@ pub struct DependencyCacheBase {
23
23
 
24
24
  impl DependencyCacheBase {
25
25
 
26
- pub fn set_cached_value_by_hash(&mut self, hash: CacheKey, value: Py<PyAny>) {
27
- if !self.method_dependency_graph.is_valid(hash) {
26
+ pub fn set_cached_value_by_key(&mut self, key: CacheKey, value: Py<PyAny>) {
27
+ if !self.method_dependency_graph.is_valid(key) {
28
28
  return ();
29
29
  }
30
- self.cache.insert(hash, value);
30
+ self.cache.insert(key, value);
31
31
  }
32
32
 
33
- pub fn validate_current_method(&mut self, hash: CacheKey, use_cache: bool) {
33
+ pub fn validate_current_method(&mut self, key: CacheKey, use_cache: bool) {
34
34
  if !use_cache {
35
- self.method_dependency_graph.permanently_invalidate(hash);
35
+ self.method_dependency_graph.permanently_invalidate(key);
36
36
  }
37
37
 
38
- let child_validation_states: Vec<CacheKey> = self.method_dependency_graph.list_child_methods(&hash);
38
+ let child_validation_states: Vec<CacheKey> = self.method_dependency_graph.list_child_methods(&key);
39
39
 
40
40
  let mut new_state = ValidationState::Valid;
41
41
 
@@ -52,13 +52,13 @@ impl DependencyCacheBase {
52
52
 
53
53
  match new_state {
54
54
  ValidationState::PermanentlyInvalid => {
55
- self.method_dependency_graph.permanently_invalidate(hash);
55
+ self.method_dependency_graph.permanently_invalidate(key);
56
56
  }
57
57
  ValidationState::Invalid => {
58
- self.method_dependency_graph.temporarily_invalidate(hash);
58
+ self.method_dependency_graph.temporarily_invalidate(key);
59
59
  }
60
60
  ValidationState::Valid => {
61
- self.method_dependency_graph.validate(hash);
61
+ self.method_dependency_graph.validate(key);
62
62
  }
63
63
  }
64
64
  }
@@ -67,38 +67,46 @@ impl DependencyCacheBase {
67
67
  self.call_stack.last().cloned()
68
68
  }
69
69
 
70
- pub fn push_call_stack(&mut self, hash: CacheKey, add_parent_dependencies: bool) {
71
- self.call_stack.push((hash, add_parent_dependencies));
70
+ pub fn push_call_stack(&mut self, key: CacheKey, add_parent_dependencies: bool) {
71
+ self.call_stack.push((key, add_parent_dependencies));
72
72
  }
73
73
 
74
74
  pub fn pop_call_stack(&mut self) {
75
75
  self.call_stack.pop();
76
76
  }
77
77
 
78
- pub fn add_parent_dependency(&mut self, hash: CacheKey, dependency: CacheKey) {
78
+ pub fn add_parent_dependency(&mut self, key: CacheKey, dependency: CacheKey) {
79
79
  self.method_dependency_graph
80
- .add_parent_dependency(hash, vec![dependency]);
80
+ .add_parent_dependency(key, vec![dependency]);
81
81
  }
82
82
 
83
- pub fn add_children_dependencies(&mut self, hash: CacheKey, dependency: Vec<CacheKey>) {
83
+ pub fn add_children_dependencies(&mut self, key: CacheKey, dependency: Vec<CacheKey>) {
84
84
  self.method_dependency_graph
85
- .add_children_dependency(hash, dependency);
85
+ .add_children_dependency(key, dependency);
86
86
  }
87
87
 
88
- pub fn get_cached_value_by_hash(&self, py: Python<'_>, hash: CacheKey) -> Option<Py<PyAny>> {
89
- if self.method_dependency_graph.is_valid(hash) {
90
- return self.cache.get(&hash).map(|obj| obj.clone_ref(py));
88
+ pub fn get_cached_value_by_key(&self, py: Python<'_>, key: CacheKey) -> Option<Py<PyAny>> {
89
+ if self.method_dependency_graph.is_valid(key) {
90
+ return self.cache.get(&key).map(|obj| obj.clone_ref(py));
91
91
  }
92
92
  return None;
93
93
  }
94
94
 
95
- pub fn create_hash(&mut self, py: Python<'_>, method_name: &String, kwargs: &Option<Py<PyDict>>) -> PyResult<CacheKey> {
95
+ pub fn create_key(&mut self, py: Python<'_>, method_name: &str, kwargs: &Option<Py<PyDict>>) -> PyResult<CacheKey> {
96
96
  let empty_args = PyTuple::empty(py);
97
97
  let kwargs_bound = kwargs.as_ref().map(|k| k.bind(py));
98
- let (hash, sig) = normalise_function_signature_and_hash(py, &method_name, None, &empty_args, kwargs_bound)?;
98
+ let (hash, sig) = normalise_function_signature_and_hash(py, method_name, None, &empty_args, kwargs_bound)?;
99
99
  let key = self.metadata_hash_manager.create_cache_key(py, hash, sig.unbind());
100
100
  return key;
101
101
  }
102
+
103
+ pub fn get_key(&mut self, py: Python<'_>, method_name: &str, kwargs: &Option<Py<PyDict>>) -> PyResult<CacheKey> {
104
+ let empty_args = PyTuple::empty(py);
105
+ let kwargs_bound = kwargs.as_ref().map(|k| k.bind(py));
106
+ let (hash, sig) = normalise_function_signature_and_hash(py, method_name, None, &empty_args, kwargs_bound)?;
107
+ let key = self.metadata_hash_manager.get_cache_key(py, &hash, &sig);
108
+ return key;
109
+ }
102
110
  }
103
111
 
104
112
  #[pymethods]
@@ -116,10 +124,10 @@ impl DependencyCacheBase {
116
124
 
117
125
  #[pyo3(signature = (method_name, **kwargs))]
118
126
  pub fn get_cached_value(&mut self, py: Python<'_>, method_name: String, kwargs: Option<Py<PyDict>>) -> PyResult<Option<Py<PyAny>>> {
119
- let hash = self.create_hash(py, &method_name, &kwargs)?;
127
+ let key = self.get_key(py, &method_name, &kwargs)?;
120
128
 
121
- if self.method_dependency_graph.is_valid(hash) {
122
- return Ok(self.cache.get(&hash).map(|obj| obj.clone_ref(py)));
129
+ if self.method_dependency_graph.is_valid(key) {
130
+ return Ok(self.cache.get(&key).map(|obj| obj.clone_ref(py)));
123
131
  }
124
132
  return Ok(None);
125
133
  }
@@ -130,23 +138,23 @@ impl DependencyCacheBase {
130
138
  method_name: String,
131
139
  kwargs: Option<Py<PyDict>>,
132
140
  ) -> PyResult<bool> {
133
- let hash = self.create_hash(py, &method_name, &kwargs)?;
134
- Ok(self.method_dependency_graph.is_valid(hash) && self.cache.contains_key(&hash))
141
+ let key = self.get_key(py, &method_name, &kwargs)?;
142
+ Ok(self.method_dependency_graph.is_valid(key) && self.cache.contains_key(&key))
135
143
  }
136
144
 
137
145
 
138
146
  #[pyo3(signature = (method_name, value, **kwargs))]
139
147
  pub fn update_cached_value(&mut self, py: Python<'_>, method_name: String, value: Py<PyAny>, kwargs: Option<Py<PyDict>>) -> PyResult<()> {
140
- let hash = self.create_hash(py, &method_name, &kwargs)?;
148
+ let key = self.get_key(py, &method_name, &kwargs)?;
141
149
 
142
- let Some(v) = self.cache.get_mut(&hash) else {
150
+ let Some(v) = self.cache.get_mut(&key) else {
143
151
  return Err(PyKeyError::new_err(format!(
144
152
  "Cannot update cached value for '{method_name}'"
145
153
  )));
146
154
  };
147
155
  *v = value;
148
- self.method_dependency_graph.temporarily_invalidate(hash);
149
- self.method_dependency_graph.validate(hash);
156
+ self.method_dependency_graph.temporarily_invalidate(key);
157
+ self.method_dependency_graph.validate(key);
150
158
  return Ok(());
151
159
  }
152
160
 
@@ -157,22 +165,22 @@ impl DependencyCacheBase {
157
165
 
158
166
  #[pyo3(signature = (method_name, **kwargs))]
159
167
  pub fn clear_cached_value(&mut self, py: Python<'_>, method_name: String, kwargs: Option<Py<PyDict>>) -> PyResult<()> {
160
- let hash = self.create_hash(py, &method_name, &kwargs)?;
168
+ let key = self.get_key(py, &method_name, &kwargs)?;
161
169
 
162
- if self.cache.remove(&hash).is_none() {
170
+ if self.cache.remove(&key).is_none() {
163
171
  return Err(PyKeyError::new_err(format!(
164
172
  "Cannot clear cached value for '{method_name}'"
165
173
  )));
166
174
  }
167
- self.method_dependency_graph.temporarily_invalidate(hash);
175
+ self.method_dependency_graph.temporarily_invalidate(key);
168
176
  Ok(())
169
177
  }
170
178
 
171
179
  #[getter]
172
180
  pub fn get_cached_values<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyDict>> {
173
181
  let dict = PyDict::new(py);
174
- for (hash, value) in &self.cache {
175
- let Some(bound) = self.metadata_hash_manager.get_signature(py, hash) else {
182
+ for (key, value) in &self.cache {
183
+ let Some(bound) = self.metadata_hash_manager.get_signature(py, key) else {
176
184
  continue;
177
185
  };
178
186
 
@@ -196,8 +204,8 @@ impl DependencyCacheBase {
196
204
  pub fn get_dependency_graph<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyDict>> {
197
205
  let dict = PyDict::new(py);
198
206
 
199
- for (child_hash, parent_hashes) in &self.method_dependency_graph.clone_graph() {
200
- let Some(child_bound) = self.metadata_hash_manager.get_signature(py, child_hash) else {
207
+ for (child_key, parent_keys) in &self.method_dependency_graph.clone_graph() {
208
+ let Some(child_bound) = self.metadata_hash_manager.get_signature(py, child_key) else {
201
209
  continue;
202
210
  };
203
211
 
@@ -213,8 +221,8 @@ impl DependencyCacheBase {
213
221
  };
214
222
 
215
223
  let mut parent_bounds = Vec::new();
216
- for parent_hash in parent_hashes {
217
- if let Some(bound) = self.metadata_hash_manager.get_signature(py, parent_hash) {
224
+ for parent_key in parent_keys {
225
+ if let Some(bound) = self.metadata_hash_manager.get_signature(py, parent_key) {
218
226
 
219
227
  let parent_name: String = bound.get_item(0)?.extract()?;
220
228
  let parent_args_item = bound.get_item(1)?;
@@ -239,8 +247,8 @@ impl DependencyCacheBase {
239
247
  #[getter]
240
248
  pub fn get_validation_state<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyDict>> {
241
249
  let dict = PyDict::new(py);
242
- for (hash, state) in &self.method_dependency_graph.clone_state() {
243
- let Some(meta_bound) = self.metadata_hash_manager.get_signature(py, hash) else {
250
+ for (key, state) in &self.method_dependency_graph.clone_state() {
251
+ let Some(meta_bound) = self.metadata_hash_manager.get_signature(py, key) else {
244
252
  continue;
245
253
  };
246
254
 
@@ -357,7 +365,7 @@ impl DependencyCacheBase {
357
365
  let _ = slf.call_method(name, (), None)?;
358
366
 
359
367
  let mut base = slf.borrow_mut();
360
- let key = base.create_hash(py, &name.to_string(), &None)?;
368
+ let key = base.create_key(py, name, &None)?;
361
369
  base.cache.insert(key, loaded_value.unbind());
362
370
  Ok(())
363
371
  };
@@ -1,5 +1,7 @@
1
1
  use pyo3::prelude::*;
2
2
  use pyo3::types::PyTuple;
3
+ use pyo3::exceptions::PyKeyError;
4
+
3
5
  use std::collections::HashMap;
4
6
 
5
7
  pub type CacheKey = u64;
@@ -15,7 +17,7 @@ impl MetadataHashManager {
15
17
  Self { buckets: HashMap::new(), signatures: HashMap::new(), next_id: 0 }
16
18
  }
17
19
 
18
- pub fn get_cache_key(
20
+ fn find_cache_key(
19
21
  &self,
20
22
  py: Python<'_>,
21
23
  hash: &isize,
@@ -24,32 +26,37 @@ impl MetadataHashManager {
24
26
  let Some(bucket) = self.buckets.get(hash) else {
25
27
  return Ok(None);
26
28
  };
27
- if bucket.len() == 1 {
28
- let key = bucket[0];
29
- let stored = self.signatures[&key].bind(py);
30
- return Ok(stored.eq(sig)?.then_some(key));
31
- }
32
29
  for &key in bucket {
33
- let stored = self.signatures[&key].bind(py);
34
- if stored.eq(sig)? {
30
+ let stored_signature = self.signatures[&key].bind(py);
31
+ if stored_signature.eq(sig)? {
35
32
  return Ok(Some(key));
36
33
  }
37
34
  }
38
35
  Ok(None)
39
36
  }
40
37
 
38
+ pub fn get_cache_key(
39
+ &self,
40
+ py: Python<'_>,
41
+ hash: &isize,
42
+ sig: &Bound<'_, PyTuple>,
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"))
46
+ }
47
+
41
48
  pub fn create_cache_key(
42
49
  &mut self,
43
50
  py: Python<'_>,
44
51
  hash: isize,
45
52
  sig: Py<PyTuple>,
46
53
  ) -> PyResult<CacheKey> {
47
- if let Some(key) = self.get_cache_key(py, &hash, sig.bind(py))? {
54
+ if let Some(key) = self.find_cache_key(py, &hash, sig.bind(py))? {
48
55
  return Ok(key);
49
56
  }
50
57
  let key = self.next_id;
51
58
  self.next_id += 1;
52
- self.buckets.entry(hash.clone()).or_default().push(key);
59
+ self.buckets.entry(hash).or_default().push(key);
53
60
  self.signatures.insert(key, sig);
54
61
  Ok(key)
55
62
  }
@@ -182,7 +182,7 @@ pub fn ast_inspect_method_dependencies(py: Python<'_>, python_func: &Py<PyAny>)
182
182
  let ast_module = PyModule::import(py, "ast")?;
183
183
  let ast_tree = ast_module
184
184
  .call_method1("parse", (function_string_clean,))
185
- .map_err(|e| PyValueError::new_err(format!("failed to parse source: {e}")))?;
185
+ .map_err(|e| PyValueError::new_err(format!("Failed to parse source: {e}")))?;
186
186
 
187
187
  let ast_function = ast_module.getattr("FunctionDef")?;
188
188
  let ast_async_function = ast_module.getattr("AsyncFunctionDef")?;
@@ -329,7 +329,7 @@ wheels = [
329
329
 
330
330
  [[package]]
331
331
  name = "dependency-cache"
332
- version = "0.4.0"
332
+ version = "0.4.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'" },