seviper


Nameseviper JSON
Version 0.0.4 PyPI version JSON
download
home_pageNone
SummaryA simple and easy to use library to catch and handle exceptions.
upload_time2024-05-03 11:43:06
maintainerNone
docs_urlNone
authorNone
requires_python>=3.11
licenseMIT
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.11",
    "maintainer_email": null,
    "keywords": "aiostream, error-handling",
    "author": null,
    "author_email": "Leon Haffmans <leon.haffmans@hochfrequenz.de>",
    "download_url": "https://files.pythonhosted.org/packages/f1/b7/9cacbbbf3b57ca5df11524d8fbe8b672deedc3e984da2d010eabe4805038/seviper-0.0.4.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.0.4",
    "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": "5aabd43e225ef9c425e19f7a4e96a8c0815c82939b2591a99d502dc5be343bbf",
                "md5": "f5819258d90bca84c617a212e2f64ada",
                "sha256": "97c1e2e39de81dc3229eee877fe2feb822b050b2c35defc7d61e92a60f062052"
            },
            "downloads": -1,
            "filename": "seviper-0.0.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f5819258d90bca84c617a212e2f64ada",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11",
            "size": 16935,
            "upload_time": "2024-05-03T11:43:05",
            "upload_time_iso_8601": "2024-05-03T11:43:05.046445Z",
            "url": "https://files.pythonhosted.org/packages/5a/ab/d43e225ef9c425e19f7a4e96a8c0815c82939b2591a99d502dc5be343bbf/seviper-0.0.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f1b79cacbbbf3b57ca5df11524d8fbe8b672deedc3e984da2d010eabe4805038",
                "md5": "f1948a5d426e48d9dddff9aa8afa2ac5",
                "sha256": "826c7d24a6ead8653a46350b6c0f0e0d3fd5646d21aee00898534940ddef817c"
            },
            "downloads": -1,
            "filename": "seviper-0.0.4.tar.gz",
            "has_sig": false,
            "md5_digest": "f1948a5d426e48d9dddff9aa8afa2ac5",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11",
            "size": 19965,
            "upload_time": "2024-05-03T11:43:06",
            "upload_time_iso_8601": "2024-05-03T11:43:06.683587Z",
            "url": "https://files.pythonhosted.org/packages/f1/b7/9cacbbbf3b57ca5df11524d8fbe8b672deedc3e984da2d010eabe4805038/seviper-0.0.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-03 11:43:06",
    "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"
}
        
Elapsed time: 0.25066s