chestkey 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
chestkey-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Wannes Vantorre
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,44 @@
1
+ Metadata-Version: 2.4
2
+ Name: chestkey
3
+ Version: 0.1.0
4
+ Summary: A generic storage datatype based on deterministic identities.
5
+ Keywords: cache,storage,memoization,content-addressed,deterministic
6
+ Author: Wannes Vantorre
7
+ Author-email: Wannes Vantorre <vantorrewannes@gmail.com>
8
+ License-Expression: MIT
9
+ License-File: LICENSE
10
+ Classifier: Development Status :: 4 - Beta
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Programming Language :: Python :: 3.13
16
+ Classifier: Programming Language :: Python :: 3.14
17
+ Classifier: Topic :: Software Development :: Libraries
18
+ Classifier: Typing :: Typed
19
+ Requires-Dist: blake3>=1.0.9
20
+ Requires-Dist: dill>=0.4.1
21
+ Requires-Python: >=3.12
22
+ Description-Content-Type: text/markdown
23
+
24
+ # chestkey
25
+
26
+ A generic storage datatype based on deterministic identities.
27
+
28
+ ```python
29
+ from pathlib import Path
30
+ from chestkey import Chest, Key
31
+
32
+ def add(x: int, y: int) -> int:
33
+ return x + y
34
+
35
+ def cached_add(x: int, y: int) -> int:
36
+ chest = Chest(Path(".cache"))
37
+ key = Key((add.__code__, x, y))
38
+ if chest.contains(key):
39
+ return chest.get[int](key)
40
+
41
+ z = add(x, y)
42
+ chest.set(key, z)
43
+ return z
44
+ ```
@@ -0,0 +1,21 @@
1
+ # chestkey
2
+
3
+ A generic storage datatype based on deterministic identities.
4
+
5
+ ```python
6
+ from pathlib import Path
7
+ from chestkey import Chest, Key
8
+
9
+ def add(x: int, y: int) -> int:
10
+ return x + y
11
+
12
+ def cached_add(x: int, y: int) -> int:
13
+ chest = Chest(Path(".cache"))
14
+ key = Key((add.__code__, x, y))
15
+ if chest.contains(key):
16
+ return chest.get[int](key)
17
+
18
+ z = add(x, y)
19
+ chest.set(key, z)
20
+ return z
21
+ ```
@@ -0,0 +1,99 @@
1
+ [project]
2
+ name = "chestkey"
3
+ version = "0.1.0"
4
+ description = "A generic storage datatype based on deterministic identities."
5
+ readme = "README.md"
6
+ license = "MIT"
7
+ license-files = ["LICENSE"]
8
+ requires-python = ">=3.12"
9
+ keywords = [
10
+ "cache",
11
+ "storage",
12
+ "memoization",
13
+ "content-addressed",
14
+ "deterministic",
15
+ ]
16
+ classifiers = [
17
+ "Development Status :: 4 - Beta",
18
+ "Intended Audience :: Developers",
19
+ "Operating System :: OS Independent",
20
+ "Programming Language :: Python :: 3",
21
+ "Programming Language :: Python :: 3.12",
22
+ "Programming Language :: Python :: 3.13",
23
+ "Programming Language :: Python :: 3.14",
24
+ "Topic :: Software Development :: Libraries",
25
+ "Typing :: Typed",
26
+ ]
27
+ dependencies = [
28
+ "blake3>=1.0.9",
29
+ "dill>=0.4.1",
30
+ ]
31
+
32
+ [[project.authors]]
33
+ name = "Wannes Vantorre"
34
+ email = "vantorrewannes@gmail.com"
35
+
36
+ [dependency-groups]
37
+ dev = [
38
+ "pytest>=8.4",
39
+ "pytest-cov>=7.0",
40
+ "pytest-mock>=3.15.1",
41
+ ]
42
+
43
+ [build-system]
44
+ requires = ["uv_build>=0.12.7,<0.13.0"]
45
+ build-backend = "uv_build"
46
+
47
+ [tool.pytest.ini_options]
48
+ testpaths = ["tests"]
49
+ addopts = "--import-mode=importlib"
50
+
51
+ [tool.coverage.run]
52
+ branch = true
53
+ source_pkgs = ["chestkey"]
54
+
55
+ [tool.coverage.report]
56
+ show_missing = true
57
+ fail_under = 100
58
+
59
+ [tool.ruff]
60
+ target-version = "py312"
61
+ line-length = 88
62
+ src = [
63
+ "src",
64
+ "tests",
65
+ ]
66
+
67
+ [tool.ruff.lint]
68
+ select = ["ALL"]
69
+ ignore = [
70
+ "CPY001",
71
+ "COM812",
72
+ "ISC001",
73
+ "D203",
74
+ "D213",
75
+ "D100",
76
+ "D104",
77
+ "D105",
78
+ "ANN401",
79
+ "FBT001",
80
+ "FBT002",
81
+ "TC001",
82
+ "TC002",
83
+ "TC003",
84
+ "EM101",
85
+ "EM102",
86
+ "TRY003",
87
+ "PLR2004",
88
+ ]
89
+
90
+ [tool.ruff.lint.per-file-ignores]
91
+ "tests/**" = [
92
+ "S101",
93
+ "PLR2004",
94
+ "D103",
95
+ "INP001",
96
+ ]
97
+
98
+ [tool.ruff.format]
99
+ docstring-code-format = true
@@ -0,0 +1,94 @@
1
+ [project]
2
+ name = "chestkey"
3
+ version = "0.1.0"
4
+ description = "A generic storage datatype based on deterministic identities."
5
+ readme = "README.md"
6
+ license = "MIT"
7
+ license-files = ["LICENSE"]
8
+ authors = [
9
+ { name = "Wannes Vantorre", email = "vantorrewannes@gmail.com" }
10
+ ]
11
+ requires-python = ">=3.12"
12
+ keywords = ["cache", "storage", "memoization", "content-addressed", "deterministic"]
13
+ classifiers = [
14
+ "Development Status :: 4 - Beta",
15
+ "Intended Audience :: Developers",
16
+ "Operating System :: OS Independent",
17
+ "Programming Language :: Python :: 3",
18
+ "Programming Language :: Python :: 3.12",
19
+ "Programming Language :: Python :: 3.13",
20
+ "Programming Language :: Python :: 3.14",
21
+ "Topic :: Software Development :: Libraries",
22
+ "Typing :: Typed",
23
+ ]
24
+ dependencies = [
25
+ "blake3>=1.0.9",
26
+ "dill>=0.4.1",
27
+ ]
28
+
29
+ [dependency-groups]
30
+ dev = [
31
+ "pytest>=8.4",
32
+ "pytest-cov>=7.0",
33
+ "pytest-mock>=3.15.1",
34
+ ]
35
+
36
+ [build-system]
37
+ requires = ["uv_build>=0.12.7,<0.13.0"]
38
+ build-backend = "uv_build"
39
+
40
+ [tool.pytest.ini_options]
41
+ testpaths = ["tests"]
42
+ addopts = "--import-mode=importlib"
43
+
44
+ [tool.coverage.run]
45
+ branch = true
46
+ source_pkgs = ["chestkey"]
47
+
48
+ [tool.coverage.report]
49
+ show_missing = true
50
+ fail_under = 100
51
+
52
+ [tool.ruff]
53
+ target-version = "py312"
54
+ line-length = 88
55
+ src = ["src", "tests"]
56
+
57
+ [tool.ruff.lint]
58
+ select = ["ALL"]
59
+ ignore = [
60
+ "CPY001",
61
+
62
+ "COM812",
63
+ "ISC001",
64
+ "D203",
65
+ "D213",
66
+
67
+ "D100",
68
+ "D104",
69
+ "D105",
70
+
71
+ "ANN401",
72
+ "FBT001",
73
+ "FBT002",
74
+
75
+ "TC001",
76
+ "TC002",
77
+ "TC003",
78
+
79
+ "EM101",
80
+ "EM102",
81
+ "TRY003",
82
+ "PLR2004",
83
+ ]
84
+
85
+ [tool.ruff.lint.per-file-ignores]
86
+ "tests/**" = [
87
+ "S101",
88
+ "PLR2004",
89
+ "D103",
90
+ "INP001",
91
+ ]
92
+
93
+ [tool.ruff.format]
94
+ docstring-code-format = true
@@ -0,0 +1,3 @@
1
+ from chestkey.lib import Chest, Key, TypedGetter
2
+
3
+ __all__ = ["Chest", "Key", "TypedGetter"]
@@ -0,0 +1,69 @@
1
+ from collections.abc import Callable
2
+ from pathlib import Path
3
+
4
+ import blake3
5
+ import dill
6
+
7
+
8
+ class Key:
9
+ """A deterministic identity derived from arbitrary content."""
10
+
11
+ __slots__ = ("digest",)
12
+
13
+ def __init__(self, seed: object) -> None:
14
+ """Derive the identity from the seed's serialized content."""
15
+ self.digest = blake3.blake3(dill.dumps(seed)).hexdigest()
16
+
17
+ def __eq__(self, other: object) -> bool:
18
+ if not isinstance(other, Key):
19
+ return NotImplemented
20
+ return self.digest == other.digest
21
+
22
+ def __hash__(self) -> int:
23
+ return hash(self.digest)
24
+
25
+ def __repr__(self) -> str:
26
+ return f"Key({self.digest[:16]})"
27
+
28
+
29
+ class TypedGetter[K]:
30
+ """Wrapper that makes a fetch method typed: `getter[T](key)`."""
31
+
32
+ __slots__ = ("fetch",)
33
+
34
+ def __init__(self, fetch: Callable[[K], object]) -> None: # noqa: D107
35
+ self.fetch = fetch
36
+
37
+ def __getitem__[T](self, value_type: type[T]) -> Callable[[K], T]:
38
+ def get(key: K) -> T:
39
+ value = self.fetch(key)
40
+ if not isinstance(value, value_type):
41
+ stored = type(value).__name__
42
+ msg = f"{key}: stored {stored}, expected {value_type.__name__}"
43
+ raise TypeError(msg)
44
+ return value
45
+
46
+ return get
47
+
48
+
49
+ class Chest:
50
+ """A generic storage datatype based on deterministic identities."""
51
+
52
+ def __init__(self, path: Path) -> None:
53
+ """Bind the storage location; nothing is created until first write."""
54
+ self.path = path
55
+ self.get = TypedGetter(self._fetch)
56
+
57
+ def contains(self, key: Key) -> bool:
58
+ """Answer whether the key has a stored value."""
59
+ return (self.path / key.digest).is_file()
60
+
61
+ def set(self, key: Key, value: object) -> None:
62
+ """Store the value at the key."""
63
+ self.path.mkdir(parents=True, exist_ok=True)
64
+ (self.path / key.digest).write_bytes(dill.dumps(value))
65
+
66
+ def _fetch(self, key: Key) -> object:
67
+ if not self.contains(key):
68
+ raise KeyError(key)
69
+ return dill.loads((self.path / key.digest).read_bytes()) # noqa: S301
File without changes