checkpointer 2.14.0__py3-none-any.whl → 2.14.2__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.
@@ -241,6 +241,16 @@ class CachedFunction(Generic[Fn]):
241
241
  except Exception as ex:
242
242
  raise CheckpointError("Could not load checkpoint") from ex
243
243
 
244
+ @overload
245
+ def get_or(self: Callable[P, Coro[R]], default: R, *args: P.args, **kw: P.kwargs) -> R: ...
246
+ @overload
247
+ def get_or(self: Callable[P, R], default: R, *args: P.args, **kw: P.kwargs) -> R: ...
248
+ def get_or(self, default, *args, **kw):
249
+ try:
250
+ return self.get(*args, **kw) # type: ignore
251
+ except CheckpointError:
252
+ return default
253
+
244
254
  @overload
245
255
  def set(self: Callable[P, Coro[R]], value: AwaitableValue[R], *args: P.args, **kw: P.kwargs): ...
246
256
  @overload
@@ -35,3 +35,9 @@ class MemoryStorage(Storage):
35
35
  for call_hash, (date, _) in list(calldict.items()):
36
36
  if self.expired_dt(date):
37
37
  del calldict[call_hash]
38
+
39
+ def clear(self):
40
+ fn_path = self.fn_dir().parent
41
+ for key in list(item_map.keys()):
42
+ if key.parent == fn_path:
43
+ del item_map[key]
@@ -2,6 +2,7 @@ import pickle
2
2
  import shutil
3
3
  from datetime import datetime
4
4
  from pathlib import Path
5
+ from ..utils import clear_directory
5
6
  from .storage import Storage
6
7
 
7
8
  def filedate(path: Path) -> datetime:
@@ -35,7 +36,7 @@ class PickleStorage(Storage):
35
36
  def cleanup(self, invalidated=True, expired=True):
36
37
  version_path = self.fn_dir()
37
38
  fn_path = version_path.parent
38
- if invalidated:
39
+ if invalidated and fn_path.exists():
39
40
  old_dirs = [path for path in fn_path.iterdir() if path.is_dir() and path != version_path]
40
41
  for path in old_dirs:
41
42
  shutil.rmtree(path)
@@ -47,3 +48,9 @@ class PickleStorage(Storage):
47
48
  count += 1
48
49
  pkl_path.unlink(missing_ok=True)
49
50
  print(f"Removed {count} expired checkpoints for {self.cached_fn.__qualname__}")
51
+ clear_directory(fn_path)
52
+
53
+ def clear(self):
54
+ fn_path = self.fn_dir().parent
55
+ if fn_path.exists():
56
+ shutil.rmtree(fn_path)
@@ -44,4 +44,6 @@ class Storage:
44
44
 
45
45
  def delete(self, call_hash: str) -> None: ...
46
46
 
47
- def cleanup(self, invalidated=True, expired=True): ...
47
+ def cleanup(self, invalidated=True, expired=True) -> None: ...
48
+
49
+ def clear(self) -> None: ...
checkpointer/utils.py CHANGED
@@ -112,3 +112,21 @@ class ContextVar(Generic[T]):
112
112
  yield
113
113
  finally:
114
114
  self.value = old
115
+
116
+ def empty_dirs(path: Path) -> Iterable[Path]:
117
+ nonempty_count = 0
118
+ for child in path.iterdir():
119
+ nonempty_count += 1
120
+ if child.is_dir():
121
+ for grand_child in empty_dirs(child):
122
+ yield grand_child
123
+ nonempty_count -= child == grand_child
124
+ if nonempty_count == 0:
125
+ yield path
126
+
127
+ def clear_directory(path: Path):
128
+ if path.is_dir():
129
+ for file in path.glob("**/.DS_Store"):
130
+ file.unlink()
131
+ for directory in empty_dirs(path):
132
+ directory.rmdir()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: checkpointer
3
- Version: 2.14.0
3
+ Version: 2.14.2
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,18 +1,18 @@
1
1
  checkpointer/__init__.py,sha256=mT706hd-BoTpQXYH_MiqreplONVDvjCWjwwGQ7oRe_8,921
