PyResilient
PyResilient is a Python module designed to provide resilience strategies such as retries, timeouts, and fallback mechanisms for your functions. This module is ideal for elegantly and robustly handling errors and exceptions in your applications, enhancing reliability and user experience.
Table of Contents
Installation
You can install PyResilient using pip:
pip install PyResilient
PyResilient requires Python 3.8 or higher.
Usage
PyResilient provides decorators that can be easily applied to your functions to add resilience features with minimal code changes.
Fallback Decorator
The fallback decorator allows you to apply a fallback strategy to a function. If the original function raises an exception, the fallback function will be executed.
from PyResilient import fallback
def fallback_function():
print("Executing fallback function")
return "Default value"
@fallback(fallback_function, exceptions=(ValueError,))
def my_function():
raise ValueError("An error occurred")
result = my_function() # Will print "Executing fallback function"
print(result) # Outputs: Default value
Parameters:
fallback_function(callable): The function to execute if an exception is raised.exceptions(tuple of Exception types, optional): The exceptions that trigger the fallback. Defaults to(Exception,).
Retry Decorator
The retry decorator allows you to retry the execution of a function a specific number of times before failing.
from PyResilient import retry
@retry(retries=3, exceptions=(ValueError,), delay=2)
def my_function():
print("Attempting to execute function")
raise ValueError("An error occurred")
my_function()
# Will attempt to execute the function 3 times with a 2-second delay between attempts
Parameters:
retries(int): The maximum number of retry attempts.exceptions(tuple of Exception types, optional): The exceptions that trigger a retry. Defaults to(Exception,).delay(int or float, optional): The delay between retry attempts in seconds. Defaults to0.
Timeout Decorator
The timeout decorator allows you to set a time limit for the execution of a function. If the function does not complete within the specified time, a TimeoutError is raised.
from PyResilient import timeout
@timeout(seconds=5)
def my_function():
import time
time.sleep(10)
return "Completed"
try:
result = my_function()
except TimeoutError:
print("Function execution timed out")
Parameters:
seconds(int or float): The maximum allowed execution time in seconds.
Note: The timeout decorator uses threading under the hood, which may have implications for thread safety and resource management in your application.
License
This project is licensed under the MIT License. See the LICENSE file for more details.
Author
Bastián García
- Email: bastiang@uc.cl
- GitHub: github.com/cve-zh00
Acknowledgments
- Inspired by the need for robust error handling mechanisms in Python applications.
- Thanks to the open-source community for their continuous support and contributions.
- Dedicated to felipe hrdina, who says that I never finish my projects xd
Metadata
Release files for PyResilient 0.0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pyresilient-0.0.5.tar.gz | 8.8 kB | Without provenance | Details for pyresilient-0.0.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| PyResilient-0.0.5-py3-none-any.whl | Python 3 | none | any | Details for PyResilient-0.0.5-py3-none-any.whl |
Total release size: 18.4 kB
Release files / pyresilient-0.0.5.tar.gz
| Download URL | pyresilient-0.0.5.tar.gz |
|---|---|
| Size | 8.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4976d26d1d9cf211858213cd33b1fe801543c31127663b5230f859136bc01934
|
|
BLAKE2b-256 checksum How to use checksums |
8d0f05a83636bb804e631d004d76466e76aa06f3faeb85a9bd3bff0dd3c19876
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.5
|
Release files / PyResilient-0.0.5-py3-none-any.whl
| Download URL | PyResilient-0.0.5-py3-none-any.whl |
|---|---|
| Size | 9.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
756a92fbc7be8d4d8d93ef1ad2ec17233fbee7ced6f97bbac07820d6a71f89ae
|
|
BLAKE2b-256 checksum How to use checksums |
07a376b8ef47097575f225af71898874f5e1b389e40e856000205415e67c7fdc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.5
|
Security for PyResilient 0.0.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.