checkpointer 2.8.1__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.
@@ -1,10 +1,11 @@
1
1
  from __future__ import annotations
2
2
  import inspect
3
3
  import re
4
+ from contextlib import suppress
4
5
  from datetime import datetime
5
- from functools import update_wrapper
6
+ from functools import cached_property, update_wrapper
6
7
  from pathlib import Path
7
- from typing import Any, Awaitable, Callable, Generic, Iterable, Literal, ParamSpec, Type, TypedDict, TypeVar, Unpack, cast, overload
8
+ from typing import Awaitable, Callable, Generic, Iterable, Literal, ParamSpec, Type, TypedDict, TypeVar, Unpack, cast, overload
8
9
  from .fn_ident import get_fn_ident
9
10
  from .object_hash import ObjectHash
10
11
  from .print_checkpoint import print_checkpoint
@@ -54,84 +55,83 @@ class Checkpointer:
54
55
 
55
56
  class CheckpointFn(Generic[Fn]):
56
57
  def __init__(self, checkpointer: Checkpointer, fn: Fn):
57
- self.checkpointer = checkpointer
58
- self.fn = fn
59
-
60
- def _set_ident(self, force=False):
61
- if not hasattr(self, "fn_hash_raw") or force:
62
- self.fn_hash_raw, self.depends = get_fn_ident(unwrap_fn(self.fn), self.checkpointer.capture)
63
- return self
64
-
65
- def _lazyinit(self):
66
- params = self.checkpointer
67
- wrapped = unwrap_fn(self.fn)
58
+ wrapped = unwrap_fn(fn)
68
59
  fn_file = Path(wrapped.__code__.co_filename).name
69
60
  fn_name = re.sub(r"[^\w.]", "", wrapped.__qualname__)
61
+ Storage = STORAGE_MAP[checkpointer.format] if isinstance(checkpointer.format, str) else checkpointer.format
70
62
  update_wrapper(cast(Callable, self), wrapped)
71
- Storage = STORAGE_MAP[params.format] if isinstance(params.format, str) else params.format
72
- deep_hashes = [child._set_ident().fn_hash_raw for child in iterate_checkpoint_fns(self)]
73
- self.fn_hash = str(params.fn_hash or ObjectHash(digest_size=16).write_text(self.fn_hash_raw, *deep_hashes))
74
- self.fn_subdir = f"{fn_file}/{fn_name}/{self.fn_hash[:32]}"
63
+ self.checkpointer = checkpointer
64
+ self.fn = fn
65
+ self.fn_dir = f"{fn_file}/{fn_name}"
75
66
  self.storage = Storage(self)
76
67
  self.cleanup = self.storage.cleanup
77
68
 
78
- def __getattribute__(self, name: str) -> Any:
79
- return object.__getattribute__(self, "_getattribute")(name)
69
+ @cached_property
70
+ def ident_tuple(self) -> tuple[str, list[Callable]]:
71
+ return get_fn_ident(unwrap_fn(self.fn), self.checkpointer.capture)
72
+
73
+ @property
74
+ def fn_hash_raw(self) -> str:
75
+ return self.ident_tuple[0]
76
+
77
+ @property
78
+ def depends(self) -> list[Callable]:
79
+ return self.ident_tuple[1]
80
80
 
81
- def _getattribute(self, name: str) -> Any:
82
- setattr(self, "_getattribute", super().__getattribute__)
83
- self._lazyinit()
84
- return self._getattribute(name)
81
+ @cached_property
82
+ def fn_hash(self) -> str:
83
+ fn_hash = self.checkpointer.fn_hash
84
+ deep_hashes = [depend.fn_hash_raw for depend in self.deep_depends()]
85
+ return str(fn_hash or ObjectHash(digest_size=16).write_text(self.fn_hash_raw, *deep_hashes))[:32]
85
86
 
86
87
  def reinit(self, recursive=False) -> CheckpointFn[Fn]:
