Skip to main content

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): If True, the program will exit upon encountering an error.
  • rv (Any): The value to return if an error occurs. If not specified, the function will return None.

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): If True, 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): If True, 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

Release files for python-errors 0.0.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-errors 0.0.9
File Size Uploaded
python_errors-0.0.9.tar.gz 10.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-errors 0.0.9
File Interpreter ABI Platform
python_errors-0.0.9-py3-none-any.whl Python 3 none any Details

Total release size: 22.4 kB

Release files / python_errors-0.0.9.tar.gz

Download URL python_errors-0.0.9.tar.gz
Size 10.9 kB
Tags Source
SHA-256 checksum
How to use checksums
f026cbdb8bae3317755c4b40990aa73e0d960f6a331b4addf4bf26066285605c
BLAKE2b-256 checksum
How to use checksums
d04477ef428b62ec58e2fa8baeef30c8908c0827e966a8d435bcafa218998d54
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.3

Release files / python_errors-0.0.9-py3-none-any.whl

Download URL python_errors-0.0.9-py3-none-any.whl
Size 11.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b19c57852b7e24827fe3e3fa2ae137a341ff121e3f6863051d3e5d0133781b2a
BLAKE2b-256 checksum
How to use checksums
674851786c117bc9d24f164991549f0c7ee09e96a15ac660efbceaf06f3485fd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.0.9 This release

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page