checkpointer 2.14.1__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
@@ -2,22 +2,12 @@ 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:
8
9
  return datetime.fromtimestamp(path.stat().st_mtime)
9
10
 
10
- def empty_dirs(path: Path):
11
- nonempty_count = 0
12
- for child in path.iterdir():
13
- nonempty_count += 1
14
- if child.is_dir():
15
- for grand_child in empty_dirs(child):
16
- yield grand_child
17
- nonempty_count -= child == grand_child
18
- if nonempty_count == 0:
19
- yield path
20
-
21
11
  class PickleStorage(Storage):
22
12
  def get_path(self, call_hash: str):
23
13
  return self.fn_dir() / f"{call_hash[:2]}/{call_hash[2:]}.pkl"
@@ -58,11 +48,7 @@ class PickleStorage(Storage):
58
48
  count += 1
59
49
  pkl_path.unlink(missing_ok=True)
60
50
  print(f"Removed {count} expired checkpoints for {self.cached_fn.__qualname__}")
61
- if fn_path.exists():
62
- for file in fn_path.glob("**/.DS_Store"):
63
- file.unlink()
64
- for directory in empty_dirs(fn_path):
65
- directory.rmdir()
51
+ clear_directory(fn_path)
66
52
 
67
53
  def clear(self):
68
54
  fn_path = self.fn_dir().parent
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.1
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
11
  checkpointer/storages/memory_storage.py,sha256=1YHZU-WSqvLA_0og53UZY141ziDgnaBub2mDjDaeAj8,1261
12
- checkpointer/storages/pickle_storage.py,sha256=PwFbCqYHjR3jiWuTnwxCU9cui7CINvq0wXIOKtf8dOk,2230
12
+ checkpointer/storages/pickle_storage.py,sha256=hgvQN4C0OB--uly6TIGZVaU_AFkjElbAoKjW_skxliA,1844
13
13
  checkpointer/storages/storage.py,sha256=JVxdq1DMhbq83bvflvIuW7okE1CQCa7poQPsj3x0ACg,1366
14
- checkpointer-2.14.1.dist-info/METADATA,sha256=KupseJCcWRNym_Ckit_F-oGLPIgqUsyKJvjl6ekEDZU,11215
15
- checkpointer-2.14.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
16
- checkpointer-2.14.1.dist-info/licenses/ATTRIBUTION.md,sha256=WF6L7-sD4s9t9ytVJOhjhpDoZ6TrWpqE3_bMdDIeJxI,1078
17
- checkpointer-2.14.1.dist-info/licenses/LICENSE,sha256=drXs6vIb7uW49r70UuMz2A1VtOCl626kiTbcmrar1Xo,1072
18
- checkpointer-2.14.1.dist-info/RECORD,,
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,,