87
- pointfns = list(iterate_checkpoint_fns(self)) if recursive else [self]
88
- for pointfn in pointfns:
89
- pointfn._set_ident(True)
90
- for pointfn in pointfns:
91
- pointfn._lazyinit()
88
+ depends = list(self.deep_depends()) if recursive else [self]
89
+ for depend in depends:
90
+ with suppress(AttributeError):
91
+ del depend.ident_tuple, depend.fn_hash
92
+ for depend in depends:
93
+ depend.fn_hash
92
94
  return self
93
95
 
94
- def get_checkpoint_id(self, args: tuple, kw: dict) -> str:
96
+ def get_call_id(self, args: tuple, kw: dict) -> str:
95
97
  hash_by = self.checkpointer.hash_by
96
98
  hash_params = hash_by(*args, **kw) if hash_by else (args, kw)
97
- call_hash = ObjectHash(hash_params, digest_size=16)
98
- return f"{self.fn_subdir}/{call_hash}"
99
+ return str(ObjectHash(hash_params, digest_size=16))
99
100
 
100
- async def _resolve_awaitable(self, checkpoint_path: Path, awaitable: Awaitable):
101
+ async def _resolve_awaitable(self, checkpoint_id: str, awaitable: Awaitable):
101
102
  data = await awaitable
102
- self.storage.store(checkpoint_path, AwaitableValue(data))
103
+ self.storage.store(checkpoint_id, AwaitableValue(data))
103
104
  return data
104
105
 
105
- def _store_on_demand(self, args: tuple, kw: dict, rerun: bool):
106
+ def _call(self, args: tuple, kw: dict, rerun=False):
106
107
  params = self.checkpointer
107
- checkpoint_id = self.get_checkpoint_id(args, kw)
108
- checkpoint_path = params.root_path / checkpoint_id
108
+ if not params.when:
109
+ return self.fn(*args, **kw)
110
+
111
+ call_id = self.get_call_id(args, kw)
112
+ call_id_long = f"{self.fn_dir}/{self.fn_hash}/{call_id}"
113
+
109
114
  refresh = rerun \
110
- or not self.storage.exists(checkpoint_path) \
111
- or (params.should_expire and params.should_expire(self.storage.checkpoint_date(checkpoint_path)))
115
+ or not self.storage.exists(call_id) \
116
+ or (params.should_expire and params.should_expire(self.storage.checkpoint_date(call_id)))
112
117
 
113
118
  if refresh:
114
- print_checkpoint(params.verbosity >= 1, "MEMORIZING", checkpoint_id, "blue")
119
+ print_checkpoint(params.verbosity >= 1, "MEMORIZING", call_id_long, "blue")
115
120
  data = self.fn(*args, **kw)
116
121
  if inspect.isawaitable(data):
117
- return self._resolve_awaitable(checkpoint_path, data)
122
+ return self._resolve_awaitable(call_id, data)
118
123
  else:
119
- self.storage.store(checkpoint_path, data)
124
+ self.storage.store(call_id, data)
120
125
  return data
121
126
 
122
127
  try:
123
- data = self.storage.load(checkpoint_path)
124
- print_checkpoint(params.verbosity >= 2, "REMEMBERED", checkpoint_id, "green")
128
+ data = self.storage.load(call_id)
129
+ print_checkpoint(params.verbosity >= 2, "REMEMBERED", call_id_long, "green")
125
130
  return data
126
131
  except (EOFError, FileNotFoundError):
127
132
  pass
128
- print_checkpoint(params.verbosity >= 1, "CORRUPTED", checkpoint_id, "yellow")
129
- return self._store_on_demand(args, kw, True)
130
-
131
- def _call(self, args: tuple, kw: dict, rerun=False):
132
- if not self.checkpointer.when:
133
- return self.fn(*args, **kw)
134
- return self._store_on_demand(args, kw, rerun)
133
+ print_checkpoint(params.verbosity >= 1, "CORRUPTED", call_id_long, "yellow")
134
+ return self._call(args, kw, True)
135
135
 
136
136
  __call__: Fn = cast(Fn, lambda self, *args, **kw: self._call(args, kw))
137
137
  rerun: Fn = cast(Fn, lambda self, *args, **kw: self._call(args, kw, True))
@@ -142,25 +142,29 @@ class CheckpointFn(Generic[Fn]):
142
142
  def get(self: Callable[P, R], *args: P.args, **kw: P.kwargs) -> R: ...
