checkpointer 2.14.3__py3-none-any.whl → 2.14.5__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/__init__.py CHANGED
@@ -5,6 +5,7 @@ from typing import Callable
5
5
  from .checkpoint import CachedFunction, Checkpointer, CheckpointError, FunctionIdent
6
6
  from .object_hash import ObjectHash
7
7
  from .storages import MemoryStorage, PickleStorage, Storage
8
+ from .storages.memory_storage import cleanup_memory_storage
8
9
  from .types import AwaitableValue, Captured, CapturedOnce, CaptureMe, CaptureMeOnce, HashBy, NoHash
9
10
 
10
11
  checkpoint = Checkpointer()
@@ -25,7 +25,7 @@ class CheckpointerOpts(TypedDict, total=False):
25
25
  directory: Path | str | None
26
26
  when: bool
27
27
  verbosity: Literal[0, 1, 2]
28
- expiry: Callable[[datetime], bool] | timedelta | None
28
+ expiry: timedelta | Callable[[datetime], bool] | None
29
29
  capture: bool
30
30
  fn_hash_from: object
31
31
 
@@ -127,11 +127,8 @@ class FunctionIdent:
127
127
 
128
128
  class CachedFunction(Generic[Fn]):
129
129
  def __init__(self, checkpointer: Checkpointer, fn: Fn):
130
- store_format = checkpointer.storage
131
- Storage = STORAGE_MAP[store_format] if isinstance(store_format, str) else store_format
132
130
  update_wrapper(self, unwrap(fn)) # type: ignore
133
131
  self.ident = FunctionIdent(self, checkpointer, fn)
134
- self.storage = Storage(self)
135
132
  self.bound = ()
136
133
 
137
134
  @overload
@@ -154,6 +151,12 @@ class CachedFunction(Generic[Fn]):
154
151
  def fn(self) -> Fn:
155
152
  return self.ident.fn # type: ignore
156
153
 
154
+ @cached_property
155
+ def storage(self) -> Storage:
156
+ store_format = self.ident.checkpointer.storage
157
+ Storage = STORAGE_MAP[store_format] if isinstance(store_format, str) else store_format
158
+ return Storage(self)
159
+
157
160
  @property
158
161
  def cleanup(self):
159
162
  return self.storage.cleanup
@@ -1,11 +1,23 @@
1
- from typing import Any
2
- from pathlib import Path
1
+ from __future__ import annotations
2
+ import gc
3
3
  from datetime import datetime
4
+ from pathlib import Path
5
+ from typing import TYPE_CHECKING, Any
6
+ from weakref import WeakSet
4
7
  from .storage import Storage
5
8
 
9
+ if TYPE_CHECKING:
10
+ from ..checkpoint import CachedFunction
11
+
6
12
  item_map: dict[Path, dict[str, tuple[datetime, Any]]] = {}
13
+ mem_stores: WeakSet[MemoryStorage] = WeakSet()
7
14
 
8
15
  class MemoryStorage(Storage):
16
+ def __init__(self, cached_fn: CachedFunction):
17
+ super().__init__(cached_fn)
18
+ self.cleanup()
19
+ mem_stores.add(self)
20
+
9
21
  def get_dict(self):
10
22
  return item_map.setdefault(self.fn_dir(), {})
11
23
 
@@ -41,3 +53,11 @@ class MemoryStorage(Storage):
41
53
  for key in list(item_map.keys()):
42
54
  if key.parent == fn_path:
43
55
  del item_map[key]
56
+
57
+ def cleanup_memory_storage():
58
+ gc.collect()
59
+ storage_keys = {store.fn_dir() for store in mem_stores}
60
+ for key in item_map.keys() - storage_keys:
61
+ del item_map[key]
62
+ for store in mem_stores:
63
+ store.cleanup()
@@ -54,14 +54,16 @@ class PickleStorage(Storage):
54
54
  old_dirs = [path for path in fn_path.iterdir() if path.is_dir() and path != version_path]
55
55
  for path in old_dirs:
56
56
  shutil.rmtree(path)
57
- print(f"Removed {len(old_dirs)} invalidated directories for {self.cached_fn.__qualname__}")
57
+ if old_dirs:
58
+ print(f"Removed {len(old_dirs)} invalidated directories for {self.cached_fn.__qualname__}")
58
59
  if expired and self.checkpointer.expiry:
59
60
  count = 0
60
61
  for pkl_path in fn_path.glob("**/*.pkl"):
61
62
  if self.expired_dt(filedate(pkl_path)):
62
63
  count += 1
63
64
  pkl_path.unlink(missing_ok=True)
64
- print(f"Removed {count} expired checkpoints for {self.cached_fn.__qualname__}")
65
+ if count:
66
+ print(f"Removed {count} expired checkpoints for {self.cached_fn.__qualname__}")
65
67
  clear_directory(fn_path)
66
68
 
67
69
  def clear(self):
@@ -1,10 +1,10 @@
1
1
  from __future__ import annotations
2
- from typing import Any, TYPE_CHECKING
3
- from pathlib import Path
4
2
  from datetime import datetime, timedelta
