checkpointer 2.9.0__py3-none-any.whl → 2.9.1__py3-none-any.whl
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.
- checkpointer/checkpoint.py +1 -1
- checkpointer/storages/memory_storage.py +2 -2
- checkpointer/storages/pickle_storage.py +2 -2
- checkpointer/storages/storage.py +5 -2
- checkpointer/test_checkpointer.py +0 -7
- checkpointer-2.9.1.dist-info/METADATA +215 -0
- checkpointer-2.9.1.dist-info/RECORD +16 -0
- checkpointer-2.9.0.dist-info/METADATA +0 -262
- checkpointer-2.9.0.dist-info/RECORD +0 -16
- {checkpointer-2.9.0.dist-info → checkpointer-2.9.1.dist-info}/WHEEL +0 -0
- {checkpointer-2.9.0.dist-info → checkpointer-2.9.1.dist-info}/licenses/LICENSE +0 -0
checkpointer/checkpoint.py
CHANGED
@@ -62,9 +62,9 @@ class CheckpointFn(Generic[Fn]):
|
|
62
62
|
update_wrapper(cast(Callable, self), wrapped)
|
63
63
|
self.checkpointer = checkpointer
|
64
64
|
self.fn = fn
|
65
|
+
self.fn_dir = f"{fn_file}/{fn_name}"
|
65
66
|
self.storage = Storage(self)
|
66
67
|
self.cleanup = self.storage.cleanup
|
67
|
-
self.fn_dir = f"{fn_file}/{fn_name}"
|
68
68
|
|
69
69
|
@cached_property
|
70
70
|
def ident_tuple(self) -> tuple[str, list[Callable]]:
|
@@ -7,7 +7,7 @@ item_map: dict[Path, dict[str, tuple[datetime, Any]]] = {}
|
|
7
7
|
|
8
8
|
class MemoryStorage(Storage):
|
9
9
|
def get_dict(self):
|
10
|
-
return item_map.setdefault(self.
|
10
|
+
return item_map.setdefault(self.fn_dir(), {})
|
11
11
|
|
12
12
|
def store(self, call_id, data):
|
13
13
|
self.get_dict()[call_id] = (datetime.now(), data)
|
@@ -25,7 +25,7 @@ class MemoryStorage(Storage):
|
|
25
25
|
self.get_dict().pop(call_id, None)
|
26
26
|
|
27
27
|
def cleanup(self, invalidated=True, expired=True):
|
28
|
-
curr_key = self.
|
28
|
+
curr_key = self.fn_dir()
|
29
29
|
for key, calldict in list(item_map.items()):
|
30
30
|
if key.parent == curr_key.parent:
|
31
31
|
if invalidated and key != curr_key:
|
@@ -5,7 +5,7 @@ from .storage import Storage
|
|
5
5
|
|
6
6
|
class PickleStorage(Storage):
|
7
7
|
def get_path(self, call_id: str):
|
8
|
-
return self.
|
8
|
+
return self.fn_dir() / f"{call_id}.pkl"
|
9
9
|
|
10
10
|
def store(self, call_id, data):
|
11
11
|
path = self.get_path(call_id)
|
@@ -28,7 +28,7 @@ class PickleStorage(Storage):
|
|
28
28
|
self.get_path(call_id).unlink(missing_ok=True)
|
29
29
|
|
30
30
|
def cleanup(self, invalidated=True, expired=True):
|
31
|
-
version_path = self.
|
31
|
+
version_path = self.fn_dir()
|
32
32
|
fn_path = version_path.parent
|
33
33
|
if invalidated:
|
34
34
|
old_dirs = [path for path in fn_path.iterdir() if path.is_dir() and path != version_path]
|
checkpointer/storages/storage.py
CHANGED
@@ -14,8 +14,11 @@ class Storage:
|
|
14
14
|
self.checkpointer = checkpoint_fn.checkpointer
|
15
15
|
self.checkpoint_fn = checkpoint_fn
|
16
16
|
|
17
|
-
def
|
18
|
-
return self.
|
17
|
+
def fn_id(self) -> str:
|
18
|
+
return f"{self.checkpoint_fn.fn_dir}/{self.checkpoint_fn.fn_hash}"
|
19
|
+
|
20
|
+
def fn_dir(self) -> Path:
|
21
|
+
return self.checkpointer.root_path / self.fn_id()
|
19
22
|
|
20
23
|
def store(self, call_id: str, data: Any) -> None: ...
|
21
24
|
|
@@ -0,0 +1,215 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: checkpointer
|
3
|
+
Version: 2.9.1
|
4
|
+
Summary: A Python library for memoizing function results with support for multiple storage backends, async runtimes, and automatic cache invalidation
|
5
|
+
Project-URL: Repository, https://github.com/Reddan/checkpointer.git
|
6
|
+
Author: Hampus Hallman
|
7
|
+
License: Copyright 2018-2025 Hampus Hallman
|
8
|
+
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
14
|
+
License-File: LICENSE
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
18
|
+
Requires-Python: >=3.11
|
19
|
+
Description-Content-Type: text/markdown
|
20
|
+
|
21
|
+
# checkpointer · [](https://github.com/Reddan/checkpointer/blob/master/LICENSE) [](https://pypi.org/project/checkpointer/) [](https://pypi.org/project/checkpointer/)
|
22
|
+
|
23
|
+
`checkpointer` is a Python library providing a decorator-based API for memoizing (caching) function results. It helps you skip redundant, computationally expensive operations, saving execution time and streamlining your workflows.
|
24
|
+
|
25
|
+
It works with synchronous and asynchronous functions, supports multiple storage backends, and automatically invalidates caches when function code, dependencies, or captured variables change.
|
26
|
+
|
27
|
+
## 📦 Installation
|
28
|
+
|
29
|
+
```bash
|
30
|
+
pip install checkpointer
|
31
|
+
```
|
32
|
+
|
33
|
+
## 🚀 Quick Start
|
34
|
+
|
35
|
+
Apply the `@checkpoint` decorator to any function:
|
36
|
+
|
37
|
+
```python
|
38
|
+
from checkpointer import checkpoint
|
39
|
+
|
40
|
+
@checkpoint
|
41
|
+
def expensive_function(x: int) -> int:
|
42
|
+
print("Computing...")
|
43
|
+
return x ** 2
|
44
|
+
|
45
|
+
result = expensive_function(4) # Computes and stores the result
|
46
|
+
result = expensive_function(4) # Loads from the cache
|
47
|
+
```
|
48
|
+
|
49
|
+
## 🧠 How It Works
|
50
|
+
|
51
|
+
When a function decorated with `@checkpoint` is called:
|
52
|
+
|
53
|
+
1. `checkpointer` computes a unique identifier (hash) for the function call based on its source code, its dependencies, and the arguments passed.
|
54
|
+
2. It attempts to retrieve a cached result using this identifier.
|
55
|
+
3. If a cached result is found, it's returned immediately.
|
56
|
+
4. If no cached result exists or the cache has expired, the original function is executed, its result is stored, and then returned.
|
57
|
+
|
58
|
+
### ♻️ Automatic Cache Invalidation
|
59
|
+
|
60
|
+
`checkpointer` ensures caches are invalidated automatically when the underlying computation changes. A function's hash, which determines cache validity, updates if:
|
61
|
+
|
62
|
+
* **Function Code Changes**: The source code of the decorated function itself is modified.
|
63
|
+
* **Dependencies Change**: Any user-defined function in its dependency tree (direct or indirect, even across modules or not decorated with `@checkpoint`) is modified.
|
64
|
+
* **Captured Variables Change** (with `capture=True`): Global or closure-based variables used within the function are altered.
|
65
|
+
|
66
|
+
**Example: Dependency Invalidation**
|
67
|
+
|
68
|
+
```python
|
69
|
+
def multiply(a, b):
|
70
|
+
return a * b
|
71
|
+
|
72
|
+
@checkpoint
|
73
|
+
def helper(x):
|
74
|
+
# Depends on `multiply`
|
75
|
+
return multiply(x + 1, 2)
|
76
|
+
|
77
|
+
@checkpoint
|
78
|
+
def compute(a, b):
|
79
|
+
# Depends on `helper`
|
80
|
+
return helper(a) + helper(b)
|
81
|
+
```
|
82
|
+
|
83
|
+
If `multiply` is modified, caches for both `helper` and `compute` will automatically be invalidated and recomputed upon their next call.
|
84
|
+
|
85
|
+
## 💡 Usage
|
86
|
+
|
87
|
+
Once a function is decorated with `@checkpoint`, you can interact with its caching behavior using the following methods:
|
88
|
+
|
89
|
+
* **`expensive_function(...)`**:
|
90
|
+
Call the function normally. This will either compute and cache the result or load it from the cache if available.
|
91
|
+
|
92
|
+
* **`expensive_function.rerun(...)`**:
|
93
|
+
Forces the original function to execute, compute a new result, and overwrite any existing cached value for the given arguments.
|
94
|
+
|
95
|
+
* **`expensive_function.fn(...)`**:
|
96
|
+
Calls the original, undecorated function directly, bypassing the cache entirely. This is particularly useful within recursive functions to prevent caching intermediate steps.
|
97
|
+
|
98
|
+
* **`expensive_function.get(...)`**:
|
99
|
+
Attempts to retrieve the cached result for the given arguments without executing the original function. Raises `CheckpointError` if no valid cached result exists.
|
100
|
+
|
101
|
+
* **`expensive_function.exists(...)`**:
|
102
|
+
Checks if a cached result exists for the given arguments without attempting to compute or load it. Returns `True` if a valid checkpoint exists, `False` otherwise.
|
103
|
+
|
104
|
+
* **`expensive_function.delete(...)`**:
|
105
|
+
Removes the cached entry for the specified arguments.
|
106
|
+
|
107
|
+
* **`expensive_function.reinit()`**:
|
108
|
+
Recalculates the function's internal hash. This is primarily used when `capture=True` and you need to update the cache based on changes to external variables within the same Python session.
|
109
|
+
|
110
|
+
## ⚙️ Configuration & Customization
|
111
|
+
|
112
|
+
The `@checkpoint` decorator accepts the following parameters to customize its behavior:
|
113
|
+
|
114
|
+
* **`format`** (Type: `str` or `checkpointer.Storage`, Default: `"pickle"`)
|
115
|
+
Defines the storage backend to use. Built-in options are `"pickle"` (disk-based, persistent) and `"memory"` (in-memory, non-persistent). You can also provide a custom `Storage` class.
|
116
|
+
|
117
|
+
* **`root_path`** (Type: `str` or `pathlib.Path` or `None`, Default: `~/.cache/checkpoints`)
|
118
|
+
The base directory for storing disk-based checkpoints. This parameter is only relevant when `format` is set to `"pickle"`.
|
119
|
+
|
120
|
+
* **`when`** (Type: `bool`, Default: `True`)
|
121
|
+
A boolean flag to enable or disable checkpointing for the decorated function. This is particularly useful for toggling caching based on environment variables (e.g., `when=os.environ.get("ENABLE_CACHING", "false").lower() == "true"`).
|
122
|
+
|
123
|
+
* **`capture`** (Type: `bool`, Default: `False`)
|
124
|
+
If set to `True`, `checkpointer` includes global or closure-based variables used by the function in its hash calculation. This ensures that changes to these external variables also trigger cache invalidation and recomputation.
|
125
|
+
|
126
|
+
* **`should_expire`** (Type: `Callable[[datetime.datetime], bool]`, Default: `None`)
|
127
|
+
A custom callable that receives the `datetime` timestamp of a cached result. It should return `True` if the cached result is considered expired and needs recomputation, or `False` otherwise.
|
128
|
+
|
129
|
+
* **`hash_by`** (Type: `Callable[..., Any]`, Default: `None`)
|
130
|
+
A custom callable that takes the function's arguments (`*args`, `**kwargs`) and returns a hashable object (or tuple of objects). This allows for custom argument normalization (e.g., sorting lists before hashing) or optimized hashing for complex input types, which can improve cache hit rates or speed up the hashing process.
|
131
|
+
|
132
|
+
* **`fn_hash`** (Type: `checkpointer.ObjectHash`, Default: `None`)
|
133
|
+
An optional parameter that takes an instance of `checkpointer.ObjectHash`. This allows you to override the automatically computed function hash, giving you explicit control over when the function's cache should be invalidated. You can pass any values relevant to your invalidation logic to `ObjectHash` (e.g., `ObjectHash(version_string, config_id, ...)`, as it can consistently hash most Python values.
|
134
|
+
|
135
|
+
* **`verbosity`** (Type: `int` (`0`, `1`, or `2`), Default: `1`)
|
136
|
+
Controls the level of logging output from `checkpointer`.
|
137
|
+
* `0`: No output.
|
138
|
+
* `1`: Shows when functions are computed and cached.
|
139
|
+
* `2`: Also shows when cached results are remembered (loaded from cache).
|
140
|
+
|
141
|
+
### 🗄️ Custom Storage Backends
|
142
|
+
|
143
|
+
For integration with databases, cloud storage, or custom serialization, implement your own storage backend by inheriting from `checkpointer.Storage` and implementing its abstract methods.
|
144
|
+
|
145
|
+
Within custom storage methods, `call_id` identifies calls by arguments. Use `self.fn_id()` to get the function's unique identity (name + hash/version), crucial for organizing stored checkpoints (e.g., by function version). Access global `Checkpointer` config via `self.checkpointer`.
|
146
|
+
|
147
|
+
#### Example: Custom Storage Backend
|
148
|
+
|
149
|
+
```python
|
150
|
+
from checkpointer import checkpoint, Storage
|
151
|
+
from datetime import datetime
|
152
|
+
|
153
|
+
class MyCustomStorage(Storage):
|
154
|
+
def exists(self, call_id):
|
155
|
+
# Example: Constructing a path based on function ID and call ID
|
156
|
+
fn_dir = self.checkpointer.root_path / self.fn_id()
|
157
|
+
return (fn_dir / call_id).exists()
|
158
|
+
|
159
|
+
def checkpoint_date(self, call_id): ...
|
160
|
+
def store(self, call_id, data): ...
|
161
|
+
def load(self, call_id): ...
|
162
|
+
def delete(self, call_id): ...
|
163
|
+
|
164
|
+
@checkpoint(format=MyCustomStorage)
|
165
|
+
def custom_cached_function(x: int):
|
166
|
+
return x ** 2
|
167
|
+
```
|
168
|
+
|
169
|
+
## 🧱 Layered Caching
|
170
|
+
|
171
|
+
You can apply multiple `@checkpoint` decorators to a single function to create layered caching strategies. `checkpointer` processes these decorators from bottom to top, meaning the decorator closest to the function definition is evaluated first.
|
172
|
+
|
173
|
+
This is useful for scenarios like combining a fast, ephemeral cache (e.g., in-memory) with a persistent, slower cache (e.g., disk-based).
|
174
|
+
|
175
|
+
**Example: Memory Cache over Disk Cache**
|
176
|
+
|
177
|
+
```python
|
178
|
+
from checkpointer import checkpoint
|
179
|
+
|
180
|
+
@checkpoint(format="memory") # Layer 2: Fast, ephemeral in-memory cache
|
181
|
+
@checkpoint(format="pickle") # Layer 1: Persistent disk cache
|
182
|
+
def some_expensive_operation():
|
183
|
+
print("Performing a time-consuming operation...")
|
184
|
+
return sum(i for i in range(10**7))
|
185
|
+
```
|
186
|
+
|
187
|
+
## ⚡ Async Support
|
188
|
+
|
189
|
+
`checkpointer` works seamlessly with Python's `asyncio` and other async runtimes.
|
190
|
+
|
191
|
+
```python
|
192
|
+
import asyncio
|
193
|
+
from checkpointer import checkpoint
|
194
|
+
|
195
|
+
@checkpoint
|
196
|
+
async def async_compute_sum(a: int, b: int) -> int:
|
197
|
+
print(f"Asynchronously computing {a} + {b}...")
|
198
|
+
await asyncio.sleep(1)
|
199
|
+
return a + b
|
200
|
+
|
201
|
+
async def main():
|
202
|
+
# First call computes and caches
|
203
|
+
result1 = await async_compute_sum(3, 7)
|
204
|
+
print(f"Result 1: {result1}")
|
205
|
+
|
206
|
+
# Second call loads from cache
|
207
|
+
result2 = await async_compute_sum(3, 7)
|
208
|
+
print(f"Result 2: {result2}")
|
209
|
+
|
210
|
+
# Retrieve from cache without re-running the async function
|
211
|
+
result3 = async_compute_sum.get(3, 7)
|
212
|
+
print(f"Result 3 (from cache): {result3}")
|
213
|
+
|
214
|
+
asyncio.run(main())
|
215
|
+
```
|
@@ -0,0 +1,16 @@
|
|
1
|
+
checkpointer/__init__.py,sha256=HRLsQ24ZhxgmDcHchZ-hX6wA0NMCSedGA0NmCnUdS_c,832
|
2
|
+
checkpointer/checkpoint.py,sha256=pbg62Q8gKhx21XEn5kPW95C7ERdQifAg-GAPQN6aWDk,6245
|
3
|
+
checkpointer/fn_ident.py,sha256=SWaksNCTlskMom0ztqjECSRjZYPWXUA1p1ZCb-9tWo0,4297
|
4
|
+
checkpointer/object_hash.py,sha256=cxuWRDrg4F9wC18aC12zOZYOPv3bk2Qf6tZ0_WgAb6Y,7484
|
5
|
+
checkpointer/print_checkpoint.py,sha256=aJCeWMRJiIR3KpyPk_UOKTaD906kArGrmLGQ3LqcVgo,1369
|
6
|
+
checkpointer/test_checkpointer.py,sha256=6gLFggsTDNdG2V9ruFlIM8weixd7ES66e5UQYdFX7Dw,3839
|
7
|
+
checkpointer/utils.py,sha256=2yk1ksKszXofwnSqBrNCFRSR4C3YLPEAdHUFf-cviRU,2755
|
8
|
+
checkpointer/storages/__init__.py,sha256=Kl4Og5jhYxn6m3tB_kTMsabf4_eWVLmFVAoC-pikNQE,301
|
9
|
+
checkpointer/storages/bcolz_storage.py,sha256=3QkSUSeG5s2kFuVV_LZpzMn1A5E7kqC7jk7w35c0NyQ,2314
|
10
|
+
checkpointer/storages/memory_storage.py,sha256=zfN2B_BgPmI1IcYQPo4ICr2AO6ne_UsEc_tvhbDtXBA,1093
|
11
|
+
checkpointer/storages/pickle_storage.py,sha256=WwNg7kmE2on68-6uSPh1JAZLO6gi-P-3VmeBdklKnL0,1541
|
12
|
+
checkpointer/storages/storage.py,sha256=hXzHf1i4Vb_JCeUhY2sVIZlpS049YkM1ya1SAW_x7Ls,912
|
13
|
+
checkpointer-2.9.1.dist-info/METADATA,sha256=cbv49N5LRSbc-7ejHNEHJn_fSZWaA_1nFNJUuR-IH8E,10977
|
14
|
+
checkpointer-2.9.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
15
|
+
checkpointer-2.9.1.dist-info/licenses/LICENSE,sha256=9xVsdtv_-uSyY9Xl9yujwAPm4-mjcCLeVy-ljwXEWbo,1059
|
16
|
+
checkpointer-2.9.1.dist-info/RECORD,,
|
@@ -1,262 +0,0 @@
|
|
1
|
-
Metadata-Version: 2.4
|
2
|
-
Name: checkpointer
|
3
|
-
Version: 2.9.0
|
4
|
-
Summary: A Python library for memoizing function results with support for multiple storage backends, async runtimes, and automatic cache invalidation
|
5
|
-
Project-URL: Repository, https://github.com/Reddan/checkpointer.git
|
6
|
-
Author: Hampus Hallman
|
7
|
-
License: Copyright 2018-2025 Hampus Hallman
|
8
|
-
|
9
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
10
|
-
|
11
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
12
|
-
|
13
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
14
|
-
License-File: LICENSE
|
15
|
-
Classifier: Programming Language :: Python :: 3.11
|
16
|
-
Classifier: Programming Language :: Python :: 3.12
|
17
|
-
Classifier: Programming Language :: Python :: 3.13
|
18
|
-
Requires-Python: >=3.11
|
19
|
-
Description-Content-Type: text/markdown
|
20
|
-
|
21
|
-
# checkpointer · [](https://github.com/Reddan/checkpointer/blob/master/LICENSE) [](https://pypi.org/project/checkpointer/) [](https://pypi.org/project/checkpointer/)
|
22
|
-
|
23
|
-
`checkpointer` is a Python library for memoizing function results. It provides a decorator-based API with support for multiple storage backends. Use it for computationally expensive operations where caching can save time, or during development to avoid waiting for redundant computations.
|
24
|
-
|
25
|
-
Adding or removing `@checkpoint` doesn't change how your code works. You can apply it to any function, including ones you've already written, without altering their behavior or introducing side effects. The original function remains unchanged and can still be called directly when needed.
|
26
|
-
|
27
|
-
### Key Features:
|
28
|
-
- 🗂️ **Multiple Storage Backends**: Built-in support for in-memory and pickle-based storage, or create your own.
|
29
|
-
- 🎯 **Simple Decorator API**: Apply `@checkpoint` to functions without boilerplate.
|
30
|
-
- 🔄 **Async and Sync Compatibility**: Works with synchronous functions and any Python async runtime (e.g., `asyncio`, `Trio`, `Curio`).
|
31
|
-
- ⏲️ **Custom Expiration Logic**: Automatically invalidate old checkpoints.
|
32
|
-
- 📂 **Flexible Path Configuration**: Control where checkpoints are stored.
|
33
|
-
- 📦 **Captured Variables Handling**: Optionally include captured variables in cache invalidation.
|
34
|
-
- ⚡ **Custom Argument Hashing**: Override argument hashing for speed or specialized hashing logic.
|
35
|
-
|
36
|
-
---
|
37
|
-
|
38
|
-
## Installation
|
39
|
-
|
40
|
-
```bash
|
41
|
-
pip install checkpointer
|
42
|
-
```
|
43
|
-
|
44
|
-
---
|
45
|
-
|
46
|
-
## Quick Start 🚀
|
47
|
-
|
48
|
-
```python
|
49
|
-
from checkpointer import checkpoint
|
50
|
-
|
51
|
-
@checkpoint
|
52
|
-
def expensive_function(x: int) -> int:
|
53
|
-
print("Computing...")
|
54
|
-
return x ** 2
|
55
|
-
|
56
|
-
result = expensive_function(4) # Computes and stores the result
|
57
|
-
result = expensive_function(4) # Loads from the cache
|
58
|
-
```
|
59
|
-
|
60
|
-
---
|
61
|
-
|
62
|
-
## How It Works
|
63
|
-
|
64
|
-
When you use `@checkpoint`, the function's **arguments** (`args`, `kwargs`) are hashed to create a unique identifier for each call. This identifier is used to store and retrieve cached results. If the same arguments are passed again, `checkpointer` loads the cached result instead of recomputing.
|
65
|
-
|
66
|
-
Additionally, `checkpointer` ensures that caches are invalidated when a function's implementation or any of its dependencies change. Each function is assigned a hash based on:
|
67
|
-
|
68
|
-
1. **Function Code**: The hash updates when the function’s own source code changes.
|
69
|
-
2. **Dependencies**: If the function calls other user-defined functions, changes in those dependencies also update the hash.
|
70
|
-
3. **External Variables** *(with `capture=True`)*: Any global or closure-based variables used by the function are included in its hash, so changes to those variables also trigger cache invalidation.
|
71
|
-
|
72
|
-
### Example: Cache Invalidation
|
73
|
-
|
74
|
-
```python
|
75
|
-
def multiply(a, b):
|
76
|
-
return a * b
|
77
|
-
|
78
|
-
@checkpoint
|
79
|
-
def helper(x):
|
80
|
-
return multiply(x + 1, 2)
|
81
|
-
|
82
|
-
@checkpoint
|
83
|
-
def compute(a, b):
|
84
|
-
return helper(a) + helper(b)
|
85
|
-
```
|
86
|
-
|
87
|
-
If you modify `multiply`, caches for both `helper` and `compute` are invalidated and recomputed.
|
88
|
-
|
89
|
-
---
|
90
|
-
|
91
|
-
## Parameterization
|
92
|
-
|
93
|
-
### Custom Configuration
|
94
|
-
|
95
|
-
Set up a `Checkpointer` instance with custom settings, and extend it by calling itself with overrides:
|
96
|
-
|
97
|
-
```python
|
98
|
-
from checkpointer import checkpoint
|
99
|
-
|
100
|
-
IS_DEVELOPMENT = True # Toggle based on your environment
|
101
|
-
|
102
|
-
tmp_checkpoint = checkpoint(root_path="/tmp/checkpoints")
|
103
|
-
dev_checkpoint = tmp_checkpoint(when=IS_DEVELOPMENT) # Adds development-specific behavior
|
104
|
-
```
|
105
|
-
|
106
|
-
### Per-Function Customization & Layered Caching
|
107
|
-
|
108
|
-
Layer caches by stacking checkpoints:
|
109
|
-
|
110
|
-
```python
|
111
|
-
@checkpoint(format="memory") # Always use memory storage
|
112
|
-
@dev_checkpoint # Adds caching during development
|
113
|
-
def some_expensive_function():
|
114
|
-
print("Performing a time-consuming operation...")
|
115
|
-
return sum(i * i for i in range(10**8))
|
116
|
-
```
|
117
|
-
|
118
|
-
- **In development**: Both `dev_checkpoint` and `memory` caches are active.
|
119
|
-
- **In production**: Only the `memory` cache is active.
|
120
|
-
|
121
|
-
---
|
122
|
-
|
123
|
-
## Usage
|
124
|
-
|
125
|
-
### Basic Invocation and Caching
|
126
|
-
|
127
|
-
Call the decorated function as usual. On the first call, the result is computed and stored in the cache. Subsequent calls with the same arguments load the result from the cache:
|
128
|
-
|
129
|
-
```python
|
130
|
-
result = expensive_function(4) # Computes and stores the result
|
131
|
-
result = expensive_function(4) # Loads the result from the cache
|
132
|
-
```
|
133
|
-
|
134
|
-
### Force Recalculation
|
135
|
-
|
136
|
-
Force a recalculation and overwrite the stored checkpoint:
|
137
|
-
|
138
|
-
```python
|
139
|
-
result = expensive_function.rerun(4)
|
140
|
-
```
|
141
|
-
|
142
|
-
### Call the Original Function
|
143
|
-
|
144
|
-
Use `fn` to directly call the original, undecorated function:
|
145
|
-
|
146
|
-
```python
|
147
|
-
result = expensive_function.fn(4)
|
148
|
-
```
|
149
|
-
|
150
|
-
This is especially useful **inside recursive functions** to avoid redundant caching of intermediate steps while still caching the final result.
|
151
|
-
|
152
|
-
### Retrieve Stored Checkpoints
|
153
|
-
|
154
|
-
Access cached results without recalculating:
|
155
|
-
|
156
|
-
```python
|
157
|
-
stored_result = expensive_function.get(4)
|
158
|
-
```
|
159
|
-
|
160
|
-
### Refresh Function Hash
|
161
|
-
|
162
|
-
If `capture=True`, you might need to re-hash a function during the same Python session. For that, call `reinit`:
|
163
|
-
|
164
|
-
```python
|
165
|
-
expensive_function.reinit()
|
166
|
-
```
|
167
|
-
|
168
|
-
This tells `checkpointer` to recalculate the function hash, reflecting changes in captured variables.
|
169
|
-
|
170
|
-
---
|
171
|
-
|
172
|
-
## Storage Backends
|
173
|
-
|
174
|
-
`checkpointer` works with built-in and custom storage backends, so you can use what's provided or roll your own as needed.
|
175
|
-
|
176
|
-
### Built-In Backends
|
177
|
-
|
178
|
-
1. **PickleStorage**: Stores checkpoints on disk using Python's `pickle`.
|
179
|
-
2. **MemoryStorage**: Keeps checkpoints in memory for non-persistent, fast caching.
|
180
|
-
|
181
|
-
You can specify a storage backend using either its name (`"pickle"` or `"memory"`) or its corresponding class (`PickleStorage` or `MemoryStorage`) in the `format` parameter:
|
182
|
-
|
183
|
-
```python
|
184
|
-
from checkpointer import checkpoint, PickleStorage, MemoryStorage
|
185
|
-
|
186
|
-
@checkpoint(format="pickle") # Short for format=PickleStorage
|
187
|
-
def disk_cached(x: int) -> int:
|
188
|
-
return x ** 2
|
189
|
-
|
190
|
-
@checkpoint(format="memory") # Short for format=MemoryStorage
|
191
|
-
def memory_cached(x: int) -> int:
|
192
|
-
return x * 10
|
193
|
-
```
|
194
|
-
|
195
|
-
### Custom Storage Backends
|
196
|
-
|
197
|
-
Create a custom storage backend by inheriting from the `Storage` class and implementing its methods. Access configuration options through the `self.checkpointer` attribute, an instance of `Checkpointer`.
|
198
|
-
|
199
|
-
#### Example: Custom Storage Backend
|
200
|
-
|
201
|
-
```python
|
202
|
-
from checkpointer import checkpoint, Storage
|
203
|
-
from datetime import datetime
|
204
|
-
|
205
|
-
class CustomStorage(Storage):
|
206
|
-
def exists(self, call_id) -> bool: ... # Check if a checkpoint exists
|
207
|
-
def checkpoint_date(self, call_id) -> datetime: ... # Get the checkpoint's timestamp
|
208
|
-
def store(self, call_id, data): ... # Save data to the checkpoint
|
209
|
-
def load(self, call_id): ... # Load data from the checkpoint
|
210
|
-
def delete(self, call_id): ... # Delete the checkpoint
|
211
|
-
|
212
|
-
@checkpoint(format=CustomStorage)
|
213
|
-
def custom_cached(x: int):
|
214
|
-
return x ** 2
|
215
|
-
```
|
216
|
-
|
217
|
-
Use a custom backend to integrate with databases, cloud storage, or specialized file formats.
|
218
|
-
|
219
|
-
---
|
220
|
-
|
221
|
-
## Configuration Options ⚙️
|
222
|
-
|
223
|
-
| Option | Type | Default | Description |
|
224
|
-
|-----------------|-------------------------------------|----------------------|-----------------------------------------------------------|
|
225
|
-
| `capture` | `bool` | `False` | Include captured variables in function hashes. |
|
226
|
-
| `format` | `"pickle"`, `"memory"`, `Storage` | `"pickle"` | Storage backend format. |
|
227
|
-
| `root_path` | `Path`, `str`, or `None` | ~/.cache/checkpoints | Root directory for storing checkpoints. |
|
228
|
-
| `when` | `bool` | `True` | Enable or disable checkpointing. |
|
229
|
-
| `verbosity` | `0`, `1` or `2` | `1` | Logging verbosity. |
|
230
|
-
| `should_expire` | `Callable[[datetime], bool]` | `None` | Custom expiration logic. |
|
231
|
-
| `hash_by` | `Callable[..., Any]` | `None` | Custom function that transforms arguments before hashing. |
|
232
|
-
|
233
|
-
---
|
234
|
-
|
235
|
-
## Full Example 🛠️
|
236
|
-
|
237
|
-
```python
|
238
|
-
import asyncio
|
239
|
-
from checkpointer import checkpoint
|
240
|
-
|
241
|
-
@checkpoint
|
242
|
-
def compute_square(n: int) -> int:
|
243
|
-
print(f"Computing {n}^2...")
|
244
|
-
return n ** 2
|
245
|
-
|
246
|
-
@checkpoint(format="memory")
|
247
|
-
async def async_compute_sum(a: int, b: int) -> int:
|
248
|
-
await asyncio.sleep(1)
|
249
|
-
return a + b
|
250
|
-
|
251
|
-
async def main():
|
252
|
-
result1 = compute_square(5)
|
253
|
-
print(result1) # Outputs 25
|
254
|
-
|
255
|
-
result2 = await async_compute_sum(3, 7)
|
256
|
-
print(result2) # Outputs 10
|
257
|
-
|
258
|
-
result3 = async_compute_sum.get(3, 7)
|
259
|
-
print(result3) # Outputs 10
|
260
|
-
|
261
|
-
asyncio.run(main())
|
262
|
-
```
|
@@ -1,16 +0,0 @@
|
|
1
|
-
checkpointer/__init__.py,sha256=HRLsQ24ZhxgmDcHchZ-hX6wA0NMCSedGA0NmCnUdS_c,832
|
2
|
-
checkpointer/checkpoint.py,sha256=_1A-IH3wNkUc1vNMg0fA4gS6mI8wiwtTX1iPFZdEbRI,6245
|
3
|
-
checkpointer/fn_ident.py,sha256=SWaksNCTlskMom0ztqjECSRjZYPWXUA1p1ZCb-9tWo0,4297
|
4
|
-
checkpointer/object_hash.py,sha256=cxuWRDrg4F9wC18aC12zOZYOPv3bk2Qf6tZ0_WgAb6Y,7484
|
5
|
-
checkpointer/print_checkpoint.py,sha256=aJCeWMRJiIR3KpyPk_UOKTaD906kArGrmLGQ3LqcVgo,1369
|
6
|
-
checkpointer/test_checkpointer.py,sha256=Jsrgpxf6ux-P1OPy4t1wIi028EPSqSltTC9BRhgCMZ4,3999
|
7
|
-
checkpointer/utils.py,sha256=2yk1ksKszXofwnSqBrNCFRSR4C3YLPEAdHUFf-cviRU,2755
|
8
|
-
checkpointer/storages/__init__.py,sha256=Kl4Og5jhYxn6m3tB_kTMsabf4_eWVLmFVAoC-pikNQE,301
|
9
|
-
checkpointer/storages/bcolz_storage.py,sha256=3QkSUSeG5s2kFuVV_LZpzMn1A5E7kqC7jk7w35c0NyQ,2314
|
10
|
-
checkpointer/storages/memory_storage.py,sha256=73KVPAlunjd2Ln4mXsmRk2Njd92ILIaYDszsAH3Pi30,1087
|
11
|
-
checkpointer/storages/pickle_storage.py,sha256=nes0vVgVt4zfGRPk73vfPp3_6ZZy3cYwhx0K_2yAaWM,1535
|
12
|
-
checkpointer/storages/storage.py,sha256=jjiUCxRZYwgBNIpQ4IMNGSb_5IvyY8kFtAYnXLmFoGs,853
|
13
|
-
checkpointer-2.9.0.dist-info/METADATA,sha256=2urG6x-DqQ8g7Mwchl0ohwGny4W_cLnFE349j5_tvbM,10615
|
14
|
-
checkpointer-2.9.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
15
|
-
checkpointer-2.9.0.dist-info/licenses/LICENSE,sha256=9xVsdtv_-uSyY9Xl9yujwAPm4-mjcCLeVy-ljwXEWbo,1059
|
16
|
-
checkpointer-2.9.0.dist-info/RECORD,,
|
File without changes
|
File without changes
|