You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
By default, if the passed function raises an exception, it will be silently suppressed. However, you can make the raised exceptions explicit by setting the `suppress_exceptions` parameter to `False`:
@@ -27,9 +27,9 @@ If you still use exception suppression mode, by default, in case of an exception
If the condition is complex enough and requires additional preparation before it can be checked, you can pass a function that runs before the condition is checked. To do this, pass any function without arguments as the `before` argument:
@@ -40,9 +40,8 @@ from cantok import ConditionToken
`ConditionToken` has another feature. If the condition has detonated at least once and canceled it, then the condition is no longer polled and the token is permanently considered canceled. You can change this by manipulating the `caching` parameter when creating a token. By setting it to `False`, you will make sure that the condition is polled every time.
However, we do not recommend doing this. In the vast majority of cases, you do not want your token to be able to roll back the fact of its cancellation. If the token has been cancelled once, it must remain cancelled. Manipulate the `caching` parameter only if you are sure that you understand what you are doing.
Copy file name to clipboardExpand all lines: docs/what_are_tokens/cancel_and_read_the_status.md
+22-18Lines changed: 22 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,9 +4,9 @@ Each token object has a `cancelled` attribute and a `cancel()` method. By the at
4
4
from cantok import SimpleToken
5
5
6
6
token = SimpleToken()
7
-
print(token.cancelled) # False
7
+
print(token.cancelled) #> False
8
8
token.cancel()
9
-
print(token.cancelled) # True
9
+
print(token.cancelled) #> True
10
10
```
11
11
12
12
The cancelled attribute is dynamically calculated and takes into account, among other things, specific conditions that are checked by a specific token. Here is an example with a [token that measures time](../types_of_tokens/TimeoutToken.md):
@@ -16,9 +16,9 @@ from time import sleep
16
16
from cantok import TimeoutToken
17
17
18
18
token = TimeoutToken(5)
19
-
print(token.cancelled) # False
19
+
print(token.cancelled) #> False
20
20
sleep(10)
21
-
print(token.cancelled) # True
21
+
print(token.cancelled) #> True
22
22
```
23
23
24
24
In addition to this attribute, each token implements the `is_cancelled()` method. It does exactly the same thing as the attribute:
@@ -27,11 +27,11 @@ In addition to this attribute, each token implements the `is_cancelled()` method
27
27
from cantok import SimpleToken
28
28
29
29
token = SimpleToken()
30
-
print(token.cancelled) # False
31
-
print(token.is_cancelled()) # False
30
+
print(token.cancelled) #> False
31
+
print(token.is_cancelled()) #> False
32
32
token.cancel()
33
-
print(token.cancelled) # True
34
-
print(token.is_cancelled()) # True
33
+
print(token.cancelled) #> True
34
+
print(token.is_cancelled()) #> True
35
35
```
36
36
37
37
Choose what you like best. To the author of the library, the use of the attribute seems more beautiful, but the method call more clearly reflects the complexity of the work that is actually being done to answer the question "has the token been canceled?".
@@ -42,11 +42,11 @@ There is another method opposite to `is_cancelled()` - `keep_on()`. It answers t
42
42
from cantok import SimpleToken
43
43
44
44
token = SimpleToken()
45
-
print(token.cancelled) # False
46
-
print(token.keep_on()) # True
45
+
print(token.cancelled) #> False
46
+
print(token.keep_on()) #> True
47
47
token.cancel()
48
-
print(token.cancelled) # True
49
-
print(token.keep_on()) # False
48
+
print(token.cancelled) #> True
49
+
print(token.keep_on()) #> False
50
50
```
51
51
52
52
You don't have to call the `keep_on()` method directly. Use the token itself as a boolean value, and the method call will occur "under the hood" automatically:
@@ -55,11 +55,11 @@ You don't have to call the `keep_on()` method directly. Use the token itself as
55
55
from cantok import SimpleToken
56
56
57
57
token = SimpleToken()
58
-
print(bool(token)) # True
59
-
print(token.keep_on()) # True
58
+
print(bool(token)) #> True
59
+
print(token.keep_on()) #> True
60
60
token.cancel()
61
-
print(bool(token)) # False
62
-
print(token.keep_on()) # False
61
+
print(bool(token)) #> False
62
+
print(token.keep_on()) #> False
63
63
```
64
64
65
65
There is another method that is close in meaning to `is_cancelled()` - `check()`. It does nothing if the token is not canceled, or raises an exception if canceled. If the token was canceled by calling the `cancel()` method, a `CancellationError` exception will be raised:
@@ -70,7 +70,9 @@ from cantok import SimpleToken
70
70
token = SimpleToken()
71
71
token.check() # Nothing happens.
72
72
token.cancel()
73
-
token.check() # cantok.errors.CancellationError: The token has been cancelled.
73
+
token.check()
74
+
#> ...
75
+
#> cantok.errors.CancellationError: The token has been cancelled.
74
76
```
75
77
76
78
Otherwise, a special exception inherited from `CancellationError` will be raised:
@@ -79,5 +81,7 @@ Otherwise, a special exception inherited from `CancellationError` will be raised
79
81
from cantok import TimeoutToken
80
82
81
83
token = TimeoutToken(0)
82
-
token.check() # cantok.errors.TimeoutCancellationError: The timeout of 0 seconds has expired.
84
+
token.check()
85
+
#> ...
86
+
#> cantok.errors.TimeoutCancellationError: The timeout of 0 seconds has expired.
Copy file name to clipboardExpand all lines: docs/what_are_tokens/exceptions.md
+4-3Lines changed: 4 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,8 @@ from cantok import TimeoutToken
6
6
token = TimeoutToken(1)
7
7
token.wait()
8
8
token.check()
9
-
# cantok.errors.TimeoutCancellationError: The timeout of 1 seconds has expired.
9
+
#> ...
10
+
#> cantok.errors.TimeoutCancellationError: The timeout of 1 seconds has expired.
10
11
```
11
12
12
13
Each type of token (except [`DefaultToken`](../types_of_tokens/DefaultToken.md)) has a corresponding type of exception that can be raised in this case:
@@ -34,7 +35,7 @@ token = TimeoutToken(0)
34
35
try:
35
36
token.check()
36
37
except CancellationError as e:
37
-
print(type(e) is TimeoutToken.exception) # True
38
+
print(type(e) is TimeoutToken.exception) #> True
38
39
```
39
40
40
41
And each exception object has a `token` attribute indicating the specific token that was canceled. This can be useful in situations where several tokens are nested in one another and you want to find out which one has been canceled:
0 commit comments