superleaf.operators.wrappers#

Functions

with_fallback(f[, fallback])

Wrap a function to return fallback when it raises one of exceptions.

superleaf.operators.wrappers.with_fallback(f: Callable | None = None, fallback=None, exceptions: type | tuple | str | None = <class 'Exception'>) → Callable[source]#

Wrap a function to return fallback when it raises one of exceptions.

Usable directly (with_fallback(f, 0)) or as a decorator, with or without arguments.

Parameters:
  • f (callable, optional) – Function to wrap. If omitted, a decorator is returned.

  • fallback (Any, optional) – Value returned when one of exceptions is raised. Default None.

  • exceptions (type, tuple of types, or str, optional) – Exceptions to catch. Default Exception. "ignore"/"all" mean Exception; "raise"/"none" or None catch nothing.

Returns:

The wrapped function, or a decorator if f is None.

Return type:

Callable

Raises:

ValueError – If exceptions is an unrecognized string.