A decorator to simplify error handling in Python.
Project description
Guard Decorator
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
guardwith class methods to maintain clean and effective error handling.
Installation
You can install the guard library via pip:
pip install guard-decorator
Basic usage:
from guard_decorator 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
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 guard_decorator-0.1.1.tar.gz.
File metadata
- Download URL: guard_decorator-0.1.1.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 |
22462389591bd09e2985a087966a35cc97fb109b919a819350d1f97960f98abd
|
|
| MD5 |
f1ef60a8dd4979d7598dd46ace1ca08a
|
|
| BLAKE2b-256 |
572e15f49d5d911839b5980a90c6e5090845bfd47ba868ed45b53a771b2d1261
|
File details
Details for the file guard_decorator-0.1.1-py3-none-any.whl.
File metadata
- Download URL: guard_decorator-0.1.1-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 |
561b83c91493bef4e185269dee49ddfc47ddcb06d00f169f519768e98809200e
|
|
| MD5 |
0faa1f40327c43c9b85058d1fcb3c320
|
|
| BLAKE2b-256 |
4354793e9651974cd985091a1206690a63b8908e2c452a44ed0f7a72ee8577b3
|