Skip to content

lukemiloszewski/safely

Repository files navigation

Safely

Capture side effects, safely ⚔️

Continuous Integration Package Version Supported Python Versions

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(...)