Skip to content

Commit 7efc7ac

Browse files
committed
Implement decorating without parenthesis
1 parent 1ad4c7c commit 7efc7ac

13 files changed

Lines changed: 66 additions & 6 deletions

File tree

checkpointing/decorator/default.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,17 @@ def checkpoint(
2525
2626
cache_pickle_protocol: the pickle protocol used by the cache to save results to the disk.
2727
If None, use the global default `cache.pickle_protocol`
28+
29+
Optionally user can directly decorate the function with `@checkpoint` (without parenthesis),
30+
this will cause the function to be passed in directly with the `directory` parameter.
2831
"""
2932

33+
# If true, the `directory` is actually the decorated function
34+
# and the actual `directory` is None
35+
used_without_parenthesis = callable(directory)
36+
3037
identifier = AutoFuncCallIdentifier()
31-
cache = PickleFileCache(directory, cache_pickle_protocol)
38+
cache = PickleFileCache(None if used_without_parenthesis else directory, cache_pickle_protocol)
3239
decorator = DecoratorCheckpoint(identifier, cache, on_error)
3340

34-
return decorator
41+
return decorator(directory) if used_without_parenthesis else decorator

docs/changelog.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
## v1.0.x
22

3-
### v1.0.0
3+
### v1.0.1
4+
5+
- Added support to decorate a function without parenthesis (`@checkpoint` instead of `@checkpoint()`)
6+
- Thanks for the proposal in https://github.com/Vopaaz/checkpointing/issues/8
7+
8+
### [v1.0.0](https://checkpointing.readthedocs.io/en/v1.0.0/)
49

510
- Drop support for Python 3.7 to remove the pickle5 dependency
611

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
As requested in https://github.com/Vopaaz/checkpointing/issues/8 we are introducing a new syntax
2+
to decorate with `checkpoint` without parenthesis.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Running
2+
0
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Running
2+
1
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from checkpointing import checkpoint
2+
3+
@checkpoint
4+
def foo(a):
5+
print("Running")
6+
return a
7+
8+
if __name__ == "__main__":
9+
print(foo(0))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from checkpointing import checkpoint
2+
3+
@checkpoint
4+
def foo(a):
5+
print("Running")
6+
return a + 1
7+
8+
if __name__ == "__main__":
9+
print(foo(0))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Running
2+
1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from checkpointing import checkpoint
2+
3+
4+
@checkpoint
5+
def foo(a):
6+
print("Running")
7+
return a
8+
9+
10+
if __name__ == "__main__":
11+
print(foo(1))

0 commit comments

Comments
 (0)