Skip to content

Commit 86f330d

Browse files
authored
Merge pull request #38 from pomponchik/develop
0.0.27
2 parents fcba83c + fc66dac commit 86f330d

File tree

10 files changed

+255
-49
lines changed

10 files changed

+255
-49
lines changed

docs/types_of_tokens/ConditionToken.md

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ token = ConditionToken(lambda: counter >= 5)
99
while token:
1010
counter += 1
1111

12-
print(counter) # 5
12+
print(counter) #> 5
1313
```
1414

1515
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
2727
```python
2828
def function(): raise ValueError
2929

30-
print(ConditionToken(function).cancelled) # False
31-
print(ConditionToken(function, default=False).cancelled) # False
32-
print(ConditionToken(function, default=True).cancelled) # True
30+
print(ConditionToken(function).cancelled) #> False
31+
print(ConditionToken(function, default=False).cancelled) #> False
32+
print(ConditionToken(function, default=True).cancelled) #> True
3333
```
3434

3535
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
4040
token = ConditionToken(lambda: print(2), before=lambda: print(1))
4141

4242
token.check()
43-
# Will be printed:
44-
# 1
45-
# 2
43+
#> 1
44+
#> 2
4645
```
4746

4847
By analogy with `before`, you can pass a function that will be executed after checking the condition as the `after` argument:
@@ -51,9 +50,8 @@ By analogy with `before`, you can pass a function that will be executed after ch
5150
token = ConditionToken(lambda: print(1), after=lambda: print(2))
5251

5352
token.check()
54-
# Will be printed:
55-
# 1
56-
# 2
53+
#> 1
54+
#> 2
5755
```
5856

5957
`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.
@@ -70,11 +68,11 @@ def increment_counter_and_get_the_value():
7068
token = ConditionToken(increment_counter_and_get_the_value, caching=False)
7169

7270
print(token.cancelled)
73-
# False
71+
#> False
7472
print(token.cancelled)
75-
# True
73+
#> True
7674
print(token.cancelled)
77-
# False
75+
#> False
7876
```
7977

8078
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.

docs/types_of_tokens/CounterToken.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ counter = 0
1111
while token:
1212
counter += 1
1313

14-
print(counter) # 5
14+
print(counter) #> 5
1515
```
1616

1717
The counter inside the `CounterToken` is reduced under one of three conditions:
@@ -28,9 +28,9 @@ from cantok import SimpleToken, CounterToken
2828
first_counter_token = CounterToken(1, direct=False)
2929
second_counter_token = CounterToken(1, direct=True)
3030

31-
print(SimpleToken(first_counter_token, second_counter_token).cancelled) # False
32-
print(first_counter_token.cancelled) # True
33-
print(second_counter_token.cancelled) # False
31+
print(SimpleToken(first_counter_token, second_counter_token).cancelled) #> False
32+
print(first_counter_token.cancelled) #> True
33+
print(second_counter_token.cancelled) #> False
3434
```
3535

3636
Like all other tokens, `CounterToken` can accept other tokens as parameters during initialization:

docs/types_of_tokens/SimpleToken.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ The base token is `SimpleToken`. It has no built-in automation that can cancel i
44
from cantok import SimpleToken
55

66
token = SimpleToken()
7-
print(token.cancelled) # False
7+
print(token.cancelled) #> False
88
token.cancel()
9-
print(token.cancelled) # True
9+
print(token.cancelled) #> True
1010
```
1111

1212
`SimpleToken` is also implicitly generated by the operation of summing two other tokens:
@@ -15,7 +15,7 @@ print(token.cancelled) # True
1515
from cantok import CounterToken, TimeoutToken
1616

1717
print(repr(CounterToken(5) + TimeoutToken(5)))
18-
# SimpleToken(CounterToken(5, direct=True), TimeoutToken(5))
18+
#> SimpleToken(CounterToken(5, direct=True), TimeoutToken(5))
1919
```
2020

2121
There is not much more to tell about it if you have read [the story](../what_are_tokens/in_general.md) about tokens in general.

docs/types_of_tokens/TimeoutToken.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ from time import sleep
55
from cantok import TimeoutToken
66

77
token = TimeoutToken(5)
8-
print(token.cancelled) # False
8+
print(token.cancelled) #> False
99
sleep(10)
10-
print(token.cancelled) # True
10+
print(token.cancelled) #> True
1111
```
1212