143
143
 
144
144
  def get(self, *args, **kw):
145
- checkpoint_path = self.checkpointer.root_path / self.get_checkpoint_id(args, kw)
145
+ call_id = self.get_call_id(args, kw)
146
146
  try:
147
- data = self.storage.load(checkpoint_path)
147
+ data = self.storage.load(call_id)
148
148
  return data.value if isinstance(data, AwaitableValue) else data
149
149
  except Exception as ex:
150
150
  raise CheckpointError("Could not load checkpoint") from ex
151
151
 
152
152
  def exists(self: Callable[P, R], *args: P.args, **kw: P.kwargs) -> bool: # type: ignore
153
153
  self = cast(CheckpointFn, self)
154
- return self.storage.exists(self.checkpointer.root_path / self.get_checkpoint_id(args, kw))
154
+ return self.storage.exists(self.get_call_id(args, kw))
155
+
156
+ def delete(self: Callable[P, R], *args: P.args, **kw: P.kwargs): # type: ignore
157
+ self = cast(CheckpointFn, self)
158
+ self.storage.delete(self.get_call_id(args, kw))
155
159
 
156
160
  def __repr__(self) -> str:
157
161
  return f"<CheckpointFn {self.fn.__name__} {self.fn_hash[:6]}>"
158
162
 
159
- def iterate_checkpoint_fns(pointfn: CheckpointFn, visited: set[CheckpointFn] = set()) -> Iterable[CheckpointFn]:
160
- visited = visited or set()
161
- if pointfn not in visited:
162
- yield pointfn
163
- visited.add(pointfn)
164
- for depend in pointfn.depends:
165
- if isinstance(depend, CheckpointFn):
166
- yield from iterate_checkpoint_fns(depend, visited)
163
+ def deep_depends(self, visited: set[CheckpointFn] = set()) -> Iterable[CheckpointFn]:
164
+ if self not in visited:
165
+ yield self
166
+ visited = visited or set()
167
+ visited.add(self)
168
+ for depend in self.depends:
169
+ if isinstance(depend, CheckpointFn):
170
+ yield from depend.deep_depends(visited)
@@ -5,35 +5,32 @@ from .storage import Storage
5
5
 
6
6
  item_map: dict[Path, dict[str, tuple[datetime, Any]]] = {}
7
7
 
8
- def get_short_path(path: Path):
9
- return path.parts[-1]
10
-
11
8
  class MemoryStorage(Storage):
12
9
  def get_dict(self):
13
- return item_map.setdefault(self.checkpointer.root_path / self.checkpoint_fn.fn_subdir, {})
10
+ return item_map.setdefault(self.fn_dir(), {})
14
11
 
15
- def store(self, path, data):
16
- self.get_dict()[get_short_path(path)] = (datetime.now(), data)
12
+ def store(self, call_id, data):
13
+ self.get_dict()[call_id] = (datetime.now(), data)
17
14
 
18
- def exists(self, path):
19
- return get_short_path(path) in self.get_dict()
15
+ def exists(self, call_id):
16
+ return call_id in self.get_dict()
20
17
 
21
- def checkpoint_date(self, path):
22
- return self.get_dict()[get_short_path(path)][0]
18
+ def checkpoint_date(self, call_id):
19
+ return self.get_dict()[call_id][0]
23
20
 
24
- def load(self, path):
25
- return self.get_dict()[get_short_path(path)][1]
21
+ def load(self, call_id):
22
+ return self.get_dict()[call_id][1]
26
23
 
27
- def delete(self, path):
28
- del self.get_dict()[get_short_path(path)]
24
+ def delete(self, call_id):
25
+ self.get_dict().pop(call_id, None)
29
26
 
30
27
  def cleanup(self, invalidated=True, expired=True):
31
- curr_key = self.checkpointer.root_path / self.checkpoint_fn.fn_subdir
28
+ curr_key = self.fn_dir()
32
29
  for key, calldict in list(item_map.items()):
33
30
  if key.parent == curr_key.parent:
34
31
  if invalidated and key != curr_key:
35
32
  del item_map[key]
36
33
  elif expired and self.checkpointer.should_expire:
