A library for reducing and identifying critical errors in Python code.
Project description
Python Errors
Python Errors is a Python library designed to handle and manage exceptions effectively, ensuring that critical errors in your code are caught, logged, and handled in a structured way. It provides decorators to help you capture errors in your functions, log them properly, and control the flow of execution.
Features
- Structured Error Handling: Log detailed error information including traceback, function name, and line number.
- Customizable Error Responses: Choose whether to return fallback values or exit the program on error.
- Flexible Decorators: Use decorators that cater to different data structures (lists, dictionaries, etc.) and their specific errors.
Installation
You can install Python Errors using pip:
pip install python-errors
Usage
To use the decorators in this library, simply import them and apply them to the functions you want to protect. You can customize the behavior of each decorator using parameters.
Decorators
@secure_call(exit_on_error=False, rv=None)
This decorator catches any general exceptions in the wrapped function. You can choose to return a fallback value or exit the program if an error occurs.
Parameters:
exit_on_error (bool): IfTrue, the program will exit upon encountering an error.rv (Any): The value to return if an error occurs. If not specified, the function will returnNone.
Example:
from python_errors import secure_call
@secure_call(exit_on_error=True, rv="Fallback Value")
def risky_function():
# Some code that may raise an exception
print(1 / 0) # This will raise ZeroDivisionError
risky_function() # Program will exit due to the exception, or return 'Fallback Value'
@secure_array_call(exit_on_error=False, rv=None, secure_size=True, secure_type=True)
This decorator ensures that the elements of the array are of the same type and that the array size does not change during execution. It’s particularly useful when working with lists.
Parameters:
exit_on_error (bool): IfTrue, exit the program if an error occurs.rv (Any): The value to return if an error occurs.secure_size (bool): Ensures the size of the array doesn’t change during execution.secure_type (bool): Ensures all elements in the array are of the same type.
Example:
from python_errors import secure_array_call
@secure_array_call(exit_on_error=True, rv="Fallback Value", secure_size=True, secure_type=True)
def process_array(arr):
# Some operation on the array
arr.append(5) # This will cause an error if secure_size=True and the array size changes
process_array([1, 2, 3]) # Will exit the program if the array size changes
@secure_dict_call(exit_on_error=False, rv=None, secure_size=False, secure_types=False, secure_key_type=False, secure_value_type=False)
This decorator ensures that dictionary operations are secure by validating various conditions, such as the size of the dictionary, the types of its keys and values, and whether the dictionary is modified during execution.
Parameters:
exit_on_error (bool): IfTrue, exit the program if an error occurs.rv (Any): The value to return if an error occurs.secure_size (bool): Ensures the size of the dictionary doesn’t change during execution.secure_types (bool): Ensures that the types of the keys and values are consistent.secure_key_type (bool): Ensures the keys in the dictionary are of the same type.secure_value_type (bool): Ensures the values in the dictionary are of the same type.
Example:
from python_errors import secure_dict_call
@secure_dict_call(exit_on_error=True, rv="Fallback Value", secure_size=True, secure_types=True, secure_key_type=True, secure_value_type=True)
def process_dict(d):
# Some operation on the dictionary
d["new_key"] = 10 # This will raise an error if the dictionary size changes and secure_size=True
process_dict({"item-1": 1, "item-2": 2, "item-3": 3}) # Will exit the program if the dictionary size changes
Project details
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_errors-0.0.9.tar.gz.
File metadata
- Download URL: python_errors-0.0.9.tar.gz
- Upload date:
- Size: 10.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f026cbdb8bae3317755c4b40990aa73e0d960f6a331b4addf4bf26066285605c
|
|
| MD5 |
03e77c0153bd50722a1b32a8914dc146
|
|
| BLAKE2b-256 |
d04477ef428b62ec58e2fa8baeef30c8908c0827e966a8d435bcafa218998d54
|
File details
Details for the file python_errors-0.0.9-py3-none-any.whl.
File metadata
- Download URL: python_errors-0.0.9-py3-none-any.whl
- Upload date:
- Size: 11.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b19c57852b7e24827fe3e3fa2ae137a341ff121e3f6863051d3e5d0133781b2a
|
|
| MD5 |
a69a13c5809efcc409d3c6ee4149dba0
|
|
| BLAKE2b-256 |
674851786c117bc9d24f164991549f0c7ee09e96a15ac660efbceaf06f3485fd
|