Safely
September 18, 2022
TLDR
safely
allows you to capture side effects, safely.
- GitHub: lukemiloszewski/safely
- Website: pypi.org/project/safely
Overview
safely
provides a higher-order function used to capture side effects from function invocations and handle them in a safe and consistent manner.
from safely import safely
def f(): raise Exception("Something went wrong!")
result = safely(f)()
>>> SafeResult(value=None, error=Exception('Something went wrong!'))
Installation
pip install safely
Usage
As a function (without second-order arguments):
def f(...): ...
result = safely(f)(...)
As a function (with second-order arguments):
def f(...): ...
result = safely(f, logger=logger.error, message="{exc_type}: {exc_value}")(...)
As a decorator (without second-order arguments):
@safely
def f(...): ...
result = f(...)
As a decorator (with second-order arguments):
@safely(logger=logger.error, message="{exc_type}: {exc_value}")
def f(...): ...
result = f(...)