-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
168 additions
and
53 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
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,37 @@ | ||
# built-in | ||
from typing import Callable | ||
|
||
# app | ||
from .._exceptions import ReasonContractError | ||
from .._types import ExceptionType | ||
from .base import Base | ||
|
||
|
||
class Reason(Base): | ||
exception: ExceptionType = ReasonContractError | ||
|
||
def __init__(self, event: Exception, validator: Callable, *, | ||
message: str = None, exception: ExceptionType = None, debug: bool = False): | ||
""" | ||
Step 1. Set allowed exceptions list. | ||
""" | ||
self.event = event | ||
super().__init__( | ||
validator=validator, | ||
message=message, | ||
exception=exception, | ||
debug=debug, | ||
) | ||
|
||
def patched_function(self, *args, **kwargs): | ||
""" | ||
Step 3. Wrapped function calling. | ||
""" | ||
try: | ||
return self.function(*args, **kwargs) | ||
except self.event as origin: | ||
try: | ||
self.validate(*args, **kwargs) | ||
except self.exception: | ||
raise self.exception from origin | ||
raise |
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
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,24 @@ | ||
# reason | ||
|
||
Checks condition if exception was raised. | ||
|
||
```python | ||
@deal.reason(ZeroDivisionError, lambda a, b: b == 0) | ||
def divide(a, b): | ||
return a / b | ||
``` | ||
|
||
## Motivation | ||
|
||
This is the [@deal.ensure](./ensure.html) for exceptions. It works perfect when it's easy to check correctness of conditions when exception is raised. | ||
|
||
For example, if function `index_of` returns index of the first element that equal to the given element and raises `LookupError` if element is not found, we can check that if `LookupError` is raised, element not in the list: | ||
|
||
```python | ||
@deal.reason(LookupError, lambda items, item: item not in items) | ||
def index_of(items: List[int], item: int) -> int: | ||
for index, el in enumerate(items): | ||
if el == item: | ||
return index | ||
raise LookupError | ||
``` |
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
Oops, something went wrong.