`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.
Project description
gqylpy-exception
English | 中文
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.
pip3 install gqylpy_exception
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:
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:
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 toTryExcept
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 thewith
statement and output the exception information to the terminal.
Handling Exceptions in Functions with TryExcept
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
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:
from gqylpy_exception import TryExcept, Retry
@TryExcept(ValueError)
@Retry(count=3, cycle=1)
def func():
int('a')
Handling Exceptions in Contexts with TryContext
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file gqylpy_exception-3.1.2.tar.gz
.
File metadata
- Download URL: gqylpy_exception-3.1.2.tar.gz
- Upload date:
- Size: 14.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a7a5f972ac4b67848baeff9d4f089e26eeda1be3ebf654adfda3076c30652fcc |
|
MD5 | 039da3cda6599a8e129e128f86a1e931 |
|
BLAKE2b-256 | 77d013ac94df8b907e74763e2bb06f7b4896e9d23a690517bb3ad00d721ed0ca |
File details
Details for the file gqylpy_exception-3.1.2-py3-none-any.whl
.
File metadata
- Download URL: gqylpy_exception-3.1.2-py3-none-any.whl
- Upload date:
- Size: 14.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b924fc866ae7513f1d9dc396bea9ce4b2010dfa2a567c841fa414e6d4fba620c |
|
MD5 | f823d6e6bca40d0c7ec4d84fa1cddfa5 |
|
BLAKE2b-256 | cd541abf95d9f22e1abd1aa51bf757a3231b5226d50f61ade3e92f54e918e413 |