37
- for callid, (date, _) in list(calldict.items()):
34
+ for call_id, (date, _) in list(calldict.items()):
38
35
  if self.checkpointer.should_expire(date):
39
- del calldict[callid]
36
+ del calldict[call_id]
@@ -1,35 +1,34 @@
1
1
  import pickle
2
2
  import shutil
3
- from pathlib import Path
4
3
  from datetime import datetime
5
4
  from .storage import Storage
6
5
 
7
- def get_path(path: Path):
8
- return path.with_name(f"{path.name}.pkl")
9
-
10
6
  class PickleStorage(Storage):
11
- def store(self, path, data):
12
- full_path = get_path(path)
13
- full_path.parent.mkdir(parents=True, exist_ok=True)
14
- with full_path.open("wb") as file:
7
+ def get_path(self, call_id: str):
8
+ return self.fn_dir() / f"{call_id}.pkl"
9
+
10
+ def store(self, call_id, data):
11
+ path = self.get_path(call_id)
12
+ path.parent.mkdir(parents=True, exist_ok=True)
13
+ with path.open("wb") as file:
15
14
  pickle.dump(data, file, -1)
16
15
 
17
- def exists(self, path):
18
- return get_path(path).exists()
16
+ def exists(self, call_id):
17
+ return self.get_path(call_id).exists()
19
18
 
20
- def checkpoint_date(self, path):
19
+ def checkpoint_date(self, call_id):
21
20
  # Should use st_atime/access time?
22
- return datetime.fromtimestamp(get_path(path).stat().st_mtime)
21
+ return datetime.fromtimestamp(self.get_path(call_id).stat().st_mtime)
23
22
 
24
- def load(self, path):
25
- with get_path(path).open("rb") as file:
23
+ def load(self, call_id):
24
+ with self.get_path(call_id).open("rb") as file:
26
25
  return pickle.load(file)
27
26
 
28
- def delete(self, path):
29
- get_path(path).unlink(missing_ok=True)
27
+ def delete(self, call_id):
28
+ self.get_path(call_id).unlink(missing_ok=True)
30
29
 
31
30
  def cleanup(self, invalidated=True, expired=True):
32
- version_path = self.checkpointer.root_path.resolve() / self.checkpoint_fn.fn_subdir
31
+ version_path = self.fn_dir()
33
32
  fn_path = version_path.parent
34
33
  if invalidated:
35
34
  old_dirs = [path for path in fn_path.iterdir() if path.is_dir() and path != version_path]
@@ -39,8 +38,7 @@ class PickleStorage(Storage):
39
38
  if expired and self.checkpointer.should_expire:
40
39
  count = 0
41
40
  for pkl_path in fn_path.rglob("*.pkl"):
42
- path = pkl_path.with_suffix("")
43
- if self.checkpointer.should_expire(self.checkpoint_date(path)):
41
+ if self.checkpointer.should_expire(self.checkpoint_date(pkl_path.stem)):
44
42
  count += 1
45
- self.delete(path)
43
+ self.delete(pkl_path.stem)
46
44
  print(f"Removed {count} expired checkpoints for {self.checkpoint_fn.__qualname__}")
@@ -14,14 +14,20 @@ class Storage:
14
14
  self.checkpointer = checkpoint_fn.checkpointer
15
15
  self.checkpoint_fn = checkpoint_fn
16
16
 
17
- def store(self, path: Path, data: Any) -> None: ...
17
+ def fn_id(self) -> str:
18
+ return f"{self.checkpoint_fn.fn_dir}/{self.checkpoint_fn.fn_hash}"
18
19
 
19
- def exists(self, path: Path) -> bool: ...
20
+ def fn_dir(self) -> Path:
21
+ return self.checkpointer.root_path / self.fn_id()
20
22
 
21
- def checkpoint_date(self, path: Path) -> datetime: ...
23
+ def store(self, call_id: str, data: Any) -> None: ...
22
24
 
23
- def load(self, path: Path) -> Any: ...
25
+ def exists(self, call_id: str) -> bool: ...
24
26
 
