dependency-cache 0.1.1__tar.gz → 0.1.3__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 (28) hide show
  1. {dependency_cache-0.1.1 → dependency_cache-0.1.3}/Cargo.lock +62 -0
  2. {dependency_cache-0.1.1 → dependency_cache-0.1.3}/Cargo.toml +3 -0
  3. {dependency_cache-0.1.1 → dependency_cache-0.1.3}/PKG-INFO +3 -4
  4. {dependency_cache-0.1.1 → dependency_cache-0.1.3}/README.md +1 -2
  5. {dependency_cache-0.1.1 → dependency_cache-0.1.3}/pyproject.toml +2 -2
  6. {dependency_cache-0.1.1 → dependency_cache-0.1.3}/src/decorator.rs +2 -2
  7. {dependency_cache-0.1.1 → dependency_cache-0.1.3}/src/decorator_factory.rs +4 -54
  8. {dependency_cache-0.1.1 → dependency_cache-0.1.3}/src/dependency_cache_base.rs +15 -6
  9. {dependency_cache-0.1.1 → dependency_cache-0.1.3}/src/lib.rs +1 -1
  10. dependency_cache-0.1.3/src/py_introspection_utils.rs +113 -0
  11. {dependency_cache-0.1.1 → dependency_cache-0.1.3}/uv.lock +1 -1
  12. dependency_cache-0.1.1/src/normalise_method_args.rs +0 -62
  13. {dependency_cache-0.1.1 → dependency_cache-0.1.3}/.github/workflows/CI.yml +0 -0
  14. {dependency_cache-0.1.1 → dependency_cache-0.1.3}/.github/workflows/Test.yml +0 -0
  15. {dependency_cache-0.1.1 → dependency_cache-0.1.3}/.gitignore +0 -0
  16. {dependency_cache-0.1.1 → dependency_cache-0.1.3}/LICENSE +0 -0
  17. {dependency_cache-0.1.1 → dependency_cache-0.1.3}/python/dependency_cache/__init__.py +0 -0
  18. {dependency_cache-0.1.1 → dependency_cache-0.1.3}/python/dependency_cache/dependency_cache.pyi +0 -0
  19. {dependency_cache-0.1.1 → dependency_cache-0.1.3}/python/dependency_cache/plot_dependency_graph.py +0 -0
  20. {dependency_cache-0.1.1 → dependency_cache-0.1.3}/python/examples/automagic_dependency_example.py +0 -0
  21. {dependency_cache-0.1.1 → dependency_cache-0.1.3}/python/examples/example.py +0 -0
  22. {dependency_cache-0.1.1 → dependency_cache-0.1.3}/python/examples/function_calls_example.py +0 -0
  23. {dependency_cache-0.1.1 → dependency_cache-0.1.3}/python/examples/inheritance_example.py +0 -0
  24. {dependency_cache-0.1.1 → dependency_cache-0.1.3}/python/examples/manual_dependency_example.py +0 -0
  25. {dependency_cache-0.1.1 → dependency_cache-0.1.3}/python/examples/use_cache_example.py +0 -0
  26. {dependency_cache-0.1.1 → dependency_cache-0.1.3}/python/test_python.py +0 -0
  27. {dependency_cache-0.1.1 → dependency_cache-0.1.3}/python/test_python_automagic.py +0 -0
  28. {dependency_cache-0.1.1 → dependency_cache-0.1.3}/src/dependency_graph.rs +0 -0
@@ -2,11 +2,50 @@
2
2
  # It is not intended for manual editing.
3
3
  version = 4
4
4
 
5
+ [[package]]
6
+ name = "cfg-if"
7
+ version = "1.0.4"
8
+ source = "registry+https://github.com/rust-lang/crates.io-index"
9
+ checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
10
+
11
+ [[package]]
12
+ name = "chacha20"
13
+ version = "0.10.2"
14
+ source = "registry+https://github.com/rust-lang/crates.io-index"
15
+ checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06"
16
+ dependencies = [
17
+ "cfg-if",
18
+ "cpufeatures",
19
+ "rand_core",
20
+ ]
21
+
22
+ [[package]]
23
+ name = "cpufeatures"
24
+ version = "0.3.1"
25
+ source = "registry+https://github.com/rust-lang/crates.io-index"
26
+ checksum = "5ca28b0ae3115b884660db4118d803791fd6756b6e88f39c0f3f7859060d7566"
27
+ dependencies = [
28
+ "libc",
29
+ ]
30
+
5
31
  [[package]]
