-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from pomponchik/develop
0.0.23
- Loading branch information
Showing
16 changed files
with
196 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from cantok import AbstractToken | ||
from cantok.errors import ImpossibleCancelError | ||
|
||
|
||
class DefaultToken(AbstractToken): | ||
exception = ImpossibleCancelError | ||
|
||
def __init__(self) -> None: | ||
super().__init__() | ||
|
||
def superpower(self) -> bool: | ||
return False | ||
|
||
def text_representation_of_superpower(self) -> str: | ||
return '' | ||
|
||
def get_superpower_exception_message(self) -> str: | ||
return 'You cannot cancel a default token.' # pragma: no cover | ||
|
||
@property | ||
def cancelled(self) -> bool: | ||
return False | ||
|
||
@cancelled.setter | ||
def cancelled(self, new_value: bool) -> None: | ||
if new_value == True: | ||
self.raise_superpower_exception() | ||
|
||
def keep_on(self) -> bool: | ||
return True | ||
|
||
def is_cancelled(self, direct: bool = True) -> bool: | ||
return False | ||
|
||
def cancel(self) -> 'AbstractToken': # type: ignore[return] | ||
self.raise_superpower_exception() |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
To run a function regularly, use [metronomes](https://github.com/pomponchik/metronomes). Just wrap your code in a context manager: | ||
|
||
```python | ||
from time import sleep | ||
from metronomes import Metronome | ||
|
||
with Metronome(0.2, lambda: print('go!')): | ||
sleep(1) | ||
#> go! | ||
#> go! | ||
#> go! | ||
#> go! | ||
#> go! | ||
``` | ||
|
||
You can also manually control the start and stop of the metronome: | ||
|
||
```python | ||
metronome = Metronome(0.2, lambda: print('go!')) | ||
|
||
metronome.start() | ||
sleep(1) | ||
metronome.stop() | ||
#> go! | ||
#> go! | ||
#> go! | ||
#> go! | ||
#> go! | ||
``` | ||
|
||
And of course, the cancellation token can be used as an optional argument: | ||
|
||
```python | ||
from cantok import TimeoutToken | ||
|
||
metronome = Metronome(0.2, lambda: None, token=TimeoutToken(1)) | ||
|
||
metronome.start() | ||
print(metronome.stopped) | ||
#> False | ||
sleep(1.5) # Here I specify a little more time than in the constructor of the token itself, since a small margin is needed for operations related to the creation of the metronome object itself. | ||
print(metronome.stopped) | ||
#> True | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
`DefaultToken` is a type of token that cannot be revoked. Otherwise, it behaves like a regular token, but if you try to cancel it, you will get an exception: | ||
|
||
```python | ||
from cantok import AbstractToken, DefaultToken | ||
|
||
DefaultToken().cancel() | ||
#> ... | ||
#> cantok.errors.ImpossibleCancelError: You cannot cancel a default token. | ||
``` | ||
|
||
In addition, you cannot embed other tokens in `DefaultToken`. | ||
|
||
It is best to use `DefaultToken` as the default argument for functions: | ||
|
||
```python | ||
def function(token: AbstractToken = DefaultToken()): | ||
... | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ mypy==1.4.1 | |
ruff==0.0.290 | ||
mkdocs-material==9.2.7 | ||
mutmut==2.4.4 | ||
full_match==0.0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,24 @@ | ||
import pytest | ||
|
||
from cantok import AbstractToken, SimpleToken, ConditionToken, TimeoutToken, CounterToken | ||
from cantok import CancellationError, ConditionCancellationError, TimeoutCancellationError, CounterCancellationError | ||
from cantok import AbstractToken, SimpleToken, ConditionToken, TimeoutToken, CounterToken, DefaultToken | ||
from cantok import CancellationError, ConditionCancellationError, TimeoutCancellationError, CounterCancellationError, ImpossibleCancelError | ||
|
||
|
||
def test_exception_inheritance_hierarchy(): | ||
assert issubclass(ConditionCancellationError, CancellationError) | ||
assert issubclass(TimeoutCancellationError, CancellationError) | ||
assert issubclass(CounterCancellationError, CancellationError) | ||
assert issubclass(ImpossibleCancelError, CancellationError) | ||
|
||
|
||
def test_exception_inheritance_hierarchy_from_view_of_tokens_classes(): | ||
assert issubclass(ConditionToken.exception, SimpleToken.exception) | ||
assert issubclass(TimeoutToken.exception, SimpleToken.exception) | ||
assert issubclass(CounterToken.exception, SimpleToken.exception) | ||
assert issubclass(DefaultToken.exception, SimpleToken.exception) | ||
|
||
assert SimpleToken.exception is CancellationError | ||
assert ConditionToken.exception is ConditionCancellationError | ||
assert TimeoutToken.exception is TimeoutCancellationError | ||
assert CounterToken.exception is CounterCancellationError | ||
assert DefaultToken.exception is ImpossibleCancelError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import sys | ||
|
||
import pytest | ||
import full_match | ||
|
||
from cantok import DefaultToken, SimpleToken, ImpossibleCancelError | ||
|
||
|
||
def test_dafault_token_is_not_cancelled_by_default(): | ||
token = DefaultToken() | ||
|
||
assert bool(token) | ||
assert token.cancelled == False | ||
assert token.is_cancelled() == False | ||
assert token.keep_on() == True | ||
|
||
token.check() | ||
|
||
|
||
def test_you_can_set_cancelled_attribute_as_false(): | ||
token = DefaultToken() | ||
|
||
token.cancelled = False | ||
|
||
assert bool(token) | ||
assert token.cancelled == False | ||
assert token.is_cancelled() == False | ||
assert token.keep_on() == True | ||
|
||
token.check() | ||
|
||
|
||
def test_you_cant_set_true_as_cancelled_attribute(): | ||
token = DefaultToken() | ||
|
||
with pytest.raises(ImpossibleCancelError, match=full_match('You cannot cancel a default token.')): | ||
token.cancelled = True | ||
|
||
assert token.cancelled == False | ||
|
||
|
||
def test_you_cannot_cancel_default_token_by_standard_way(): | ||
token = DefaultToken() | ||
|
||
with pytest.raises(ImpossibleCancelError, match=full_match('You cannot cancel a default token.')): | ||
token.cancel() | ||
|
||
assert token.cancelled == False | ||
|
||
|
||
def test_str_for_default_token(): | ||
assert str(DefaultToken()) == '<DefaultToken (not cancelled)>' | ||
|
||
|
||
@pytest.mark.skipif(sys.version_info >= (3, 10), reason='Format of this exception messages was changed.') | ||
def test_you_cannot_neste_another_token_to_default_one_old_pythons(): | ||
with pytest.raises(TypeError, match=full_match('__init__() takes 1 positional argument but 2 were given')): | ||
DefaultToken(SimpleToken(TypeError)) | ||
|
||
|
||
@pytest.mark.skipif(sys.version_info < (3, 10), reason='Format of this exception messages was changed.') | ||
def test_you_cannot_neste_another_token_to_default_one_new_pythons(): | ||
with pytest.raises(TypeError, match=full_match('DefaultToken.__init__() takes 1 positional argument but 2 were given')): | ||
DefaultToken(SimpleToken(TypeError)) |