25
- def delete(self, path: Path) -> None: ...
27
+ def checkpoint_date(self, call_id: str) -> datetime: ...
28
+
29
+ def load(self, call_id: str) -> Any: ...
30
+
31
+ def delete(self, call_id: str) -> None: ...
26
32
 
27
33
  def cleanup(self, invalidated=True, expired=True): ...
@@ -142,7 +142,7 @@ def test_depends():
142
142
  assert set(test_a.depends) == {test_a.fn, helper, multiply_wrapper, global_multiply}
143
143
  assert set(test_b.depends) == {test_b.fn, test_a, multiply_wrapper, global_multiply}
144
144
 
145
- def test_lazy_init():
145
+ def test_lazy_init_1():
146
146
  @checkpoint
147
147
  def fn1(x: object) -> object:
148
148
  return fn2(x)
@@ -151,10 +151,21 @@ def test_lazy_init():
151
151
  def fn2(x: object) -> object:
152
152
  return fn1(x)
153
153
 
154
- assert type(object.__getattribute__(fn1, "_getattribute")) is MethodType
155
- with pytest.raises(AttributeError):
156
- object.__getattribute__(fn1, "fn_hash")
157
- assert fn1.fn_hash == object.__getattribute__(fn1, "fn_hash")
158
- assert type(object.__getattribute__(fn1, "_getattribute")) is MethodWrapperType
154
+ assert set(fn1.depends) == {fn1.fn, fn2}
155
+ assert set(fn2.depends) == {fn1, fn2.fn}
156
+
157
+ def test_lazy_init_2():
158
+ @checkpoint
159
+ def fn1(x: object) -> object:
160
+ return fn2(x)
161
+
162
+ assert set(fn1.depends) == {fn1.fn}
163
+
164
+ @checkpoint
165
+ def fn2(x: object) -> object:
166
+ return fn1(x)
167
+
168
+ assert set(fn1.depends) == {fn1.fn}
169
+ fn1.reinit()
159
170
  assert set(fn1.depends) == {fn1.fn, fn2}
160
171
  assert set(fn2.depends) == {fn1, fn2.fn}
@@ -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 · [![License](https://img.shields.io/badge/license-MIT-blue)](https://github.com/Reddan/checkpointer/blob/master/LICENSE) [![pypi](https://img.shields.io/pypi/v/checkpointer)](https://pypi.org/project/checkpointer/) [![pypi](https://img.shields.io/pypi/pyversions/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.8.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 &middot; [![License](https://img.shields.io/badge/license-MIT-blue)](https://github.com/Reddan/checkpointer/blob/master/LICENSE) [![pypi](https://img.shields.io/pypi/v/checkpointer)](https://pypi.org/project/checkpointer/) [![pypi](https://img.shields.io/pypi/pyversions/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, path) -> bool: ... # Check if a checkpoint exists
207
- def checkpoint_date(self, path) -> datetime: ... # Get the checkpoint's timestamp
208
- def store(self, path, data): ... # Save data to the checkpoint
209
- def load(self, path): ... # Load data from the checkpoint
210
- def delete(self, path): ... # 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=FZSKQVIXj_Enja0253WDUauO8siUHwlzcr4frHMJzB0,6538
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=DM5f1Ci4z7MhDnxK-2Z-V3a3ntzBJqORQvCFROAf2SI,3807
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=S5ayOZE_CyaFQJ-vSgObTanldPzG3gh3NksjNAc7vsk,1282
11
- checkpointer/storages/pickle_storage.py,sha256=idh9sBMdWuyvS220oa_7bAUpc9Xo9v6Ud9aYKGWasUs,1593
12
- checkpointer/storages/storage.py,sha256=_m18Z8TKrdAbi6YYYQmuNOnhna4RB2sJDn1v3liaU3U,721
13
- checkpointer-2.8.1.dist-info/METADATA,sha256=z0SbrcyKvHTOgJ6oq0dshV25fBAsqGjMCl_uWmxSFRM,10600
14
- checkpointer-2.8.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
15
- checkpointer-2.8.1.dist-info/licenses/LICENSE,sha256=9xVsdtv_-uSyY9Xl9yujwAPm4-mjcCLeVy-ljwXEWbo,1059
16
- checkpointer-2.8.1.dist-info/RECORD,,