6
32
  name = "dependency_cache"
7
33
  version = "0.1.0"
8
34
  dependencies = [
9
35
  "pyo3",
36
+ "rand",
37
+ ]
38
+
39
+ [[package]]
40
+ name = "getrandom"
41
+ version = "0.4.3"
42
+ source = "registry+https://github.com/rust-lang/crates.io-index"
43
+ checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099"
44
+ dependencies = [
45
+ "cfg-if",
46
+ "libc",
47
+ "r-efi",
48
+ "rand_core",
10
49
  ]
11
50
 
12
51
  [[package]]
@@ -108,6 +147,29 @@ dependencies = [
108
147
  "proc-macro2",
109
148
  ]
110
149
 
150
+ [[package]]
151
+ name = "r-efi"
152
+ version = "6.0.0"
153
+ source = "registry+https://github.com/rust-lang/crates.io-index"
154
+ checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf"
155
+
156
+ [[package]]
157
+ name = "rand"
158
+ version = "0.10.2"
159
+ source = "registry+https://github.com/rust-lang/crates.io-index"
160
+ checksum = "c7f5fa3a058cd35567ef9bfa5e75732bee0f9e4c55fa90477bef2dfcdbc4be80"
161
+ dependencies = [
162
+ "chacha20",
163
+ "getrandom",
164
+ "rand_core",
165
+ ]
166
+
167
+ [[package]]
168
+ name = "rand_core"
169
+ version = "0.10.1"
170
+ source = "registry+https://github.com/rust-lang/crates.io-index"
171
+ checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69"
172
+
111
173
  [[package]]
112
174
  name = "syn"
113
175
  version = "2.0.119"
@@ -9,6 +9,9 @@ readme = "README.md"
9
9
  name = "dependency_cache"
10
10
  crate-type = ["cdylib"]
11
11
 
12
+ [dependencies]
13
+ rand = "0.10.2"
14
+
12
15
  [dependencies.pyo3]
13
16
  version = "0.29.*"
