A decorator to simplify error handling in Python.
Project description
Guard
Guard is a Python library designed to simplify exception handling in your code. By using the guard
decorator, you can significantly reduce the boilerplate code associated with try-except-finally
blocks, making your code cleaner and more readable.
Features
- Simplified Exception Handling: Easily handle exceptions with minimal boilerplate code.
- custom Handlers: Specify custom exception handlers for different exceptions.
- Cleanup Actions: Automatically perform cleanup actions after function execution, regardless of success or failure.
- Support for Classes: Use
guard
with class methods to maintain clean and effective error handling.
Installation
You can install the guard
library via pip:
pip install guard
Basic usage:
from guard import guard
# Exception handler
def handle_division_error(e, *args, **kwargs):
print(f"Caught exception: {e}")
@guard(h=handle_division_error)
def divide_numbers(a, b):
print(f"Dividing {a} by {b}")
return a / b
print(f"Result: {divide_numbers(10, 2)}") # OK
print(f"Result: {divide_numbers(10, 0)}") # Raises Exception
That code will produce the following output:
Dividing 10 by 2
Result: 5.0
Dividing 10 by 0
Caught exception: division by zero
Result: None
You can also specify exception type
@guard(type=ValueError, h=handle_value_error)
def risky_function(value):
if value < 0:
raise ValueError("Negative value not allowed!")
And cleanup actions
def cleanup_file(*args, **kwargs):
print("Cleaning up resources...")
@guard(f=cleanup_file)
def open_file(file_name):
raise FileNotFoundError("File not found!") # Simulate error
open_file("non_existent_file.txt")
License
This project is licensed under the Apache 2.0 License
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
guard_decorator-0.1.0.tar.gz
(6.7 kB
view details)
Built Distribution
File details
Details for the file guard_decorator-0.1.0.tar.gz
.
File metadata
- Download URL: guard_decorator-0.1.0.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c113ed1ba8ed0c4ba73446add66a6e9e345237cabb3b38a85ed624760bdc4aa7 |
|
MD5 | daa0107079af259464a2a5210c1126c9 |
|
BLAKE2b-256 | ab14f483d2cbf2699e29ed231c7e85cc8ce3737cebaa0b280d97fc7ba245c9aa |
File details
Details for the file guard_decorator-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: guard_decorator-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b62256b320c30610e557311df0ac39ecaa1364f37eccb9b13b992b42e0c8dd6a |
|
MD5 | 1b5f9bea0887c1c8d4f175ad9fc82a89 |
|
BLAKE2b-256 | e300b74758c5ba77ece257731fe119772ff8cab58ef41bdcdeff9a45d36c5d25 |