Skip to main content

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 guard with 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


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.1.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

guard_decorator-0.1.1-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

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

Hashes for guard_decorator-0.1.1.tar.gz
Algorithm Hash digest
SHA256 22462389591bd09e2985a087966a35cc97fb109b919a819350d1f97960f98abd
MD5 f1ef60a8dd4979d7598dd46ace1ca08a
BLAKE2b-256 572e15f49d5d911839b5980a90c6e5090845bfd47ba868ed45b53a771b2d1261

See more details on using hashes here.

File details

Details for the file guard_decorator-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for guard_decorator-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 561b83c91493bef4e185269dee49ddfc47ddcb06d00f169f519768e98809200e
MD5 0faa1f40327c43c9b85058d1fcb3c320
BLAKE2b-256 4354793e9651974cd985091a1206690a63b8908e2c452a44ed0f7a72ee8577b3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page