gqylpy-exception


Namegqylpy-exception JSON
Version 3.1.2 PyPI version JSON
download
home_pagehttp://gqylpy.com
Summary`gqylpy-exception` is a flexible and convenient Python exception handling library that allows you to dynamically create exception classes and provides various exception handling mechanisms.
upload_time2024-07-15 01:31:47
maintainerNone
docs_urlNone
author竹永康
requires_python>=3.8
licenseApache 2.0
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [<img alt="LOGO" src="http://gqylpy.com/static/img/favicon.ico" height="21" width="21"/>](http://www.gqylpy.com)
[![Release](https://img.shields.io/github/release/gqylpy/gqylpy-exception.svg?style=flat-square")](https://github.com/gqylpy/gqylpy-exception/releases/latest)
[![Python Versions](https://img.shields.io/pypi/pyversions/gqylpy_exception)](https://pypi.org/project/gqylpy_exception)
[![License](https://img.shields.io/pypi/l/gqylpy_exception)](https://github.com/gqylpy/gqylpy-exception/blob/master/LICENSE)
[![Downloads](https://static.pepy.tech/badge/gqylpy_exception)](https://pepy.tech/project/gqylpy_exception)

# gqylpy-exception
English | [中文](https://github.com/gqylpy/gqylpy-exception/blob/master/README_CN.md)

`gqylpy-exception` is a flexible and convenient Python exception handling library that allows you to dynamically create exception classes and provides various exception handling mechanisms.

<kbd>pip3 install gqylpy_exception</kbd>

## Dynamically Creating Exceptions

With `gqylpy-exception`, you can instantly create exception classes when needed, without the need for advance definition. For example, if you want to throw an exception named `NotUnderstandError`, you can simply import the library and call it as follows:

```python
import gqylpy_exception as ge

raise ge.NotUnderstandError(...)
```

Here, `NotUnderstandError` is not predefined by `gqylpy-exception` but is dynamically created through the magic method `__getattr__` when you try to access `ge.NotUnderstandError`. This flexibility means you can create exception classes with any name as needed.

Additionally, `gqylpy-exception` ensures that the same exception class is not created repeatedly. All created exception classes are stored in the `ge.__history__` dictionary for quick access later.

There is another usage, import and create immediately:

```python
from gqylpy_exception import NotUnderstandError

raise NotUnderstandError(...)
```

## Powerful Exception Handling Capabilities

`gqylpy-exception` also provides a series of powerful exception handling tools:

- `TryExcept`: A decorator that catches exceptions raised in the decorated function and outputs the exception information to the terminal (instead of throwing it). This helps prevent the program from crashing due to unhandled exceptions.
- `Retry`: A decorator that works similarly to `TryExcept` but attempts to re-execute the function, controlling the number of attempts and the interval between each retry through parameters. It throws an exception after reaching the maximum number of attempts.
- `TryContext`: A context manager that allows you to easily catch exceptions raised in a code block using the `with` statement and output the exception information to the terminal.

**Handling Exceptions in Functions with `TryExcept`**

```python
from gqylpy_exception import TryExcept

@TryExcept(ValueError)
def func():
    int('a')
```

The default handling scheme is to output brief exception information to the terminal without interrupting program execution. Of course, it can also be output to logs or processed in other ways through parameters.

> According to Python programming conventions, exception types should be explicitly specified when handling exceptions. Therefore, when using the `TryExcept` decorator, it is necessary to explicitly pass the handled exception types.

**Retrying Exceptions in Functions with `Retry`**

```python
from gqylpy_exception import Retry

@Retry(count=3, cycle=1)
def func():
    int('a')
```

If an exception is raised in the decorated function, it will attempt to re-execute the decorated function. The default behavior is to retry exceptions of type `Exception` and all its subclasses. Calling `Retry(count=3, cycle=1)` as above means a maximum of 3 attempts will be made, with a 1-second interval between each attempt.

`Retry` can be used in combination with `TryExcept` to retry exceptions first and then handle them if the retries are unsuccessful:

```python
from gqylpy_exception import TryExcept, Retry

@TryExcept(ValueError)
@Retry(count=3, cycle=1)
def func():
    int('a')
```

**Handling Exceptions in Contexts with `TryContext`**

```python
from gqylpy_exception import TryContext

with TryContext(ValueError):
    int('a')
```

With `gqylpy-exception`, you can handle exceptions in Python programs more flexibly and efficiently, enhancing the robustness and reliability of your code.

            

Raw data

            {
    "_id": null,
    "home_page": "http://gqylpy.com",
    "name": "gqylpy-exception",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": "\u7af9\u6c38\u5eb7",
    "author_email": "<gqylpy@outlook.com>",
    "download_url": "https://files.pythonhosted.org/packages/77/d0/13ac94df8b907e74763e2bb06f7b4896e9d23a690517bb3ad00d721ed0ca/gqylpy_exception-3.1.2.tar.gz",
    "platform": null,
    "description": "[<img alt=\"LOGO\" src=\"http://gqylpy.com/static/img/favicon.ico\" height=\"21\" width=\"21\"/>](http://www.gqylpy.com)\n[![Release](https://img.shields.io/github/release/gqylpy/gqylpy-exception.svg?style=flat-square\")](https://github.com/gqylpy/gqylpy-exception/releases/latest)\n[![Python Versions](https://img.shields.io/pypi/pyversions/gqylpy_exception)](https://pypi.org/project/gqylpy_exception)\n[![License](https://img.shields.io/pypi/l/gqylpy_exception)](https://github.com/gqylpy/gqylpy-exception/blob/master/LICENSE)\n[![Downloads](https://static.pepy.tech/badge/gqylpy_exception)](https://pepy.tech/project/gqylpy_exception)\n\n# gqylpy-exception\nEnglish | [\u4e2d\u6587](https://github.com/gqylpy/gqylpy-exception/blob/master/README_CN.md)\n\n`gqylpy-exception` is a flexible and convenient Python exception handling library that allows you to dynamically create exception classes and provides various exception handling mechanisms.\n\n<kbd>pip3 install gqylpy_exception</kbd>\n\n## Dynamically Creating Exceptions\n\nWith `gqylpy-exception`, you can instantly create exception classes when needed, without the need for advance definition. For example, if you want to throw an exception named `NotUnderstandError`, you can simply import the library and call it as follows:\n\n```python\nimport gqylpy_exception as ge\n\nraise ge.NotUnderstandError(...)\n```\n\nHere, `NotUnderstandError` is not predefined by `gqylpy-exception` but is dynamically created through the magic method `__getattr__` when you try to access `ge.NotUnderstandError`. This flexibility means you can create exception classes with any name as needed.\n\nAdditionally, `gqylpy-exception` ensures that the same exception class is not created repeatedly. All created exception classes are stored in the `ge.__history__` dictionary for quick access later.\n\nThere is another usage, import and create immediately:\n\n```python\nfrom gqylpy_exception import NotUnderstandError\n\nraise NotUnderstandError(...)\n```\n\n## Powerful Exception Handling Capabilities\n\n`gqylpy-exception` also provides a series of powerful exception handling tools:\n\n- `TryExcept`: A decorator that catches exceptions raised in the decorated function and outputs the exception information to the terminal (instead of throwing it). This helps prevent the program from crashing due to unhandled exceptions.\n- `Retry`: A decorator that works similarly to `TryExcept` but attempts to re-execute the function, controlling the number of attempts and the interval between each retry through parameters. It throws an exception after reaching the maximum number of attempts.\n- `TryContext`: A context manager that allows you to easily catch exceptions raised in a code block using the `with` statement and output the exception information to the terminal.\n\n**Handling Exceptions in Functions with `TryExcept`**\n\n```python\nfrom gqylpy_exception import TryExcept\n\n@TryExcept(ValueError)\ndef func():\n    int('a')\n```\n\nThe default handling scheme is to output brief exception information to the terminal without interrupting program execution. Of course, it can also be output to logs or processed in other ways through parameters.\n\n> According to Python programming conventions, exception types should be explicitly specified when handling exceptions. Therefore, when using the `TryExcept` decorator, it is necessary to explicitly pass the handled exception types.\n\n**Retrying Exceptions in Functions with `Retry`**\n\n```python\nfrom gqylpy_exception import Retry\n\n@Retry(count=3, cycle=1)\ndef func():\n    int('a')\n```\n\nIf an exception is raised in the decorated function, it will attempt to re-execute the decorated function. The default behavior is to retry exceptions of type `Exception` and all its subclasses. Calling `Retry(count=3, cycle=1)` as above means a maximum of 3 attempts will be made, with a 1-second interval between each attempt.\n\n`Retry` can be used in combination with `TryExcept` to retry exceptions first and then handle them if the retries are unsuccessful:\n\n```python\nfrom gqylpy_exception import TryExcept, Retry\n\n@TryExcept(ValueError)\n@Retry(count=3, cycle=1)\ndef func():\n    int('a')\n```\n\n**Handling Exceptions in Contexts with `TryContext`**\n\n```python\nfrom gqylpy_exception import TryContext\n\nwith TryContext(ValueError):\n    int('a')\n```\n\nWith `gqylpy-exception`, you can handle exceptions in Python programs more flexibly and efficiently, enhancing the robustness and reliability of your code.\n",
    "bugtrack_url": null,
    "license": "Apache 2.0",
    "summary": "`gqylpy-exception` is a flexible and convenient Python exception handling library that allows you to dynamically create exception classes and provides various exception handling mechanisms.",
    "version": "3.1.2",
    "project_urls": {
        "Homepage": "http://gqylpy.com",
        "Source": "https://github.com/gqylpy/gqylpy-exception"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "cd541abf95d9f22e1abd1aa51bf757a3231b5226d50f61ade3e92f54e918e413",
                "md5": "f823d6e6bca40d0c7ec4d84fa1cddfa5",
                "sha256": "b924fc866ae7513f1d9dc396bea9ce4b2010dfa2a567c841fa414e6d4fba620c"
            },
            "downloads": -1,
            "filename": "gqylpy_exception-3.1.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f823d6e6bca40d0c7ec4d84fa1cddfa5",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 14332,
            "upload_time": "2024-07-15T01:31:46",
            "upload_time_iso_8601": "2024-07-15T01:31:46.617959Z",
            "url": "https://files.pythonhosted.org/packages/cd/54/1abf95d9f22e1abd1aa51bf757a3231b5226d50f61ade3e92f54e918e413/gqylpy_exception-3.1.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "77d013ac94df8b907e74763e2bb06f7b4896e9d23a690517bb3ad00d721ed0ca",
                "md5": "039da3cda6599a8e129e128f86a1e931",
                "sha256": "a7a5f972ac4b67848baeff9d4f089e26eeda1be3ebf654adfda3076c30652fcc"
            },
            "downloads": -1,
            "filename": "gqylpy_exception-3.1.2.tar.gz",
            "has_sig": false,
            "md5_digest": "039da3cda6599a8e129e128f86a1e931",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 14136,
            "upload_time": "2024-07-15T01:31:47",
            "upload_time_iso_8601": "2024-07-15T01:31:47.996713Z",
            "url": "https://files.pythonhosted.org/packages/77/d0/13ac94df8b907e74763e2bb06f7b4896e9d23a690517bb3ad00d721ed0ca/gqylpy_exception-3.1.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-15 01:31:47",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "gqylpy",
    "github_project": "gqylpy-exception",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "gqylpy-exception"
}
        
Elapsed time: 0.36635s