2
- checkpointer/checkpoint.py,sha256=SUHu5ADRtSrapspk2DpBLaYnAQ7yuTc05lUELig_Tpc,9972
2
+ checkpointer/checkpoint.py,sha256=FajtvHHfV3TkbD5fxYq0F1TdiJLWH1eZJUybyPrxAXQ,10330
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
6
6
  checkpointer/object_hash.py,sha256=_VgyTEoe3H6268KBAOFZxSgWHb6otgG-3NzhHyehxe4,8595
7
7
  checkpointer/print_checkpoint.py,sha256=uUQ493fJCaB4nhp4Ox60govSCiBTIPbBX15zt2QiRGo,1356
8
8
  checkpointer/types.py,sha256=GFqbGACdDxzQX3bb2LmF9UxQVWOEisGvdtobnqCBAOA,1129
9
- checkpointer/utils.py,sha256=CF28pGytCWV4URhRDFaIDawX8qmCAAByQp0ZkEwrA6c,3014
9
+ checkpointer/utils.py,sha256=o9RCyUmVL8fUenwQ3VBZ80764cs7lQzbeQ9ZhP5bZ-I,3498
10
10
  checkpointer/storages/__init__.py,sha256=p-r4YrPXn505_S3qLrSXHSlsEtb13w_DFnCt9IiUomk,296
11
- checkpointer/storages/memory_storage.py,sha256=f242kLEsB4TcqEkEBmJGIewJE9mvv9aCAZ0V9pPB2EQ,1110
12
- checkpointer/storages/pickle_storage.py,sha256=burbgX-ICPLNMAF9iU3HRZroxJibKfmgJLqTCm6YbbI,1649
13
- checkpointer/storages/storage.py,sha256=PgpwW_VLkiwD361Kjsz8aoq5FHZLBTqF43yD2mw9dTI,1326
14
- checkpointer-2.14.0.dist-info/METADATA,sha256=MSJ--2X9N_Ug8sd9VhffONSzhIPA_njmzJhCe3fa_l8,11215
15
- checkpointer-2.14.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
16
- checkpointer-2.14.0.dist-info/licenses/ATTRIBUTION.md,sha256=WF6L7-sD4s9t9ytVJOhjhpDoZ6TrWpqE3_bMdDIeJxI,1078
17
- checkpointer-2.14.0.dist-info/licenses/LICENSE,sha256=drXs6vIb7uW49r70UuMz2A1VtOCl626kiTbcmrar1Xo,1072
18
- checkpointer-2.14.0.dist-info/RECORD,,
11
+ checkpointer/storages/memory_storage.py,sha256=1YHZU-WSqvLA_0og53UZY141ziDgnaBub2mDjDaeAj8,1261
12
+ checkpointer/storages/pickle_storage.py,sha256=hgvQN4C0OB--uly6TIGZVaU_AFkjElbAoKjW_skxliA,1844
13
+ checkpointer/storages/storage.py,sha256=JVxdq1DMhbq83bvflvIuW7okE1CQCa7poQPsj3x0ACg,1366
14
+ checkpointer-2.14.2.dist-info/METADATA,sha256=ttVoBFscpAup79wZytwM4hCf0aqDAMWjjhEpQD8oiOY,11215
15
+ checkpointer-2.14.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
16
+ checkpointer-2.14.2.dist-info/licenses/ATTRIBUTION.md,sha256=WF6L7-sD4s9t9ytVJOhjhpDoZ6TrWpqE3_bMdDIeJxI,1078
17
+ checkpointer-2.14.2.dist-info/licenses/LICENSE,sha256=drXs6vIb7uW49r70UuMz2A1VtOCl626kiTbcmrar1Xo,1072
18
+ checkpointer-2.14.2.dist-info/RECORD,,