14
17
  features = [
@@ -1,13 +1,13 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dependency_cache
3
- Version: 0.1.1
3
+ Version: 0.1.3
4
4
  Classifier: Programming Language :: Rust
5
5
  Classifier: Programming Language :: Python :: Implementation :: CPython
6
6
  Classifier: Programming Language :: Python :: Implementation :: PyPy
7
7
  Requires-Dist: matplotlib>=3.7.5
8
8
  Requires-Dist: networkx>=3.1
9
9
  License-File: LICENSE
10
- Summary: A caching system for object methods that utilises a dependency graph to track relationships and trigger invalidation
10
+ Summary: A caching library for Python object methods that utilises a dependency graph to track relationships and control invalidation, enabling efficient recomputation
11
11
  Keywords: caching,dependency-graph,invalidation
12
12
  License: MIT
13
13
  Requires-Python: >=3.8
@@ -118,8 +118,7 @@ uv run ./python/examples/example.py
118
118
  This project is still a work in progress, with everything from API to underlying design subject to change on my whim.
119
119
 
120
120
  Current TODO list (in no particular order):
121
- - load and dump cache methods for base class
122
- - add back in ast parsing
121
+ - add back in ast parsing, to decorator factory
123
122
  - move plot_dependency_graph to rust side instead of python
124
123
  - add object thread safety for python 3.14+
125
124
  - work out how best to handle nested objects
@@ -100,8 +100,7 @@ uv run ./python/examples/example.py
100
100
  This project is still a work in progress, with everything from API to underlying design subject to change on my whim.
101
101
 
102
102
  Current TODO list (in no particular order):
103
- - load and dump cache methods for base class
104
- - add back in ast parsing
103
+ - add back in ast parsing, to decorator factory
105
104
  - move plot_dependency_graph to rust side instead of python
106
105
  - add object thread safety for python 3.14+
107
106
  - work out how best to handle nested objects
@@ -11,8 +11,8 @@ classifiers = [
11
11
  "Programming Language :: Python :: Implementation :: PyPy",
12
12
  ]
13
13
  keywords = ["caching", "dependency-graph", "invalidation"]
14
- version = "0.1.1"
15
- description = "A caching system for object methods that utilises a dependency graph to track relationships and trigger invalidation"
14
+ version = "0.1.3"
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
18
  dependencies = [
@@ -3,7 +3,7 @@ use pyo3::prelude::*;
3
3
  use pyo3::types::{PyDict, PyTuple};
4
4
 
5
5
  use crate::dependency_cache_base::DependencyCacheBase;
6
- use crate::normalise_method_args::normalise_and_hash_method;
6
+ use crate::py_introspection_utils::normalise_function_signature_and_hash;
7
7
 
8
8
  #[pyclass(frozen)]
9
9
  pub struct DependencyCacheDecorator {
@@ -62,7 +62,7 @@ impl DependencyCacheDecorator {
62
62
  })?;
63
63
 
64
64
  let func = self.func.bind(py);
65
- let (hash, metadata) = normalise_and_hash_method(py, &self.method_name, Some(func), args, kwargs)?;
65
+ let (hash, metadata) = normalise_function_signature_and_hash(py, &self.method_name, Some(func), args, kwargs)?;
66
66
 
67
67
  // 1. add/check child dependencies
68
68
  base.borrow_mut().add_children_dependencies(hash, self.dependencies.clone(), metadata.unbind());
@@ -1,60 +1,10 @@
1
1
  use pyo3::prelude::*;
2
- use pyo3::types::{PyDict, PyList, PyTuple};
3
- use pyo3::exceptions::PyValueError;
2
+ use pyo3::types::{PyDict, PyTuple};
4
3
 
5
4
  use crate::decorator::DependencyCacheDecorator;
6
- use crate::normalise_method_args::normalise_and_hash_method;
5
+ use crate::py_introspection_utils::{normalise_function_signature_and_hash, parse_dependencies, validate_self_only_method};
7
6
 
8
7
 
9
- pub fn validate_self_only_method(py: Python<'_>, func: &Bound<'_, PyAny>) -> PyResult<()> {
10
- let inspect = py.import("inspect")?;
11
- let builtins = py.import("builtins")?;
12
- let signature = inspect.call_method1("signature", (func,))?;
13
- // really ugly, but the inspect.Signature type is not extractable
14
- let parameters = signature.getattr("parameters")?;
15
- let list_obj = builtins.call_method1("list", (parameters,))?;
16
- let param_names: Vec<String> = list_obj.extract()?;
17
-
18
- if param_names.len() != 1 || param_names[0] != "self" {
19
- let msg = format!(
20
- "Serialisable method must have exactly one parameter named 'self', got: {:?}",
21
- param_names
22
- );
23
- return Err(PyValueError::new_err(msg));
24
- }
25
- Ok(())
26
- }
27
-
28
-
29
- fn parse_dependencies(py: Python<'_>, dep_list: &Bound<'_, PyAny>) -> PyResult<Vec<(String, Py<PyDict>)>> {
30
- let list = dep_list.cast::<PyList>()?;
31
- let mut parsed = Vec::with_capacity(list.len());
32
-
33
- for item in list.iter() {
34
- if let Ok(tuple) = item.cast::<PyTuple>() {
35
- if tuple.len() == 2 {
36
- let method_name: String = tuple.get_item(0)?.extract()?;
37
- let kwargs = tuple.get_item(1)?;
38
- let kwargs_dict = kwargs.cast::<PyDict>()?;
39
- parsed.push((method_name, kwargs_dict.clone().unbind()));
40
- continue;
41
- }
42
- }
43
-
44
- if let Ok(method_name) = item.extract::<String>() {
45
- let empty_dict = PyDict::new(py);
46
- parsed.push((method_name, empty_dict.unbind()));
47
- continue;
48
- }
49
-
50
- return Err(PyValueError::new_err(
51
- "Dependency must be a tuple (str, dict) or a str",
52
- ));
53
- }
54
-
55
- return Ok(parsed);
56
- }
57
-
58
8
  #[pyclass(name = "dependency_cached", frozen)]
59
9
  pub struct ManualDependencyCacheDecoratorFactory {
60
10
  use_cache: bool,
@@ -95,7 +45,7 @@ impl ManualDependencyCacheDecoratorFactory {
95
45
  .dependencies
96
46
  .iter()
97
47
  .map(|(method_name, kwargs)| {
98
- let (hash, _) = normalise_and_hash_method(
48
+ let (hash, _) = normalise_function_signature_and_hash(
99
49
  py,
100
50
  method_name,
101
51
  None,
@@ -158,7 +108,7 @@ impl AutomagicDependencyCacheDecoratorFactory {
158
108
  .dependencies
159
109
  .iter()
160
110
  .map(|(method_name, kwargs)| {
161
- let (hash, _) = normalise_and_hash_method(
111
+ let (hash, _) = normalise_function_signature_and_hash(
162
112
  py,
163
113
  method_name,
164
114
  None,
@@ -2,12 +2,16 @@ use pyo3::prelude::*;
2
2
  use pyo3::types::{PyDict, PyTuple, PyString, PySet, PyType};
3
3
  use pyo3::exceptions::PyValueError;
4
4
 
5
+ use rand::rng;
6
+ use rand::seq::SliceRandom;
7
+
5
8
  use std::collections::{HashMap, HashSet};
6
9
 
7
10
  use crate::dependency_graph::{MethodDependencyGraph, ValidationState};
8
- use crate::normalise_method_args::normalise_and_hash_method;
11
+ use crate::py_introspection_utils::normalise_function_signature_and_hash;
9
12
  use crate::decorator::DependencyCacheDecorator;
10
13
 
14
+
11
15
  #[pyclass(subclass)]
12
16
  pub struct DependencyCacheBase {
13
17
  pub cache: HashMap<isize, Py<PyAny>>,
@@ -141,6 +145,7 @@ impl DependencyCacheBase {
141
145
  // slf.borrow_mut().method_dependency_graph = graph;
142
146
  // return Ok(());
143
147
  // }
148
+
144
149
  pub fn get_cached_value_by_hash(&self, py: Python<'_>, hash: isize) -> Option<Py<PyAny>> {
145
150
  if self.method_dependency_graph.is_valid(hash) {
146
151
  return self.cache.get(&hash).map(|obj| obj.clone_ref(py));
@@ -152,7 +157,7 @@ impl DependencyCacheBase {
152
157
  pub fn get_cached_value(&self, py: Python<'_>, method_name: String, kwargs: Option<Py<PyDict>>) -> Option<Py<PyAny>> {
153
158
  let empty_args = PyTuple::empty(py);
154
159
  let kwargs_bound = kwargs.as_ref().map(|k| k.bind(py));
155
- let (hash, _) = normalise_and_hash_method(py, &method_name, None, &empty_args, kwargs_bound).ok()?;
160
+ let (hash, _) = normalise_function_signature_and_hash(py, &method_name, None, &empty_args, kwargs_bound).ok()?;
156
161
 
157
162
  if self.method_dependency_graph.is_valid(hash) {
158
163
  self.cache.get(&hash).map(|obj| obj.clone_ref(py))
@@ -165,7 +170,7 @@ impl DependencyCacheBase {
165
170
  pub fn update_cached_value(&mut self, py: Python<'_>, method_name: String, value: Py<PyAny>, kwargs: Option<Py<PyDict>>) {
166
171
  let empty_args = PyTuple::empty(py);
167
172
  let kwargs_bound = kwargs.as_ref().map(|k| k.bind(py));
168
- let Ok((hash, _)) = normalise_and_hash_method(py, &method_name, None, &empty_args, kwargs_bound) else {
173
+ let Ok((hash, _)) = normalise_function_signature_and_hash(py, &method_name, None, &empty_args, kwargs_bound) else {
169
174
  return;
170
175
  };
171
176
 
@@ -280,8 +285,10 @@ impl DependencyCacheBase {
280
285
  for mro_class in cls.mro().iter() {
281
286
  let mro_class: Bound<'_, PyType> = mro_class.extract()?;
282
287
  let namespace = mro_class.getattr("__dict__")?;
283
- for item in namespace.call_method0("items")?.try_iter()? {
284
- let (name, value): (String, Bound<'_, PyAny>) = item?.extract()?;
288
+
289
+ let method_names = namespace.call_method0("items")?.try_iter()?;
290
+ for method_name in method_names {
291
+ let (name, value): (String, Bound<'_, PyAny>) = method_name?.extract()?;
285
292
  if name.starts_with("__") || !visited.insert(name.clone()) {
286
293
  continue;
287
294
  }
@@ -294,6 +301,8 @@ impl DependencyCacheBase {
294
301
  }
295
302
 
296
303
  let result = PyDict::new(py);
304
+ // change methods to random order, since we do know what order is best
305
+ serialisable_methods.shuffle(&mut rng());
297
306
  for method_name in serialisable_methods {
298
307
  let value = slf.call_method(&method_name, (), None)?;
299
308
  result.set_item(method_name, value)?;
@@ -313,7 +322,7 @@ impl DependencyCacheBase {
313
322
  let _ = slf.call_method(&method_name, (), None)?;
314
323
 
315
324
  let empty_args = PyTuple::empty(py);
316
- let (hash, _) = normalise_and_hash_method(py, &method_name, None, &empty_args, None)
325
+ let (hash, _) = normalise_function_signature_and_hash(py, &method_name, None, &empty_args, None)
317
326
  .map_err(|e| PyValueError::new_err(format!("Failed to hash '{}': {}", method_name, e)))?;
318
327
 
319
328
  slf.borrow_mut().cache.insert(hash, loaded_value.unbind());
@@ -2,7 +2,7 @@ mod decorator;
2
2
  mod dependency_graph;
3
3
  mod dependency_cache_base;
4
4
  mod decorator_factory;
5
- mod normalise_method_args;
5
+ mod py_introspection_utils;
6
6
 
7
7
  use pyo3::prelude::*;
8
8
 
@@ -0,0 +1,113 @@
1
+ use pyo3::prelude::*;
2
+ use pyo3::exceptions::PyValueError;
3
+ use pyo3::types::{PyDict, PyString, PyTuple, PyList};
4
+
5
+
6
+ pub fn normalise_function_signature_and_hash<'py>(
7
+ py: Python<'py>,
8
+ func_name: &str,
9
+ func: Option<&Bound<'py, PyAny>>,
10
+ args: &Bound<'py, PyTuple>,
11
+ kwargs: Option<&Bound<'py, PyDict>>,
12
+ ) -> PyResult<(isize, Bound<'py, PyTuple>)> {
13
+ let dict = match func {
14
+ Some(f) => {
15
+ let inspect = py.import("inspect")?;
16
+ let bound = inspect
17
+ .getattr("signature")?
18
+ .call1((f,))?
19
+ .getattr("bind")?
20
+ .call(args, kwargs)?;
21
+ bound.getattr("apply_defaults")?.call0()?;
22
+ bound.getattr("arguments")?
23
+ }
24
+ None => match kwargs {
25
+ Some(d) => d.clone().into_any(),
26
+ None => PyDict::new(py).into_any(),
27
+ },
28
+ };
29
+
30
+ let dict = dict.cast::<PyDict>()?;
31
+
32
+ let mut items: Vec<(String, Bound<'py, PyAny>)> = Vec::new();
33
+ for (k, v) in dict.iter() {
34
+ let key: String = k.extract()?;
35
+ if key == "self" {
36
+ continue;
37
+ }
38
+ items.push((key, v));
39
+ }
40
+ items.sort_by(|a, b| a.0.cmp(&b.0));
41
+
42
+
43
+ let pairs = items
44
+ .into_iter()
45
+ .map(|(k, v)| {
46
+ let k_py = PyString::new(py, &k).into_any();
47
+ PyTuple::new(py, vec![k_py, v])
48
+ })
49
+ .collect::<PyResult<Vec<_>>>()?;
50
+
51
+ let normalised = PyTuple::new(py, pairs)?;
52
+
53
+ let combined = PyTuple::new(
54
+ py,
55
+ vec![
56
+ PyString::new(py, func_name).into_any(),
57
+ normalised.clone().into_any(),
58
+ ],
59
+ )?;
60
+ let hash_val = combined.hash()?;
61
+
62
+ return Ok((hash_val, combined));
63
+ }
64
+
65
+
66
+ pub fn validate_self_only_method(py: Python<'_>, func: &Bound<'_, PyAny>) -> PyResult<()> {
67
+ let inspect = py.import("inspect")?;
68
+ let builtins = py.import("builtins")?;
69
+ let signature = inspect.call_method1("signature", (func,))?;
70
+ // really ugly, but the inspect.Signature type is not extractable
71
+ let parameters = signature.getattr("parameters")?;
72
+ let list_obj = builtins.call_method1("list", (parameters,))?;
73
+ let param_names: Vec<String> = list_obj.extract()?;
74
+
75
+ if param_names.len() != 1 || param_names[0] != "self" {
76
+ let msg = format!(
77
+ "Serialisable method must have exactly one parameter named 'self', got: {:?}",
78
+ param_names
79
+ );
80
+ return Err(PyValueError::new_err(msg));
81
+ }
82
+ Ok(())
83
+ }
84
+
85
+
86
+ pub fn parse_dependencies(py: Python<'_>, dep_list: &Bound<'_, PyAny>) -> PyResult<Vec<(String, Py<PyDict>)>> {
87
+ let list = dep_list.cast::<PyList>()?;
88
+ let mut parsed = Vec::with_capacity(list.len());
89
+
90
+ for item in list.iter() {
91
+ if let Ok(tuple) = item.cast::<PyTuple>() {
92
+ if tuple.len() == 2 {
93
+ let method_name: String = tuple.get_item(0)?.extract()?;
94
+ let kwargs = tuple.get_item(1)?;
95
+ let kwargs_dict = kwargs.cast::<PyDict>()?;
96
+ parsed.push((method_name, kwargs_dict.clone().unbind()));
97
+ continue;
98
+ }
99
+ }
100
+
101
+ if let Ok(method_name) = item.extract::<String>() {
102
+ let empty_dict = PyDict::new(py);
103
+ parsed.push((method_name, empty_dict.unbind()));
104
+ continue;
105
+ }
106
+
107
+ return Err(PyValueError::new_err(
108
+ "Dependency must be a tuple (str, dict) or a str",
109
+ ));
110
+ }
111
+
112
+ return Ok(parsed);
113
+ }
@@ -329,7 +329,7 @@ wheels = [
329
329
 
330
330
  [[package]]
331
331
  name = "dependency-cache"
332
- version = "0.1.1"
332
+ version = "0.1.3"
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'" },
@@ -1,62 +0,0 @@
1
- use pyo3::prelude::*;
2
- use pyo3::types::{PyDict, PyString, PyTuple};
3
-
4
-
5
- pub fn normalise_and_hash_method<'py>(
6
- py: Python<'py>,
7
- func_name: &str,
8
- func: Option<&Bound<'py, PyAny>>,
9
- args: &Bound<'py, PyTuple>,
10
- kwargs: Option<&Bound<'py, PyDict>>,
11
- ) -> PyResult<(isize, Bound<'py, PyTuple>)> {
12
- let dict = match func {
13
- Some(f) => {
14
- let inspect = py.import("inspect")?;
15
- let bound = inspect
16
- .getattr("signature")?
17
- .call1((f,))?
18
- .getattr("bind")?
19
- .call(args, kwargs)?;
20
- bound.getattr("apply_defaults")?.call0()?;
21
- bound.getattr("arguments")?
22
- }
23
- None => match kwargs {
24
- Some(d) => d.clone().into_any(),
25
- None => PyDict::new(py).into_any(),
26
- },
27
- };
28
-
29
- let dict = dict.cast::<PyDict>()?;
30
-
31
- let mut items: Vec<(String, Bound<'py, PyAny>)> = Vec::new();
32
- for (k, v) in dict.iter() {
33
- let key: String = k.extract()?;
34
- if key == "self" {
35
- continue;
36
- }
37
- items.push((key, v));
38
- }
39
- items.sort_by(|a, b| a.0.cmp(&b.0));
40
-
41
-
42
- let pairs = items
43
- .into_iter()
44
- .map(|(k, v)| {
45
- let k_py = PyString::new(py, &k).into_any();
46
- PyTuple::new(py, vec![k_py, v])
47
- })
48
- .collect::<PyResult<Vec<_>>>()?;
49
-
50
- let normalised = PyTuple::new(py, pairs)?;
51
-
52
- let combined = PyTuple::new(
53
- py,
54
- vec![
55
- PyString::new(py, func_name).into_any(),
56
- normalised.clone().into_any(),
57
- ],
58
- )?;
59
- let hash_val = combined.hash()?;
60
-
61
- return Ok((hash_val, combined));
62
- }