Name | seviper JSON |
Version |
0.1.0
JSON |
| download |
home_page | None |
Summary | A simple and easy to use library to catch and handle exceptions. |
upload_time | 2024-10-01 12:32:07 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.10 |
license | MIT |
keywords |
aiostream
error-handling
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# Seviper - Error handling framework to catch 'em all
![Unittests status badge](https://github.com/Hochfrequenz/seviper/workflows/Unittests/badge.svg)
![Coverage status badge](https://github.com/Hochfrequenz/seviper/workflows/Coverage/badge.svg)
![Linting status badge](https://github.com/Hochfrequenz/seviper/workflows/Linting/badge.svg)
![Black status badge](https://github.com/Hochfrequenz/seviper/workflows/Formatting/badge.svg)
## Features
This framework provides several error handlers to catch errors and call callback functions to handle these errors
(or successes). It comes fully equipped with:
- A decorator to handle errors in functions or coroutines
- A decorator to retry a function or coroutine if it fails (can be useful for network requests)
- A context manager to handle errors in a block of code
Additionally, if you use `aiostream` (e.g. using `pip install seviper[aiostream]`), you can use the following features:
- The `stream.map` (or `pipe.map`, analogous to the `aiostream` functions) function to run the function, catch all
exceptions and call the error handler if an exception occurs. Additionally, filters out all failed results.
## Installation
```bash
pip install seviper
```
or optionally:
```bash
pip install seviper[aiostream]
```
## Usage
Here is a more or less complex example as showcase of the features of this library:
```python
import asyncio
import logging
import sys
import aiostream
import error_handler
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, force=True)
logger = logging.root
op = aiostream.stream.iterate(range(10))
def log_error(error: Exception, num: int):
"""Only log error and reraise it"""
logger.error("double_only_odd_nums_except_5 failed for input %d. ", num)
raise error
@error_handler.decorator(on_error=log_error)
async def double_only_odd_nums_except_5(num: int) -> int:
if num % 2 == 0:
raise ValueError(num)
with error_handler.context_manager(on_success=lambda: logging.info("Success: %s", num)):
if num == 5:
raise RuntimeError("Another unexpected error. Number 5 will not be doubled.")
num *= 2
return num
def catch_value_errors(error: Exception, _: int):
if not isinstance(error, ValueError):
raise error
def log_success(result_num: int, provided_num: int):
logger.info("Success: %d -> %d", provided_num, result_num)
op = op | error_handler.pipe.map(
double_only_odd_nums_except_5,
on_error=catch_value_errors,
on_success=log_success,
wrap_secured_function=True,
suppress_recalling_on_error=False,
)
result = asyncio.run(aiostream.stream.list(op))
assert result == [2, 6, 5, 14, 18]
```
This outputs:
```
ERROR:root:double_only_odd_nums_except_5 failed for input 0.
INFO:root:Success: 2
INFO:root:Success: 1 -> 2
ERROR:root:double_only_odd_nums_except_5 failed for input 2.
INFO:root:Success: 6
INFO:root:Success: 3 -> 6
ERROR:root:double_only_odd_nums_except_5 failed for input 4.
INFO:root:Success: 5 -> 5
ERROR:root:double_only_odd_nums_except_5 failed for input 6.
INFO:root:Success: 14
INFO:root:Success: 7 -> 14
ERROR:root:double_only_odd_nums_except_5 failed for input 8.
INFO:root:Success: 18
INFO:root:Success: 9 -> 18
```
## How to use this Repository on Your Machine
Please refer to the respective section in our [Python template repository](https://github.com/Hochfrequenz/python_template_repository?tab=readme-ov-file#how-to-use-this-repository-on-your-machine)
to learn how to use this repository on your machine.
## Contribute
You are very welcome to contribute to this template repository by opening a pull request against the main branch.
Raw data
{
"_id": null,
"home_page": null,
"name": "seviper",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": "aiostream, error-handling",
"author": null,
"author_email": "Leon Haffmans <leon.haffmans@hochfrequenz.de>",
"download_url": "https://files.pythonhosted.org/packages/aa/59/3b357a68daa39cfb72fb0b729a4b13d1c61fc684f9db5cbc92ec2f73aa51/seviper-0.1.0.tar.gz",
"platform": null,
"description": "# Seviper - Error handling framework to catch 'em all\n\n![Unittests status badge](https://github.com/Hochfrequenz/seviper/workflows/Unittests/badge.svg)\n![Coverage status badge](https://github.com/Hochfrequenz/seviper/workflows/Coverage/badge.svg)\n![Linting status badge](https://github.com/Hochfrequenz/seviper/workflows/Linting/badge.svg)\n![Black status badge](https://github.com/Hochfrequenz/seviper/workflows/Formatting/badge.svg)\n\n## Features\nThis framework provides several error handlers to catch errors and call callback functions to handle these errors\n(or successes). It comes fully equipped with:\n\n- A decorator to handle errors in functions or coroutines\n- A decorator to retry a function or coroutine if it fails (can be useful for network requests)\n- A context manager to handle errors in a block of code\n\nAdditionally, if you use `aiostream` (e.g. using `pip install seviper[aiostream]`), you can use the following features:\n\n- The `stream.map` (or `pipe.map`, analogous to the `aiostream` functions) function to run the function, catch all\n exceptions and call the error handler if an exception occurs. Additionally, filters out all failed results.\n\n## Installation\n\n```bash\npip install seviper\n```\n\nor optionally:\n\n```bash\npip install seviper[aiostream]\n```\n\n## Usage\nHere is a more or less complex example as showcase of the features of this library:\n\n```python\nimport asyncio\nimport logging\nimport sys\nimport aiostream\nimport error_handler\n\nlogging.basicConfig(stream=sys.stdout, level=logging.DEBUG, force=True)\nlogger = logging.root\nop = aiostream.stream.iterate(range(10))\n\n\ndef log_error(error: Exception, num: int):\n \"\"\"Only log error and reraise it\"\"\"\n logger.error(\"double_only_odd_nums_except_5 failed for input %d. \", num)\n raise error\n\n\n@error_handler.decorator(on_error=log_error)\nasync def double_only_odd_nums_except_5(num: int) -> int:\n if num % 2 == 0:\n raise ValueError(num)\n with error_handler.context_manager(on_success=lambda: logging.info(\"Success: %s\", num)):\n if num == 5:\n raise RuntimeError(\"Another unexpected error. Number 5 will not be doubled.\")\n num *= 2\n return num\n\n\ndef catch_value_errors(error: Exception, _: int):\n if not isinstance(error, ValueError):\n raise error\n\n\ndef log_success(result_num: int, provided_num: int):\n logger.info(\"Success: %d -> %d\", provided_num, result_num)\n\n\nop = op | error_handler.pipe.map(\n double_only_odd_nums_except_5,\n on_error=catch_value_errors,\n on_success=log_success,\n wrap_secured_function=True,\n suppress_recalling_on_error=False,\n)\n\nresult = asyncio.run(aiostream.stream.list(op))\n\nassert result == [2, 6, 5, 14, 18]\n```\n\nThis outputs:\n\n```\nERROR:root:double_only_odd_nums_except_5 failed for input 0.\nINFO:root:Success: 2\nINFO:root:Success: 1 -> 2\nERROR:root:double_only_odd_nums_except_5 failed for input 2.\nINFO:root:Success: 6\nINFO:root:Success: 3 -> 6\nERROR:root:double_only_odd_nums_except_5 failed for input 4.\nINFO:root:Success: 5 -> 5\nERROR:root:double_only_odd_nums_except_5 failed for input 6.\nINFO:root:Success: 14\nINFO:root:Success: 7 -> 14\nERROR:root:double_only_odd_nums_except_5 failed for input 8.\nINFO:root:Success: 18\nINFO:root:Success: 9 -> 18\n```\n\n## How to use this Repository on Your Machine\n\nPlease refer to the respective section in our [Python template repository](https://github.com/Hochfrequenz/python_template_repository?tab=readme-ov-file#how-to-use-this-repository-on-your-machine)\nto learn how to use this repository on your machine.\n\n## Contribute\n\nYou are very welcome to contribute to this template repository by opening a pull request against the main branch.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A simple and easy to use library to catch and handle exceptions.",
"version": "0.1.0",
"project_urls": {
"Changelog": "https://github.com/Hochfrequenz/seviper/releases",
"Homepage": "https://github.com/Hochfrequenz/seviper"
},
"split_keywords": [
"aiostream",
" error-handling"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "661c8a8e24b13421d76daeb0c7d45e09d1d6c564c6c71f363d4346fd07242fec",
"md5": "5bdd64a9e137a3bf4534eeda73fa38e4",
"sha256": "16c50d94368b3a15eae4ebd59618e7e8cac4ad93829be322e4e8d84cd59f5727"
},
"downloads": -1,
"filename": "seviper-0.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "5bdd64a9e137a3bf4534eeda73fa38e4",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 17399,
"upload_time": "2024-10-01T12:32:06",
"upload_time_iso_8601": "2024-10-01T12:32:06.419576Z",
"url": "https://files.pythonhosted.org/packages/66/1c/8a8e24b13421d76daeb0c7d45e09d1d6c564c6c71f363d4346fd07242fec/seviper-0.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "aa593b357a68daa39cfb72fb0b729a4b13d1c61fc684f9db5cbc92ec2f73aa51",
"md5": "f45e34ab58a2cfd06b4a9bdb7e25cd48",
"sha256": "e285381d22d1a5f8ade0c3391832e875eb6c6b13ff36789614590470b87b7e42"
},
"downloads": -1,
"filename": "seviper-0.1.0.tar.gz",
"has_sig": false,
"md5_digest": "f45e34ab58a2cfd06b4a9bdb7e25cd48",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 19247,
"upload_time": "2024-10-01T12:32:07",
"upload_time_iso_8601": "2024-10-01T12:32:07.811836Z",
"url": "https://files.pythonhosted.org/packages/aa/59/3b357a68daa39cfb72fb0b729a4b13d1c61fc684f9db5cbc92ec2f73aa51/seviper-0.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-01 12:32:07",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Hochfrequenz",
"github_project": "seviper",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"tox": true,
"lcname": "seviper"
}