1313
Just like `ConditionToken`, `TimeoutToken` can include other tokens:

docs/what_are_tokens/cancel_and_read_the_status.md

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ Each token object has a `cancelled` attribute and a `cancel()` method. By the at
44
from cantok import SimpleToken
55

66
token = SimpleToken()
7-
print(token.cancelled) # False
7+
print(token.cancelled) #> False
88
token.cancel()
9-
print(token.cancelled) # True
9+
print(token.cancelled) #> True
1010
```
1111

1212
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
1616
from cantok import TimeoutToken
1717

1818
token = TimeoutToken(5)
19-
print(token.cancelled) # False
19+
print(token.cancelled) #> False
2020
sleep(10)
21-
print(token.cancelled) # True
21+
print(token.cancelled) #> True
2222
```
2323

2424
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
2727
from cantok import SimpleToken
2828

2929
token = SimpleToken()
30-
print(token.cancelled) # False
31-
print(token.is_cancelled()) # False
30+
print(token.cancelled) #> False
31+
print(token.is_cancelled()) #> False
3232
token.cancel()
33-
print(token.cancelled) # True
34-
print(token.is_cancelled()) # True
33+
print(token.cancelled) #> True
34+
print(token.is_cancelled()) #> True
3535
```
3636

3737
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
4242
from cantok import SimpleToken
4343

4444
token = SimpleToken()
45-
print(token.cancelled) # False
46-
print(token.keep_on()) # True
45+
print(token.cancelled) #> False
46+
print(token.keep_on()) #> True
4747
token.cancel()
48-
print(token.cancelled) # True
49-
print(token.keep_on()) # False
48+
print(token.cancelled) #> True
49+
print(token.keep_on()) #> False
5050
```
5151

5252
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
5555
from cantok import SimpleToken
5656

5757
token = SimpleToken()
58-
print(bool(token)) # True
59-
print(token.keep_on()) # True
58+
print(bool(token)) #> True
59+
print(token.keep_on()) #> True
6060
token.cancel()
61-
print(bool(token)) # False
62-
print(token.keep_on()) # False
61+
print(bool(token)) #> False
62+
print(token.keep_on()) #> False
6363
```
6464

6565
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
7070
token = SimpleToken()
7171
token.check() # Nothing happens.
7272
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.
7476
```
7577

7678
Otherwise, a special exception inherited from `CancellationError` will be raised:
@@ -79,5 +81,7 @@ Otherwise, a special exception inherited from `CancellationError` will be raised
7981
from cantok import TimeoutToken
8082

8183
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.
8387
```

docs/what_are_tokens/embedding.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ third_token = SimpleToken(first_token, second_token)
99

1010
first_token.cancel()
1111

12-
print(first_token.cancelled) # True
13-
print(second_token.cancelled) # False
14-
print(third_token.cancelled) # True
12+
print(first_token.cancelled) #> True
13+
print(second_token.cancelled) #> False
14+
print(third_token.cancelled) #> True
1515
```

docs/what_are_tokens/exceptions.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ from cantok import TimeoutToken
66
token = TimeoutToken(1)
77
token.wait()
88
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.
1011
```
1112

1213
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)
3435
try:
3536
token.check()
3637
except CancellationError as e:
37-
print(type(e) is TimeoutToken.exception) # True
38+
print(type(e) is TimeoutToken.exception) #> True
3839
```
3940

4041
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:
@@ -48,5 +49,5 @@ token = SimpleToken(nested_token)
4849
try:
4950
token.check()
5051
except CancellationError as e:
51-
print(e.token is nested_token) # True
52+
print(e.token is nested_token) #> True
5253
```

docs/what_are_tokens/waiting.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ from cantok import TimeoutToken
66
token = TimeoutToken(5)
77
token.wait() # It will take about 5 seconds.
88
token.check() # Since the timeout has expired, an exception will be raised.
9-
# cantok.errors.TimeoutCancellationError: The timeout of 5 seconds has expired.
9+
#> ...
10+
#> cantok.errors.TimeoutCancellationError: The timeout of 5 seconds has expired.
1011
```
1112

1213
If you add the `await` keyword before calling `wait()`, the method will be automatically used in non-blocking mode:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "cantok"
7-
version = "0.0.26"
7+
version = "0.0.27"
88
authors = [
99
{ name="Evgeniy Blinov", email="zheni-b@yandex.ru" },
1010
]

0 commit comments

Comments
 (0)