3
+ from pathlib import Path
4
+ from typing import TYPE_CHECKING, Any
5
5
 
6
6
  if TYPE_CHECKING:
7
- from ..checkpoint import Checkpointer, CachedFunction
7
+ from ..checkpoint import CachedFunction, Checkpointer
8
8
 
9
9
  class Storage:
10
10
  checkpointer: Checkpointer
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: checkpointer
3
- Version: 2.14.3
3
+ Version: 2.14.5
4
4
  Summary: checkpointer adds code-aware caching to Python functions, maintaining correctness and speeding up execution as your code changes.
5
5
  Project-URL: Repository, https://github.com/Reddan/checkpointer.git
6
6
  Author: Hampus Hallman
@@ -1,5 +1,5 @@
1
- checkpointer/__init__.py,sha256=mT706hd-BoTpQXYH_MiqreplONVDvjCWjwwGQ7oRe_8,921
2
- checkpointer/checkpoint.py,sha256=N3MtNF4Oq3FV69LCfIuyTfNxrcnw9-dGzX312eYigVQ,10454
1
+ checkpointer/__init__.py,sha256=lprhgo4BcVbr4RczWdkNIWroVX_9M-akZcMbxOAWHf0,981
2
+ checkpointer/checkpoint.py,sha256=Vw0uKPBtp01Rit-ydE28TrMO3t3HQ2dkT0uJT2gtyms,10509
3
3
  checkpointer/fn_ident.py,sha256=Z2HtkoG8n0ddqNzjejnKW6Lo-ID2unKQZa1tOpRGujs,6911
4
4
  checkpointer/fn_string.py,sha256=YldjAU91cwiZNwuE_AQN_DMiQCPAhGiiC3lPi0uvM0g,2579
5
5
  checkpointer/import_mappings.py,sha256=ESqWvZTzYAmaVnJ6NulUvn3_8CInOOPmEKUXO2WD_WA,1794
@@ -8,11 +8,11 @@ checkpointer/print_checkpoint.py,sha256=uUQ493fJCaB4nhp4Ox60govSCiBTIPbBX15zt2Qi
8
8
  checkpointer/types.py,sha256=GFqbGACdDxzQX3bb2LmF9UxQVWOEisGvdtobnqCBAOA,1129
9
9
  checkpointer/utils.py,sha256=BAv_qvcanbYqgr7cCOCKNiG0_rGbNbLomBbpvoYlltc,3553
10
10
  checkpointer/storages/__init__.py,sha256=p-r4YrPXn505_S3qLrSXHSlsEtb13w_DFnCt9IiUomk,296
11
- checkpointer/storages/memory_storage.py,sha256=1YHZU-WSqvLA_0og53UZY141ziDgnaBub2mDjDaeAj8,1261
12
- checkpointer/storages/pickle_storage.py,sha256=WCbWGdDH046_GrlnttcqqGZ55wGOz2Rb7Gu0slI448g,2203
13
- checkpointer/storages/storage.py,sha256=JVxdq1DMhbq83bvflvIuW7okE1CQCa7poQPsj3x0ACg,1366
14
- checkpointer-2.14.3.dist-info/METADATA,sha256=YehES3ivV7IqJfs3jKVNCfrgRZwdEySFJrwqem8MVPo,11215
15
- checkpointer-2.14.3.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
16
- checkpointer-2.14.3.dist-info/licenses/ATTRIBUTION.md,sha256=WF6L7-sD4s9t9ytVJOhjhpDoZ6TrWpqE3_bMdDIeJxI,1078
17
- checkpointer-2.14.3.dist-info/licenses/LICENSE,sha256=drXs6vIb7uW49r70UuMz2A1VtOCl626kiTbcmrar1Xo,1072
18
- checkpointer-2.14.3.dist-info/RECORD,,
11
+ checkpointer/storages/memory_storage.py,sha256=M5O3gRMHBiG3CgMcFdA6-iDGvfafKqTi9nN3u8sETCg,1801
12
+ checkpointer/storages/pickle_storage.py,sha256=3_it1YQL6y2WQwnOHT_UPZ1BQkVJZvuR9txcuiuPwYQ,2242
13
+ checkpointer/storages/storage.py,sha256=0dwxQvg0HXlgU93FRXrpZnF_Qb6-KE0WyWhS_GJmmm0,1366
14
+ checkpointer-2.14.5.dist-info/METADATA,sha256=vNYVwNi98WgyKujBVSLhnkWqsx3VMo7KHhgOiWe-i9w,11215
15
+ checkpointer-2.14.5.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
16
+ checkpointer-2.14.5.dist-info/licenses/ATTRIBUTION.md,sha256=WF6L7-sD4s9t9ytVJOhjhpDoZ6TrWpqE3_bMdDIeJxI,1078
17
+ checkpointer-2.14.5.dist-info/licenses/LICENSE,sha256=drXs6vIb7uW49r70UuMz2A1VtOCl626kiTbcmrar1Xo,1072
18
+ checkpointer-2.14.5.dist-info/RECORD,,