Skip to main content

paraboloski-monad

An error handling decorator for Python that leverages the monadic design pattern. It represents operations that run on two parallel tracks: they can either succeed (Ok) or fail (Err), without raising exceptions.


Installation

PyPI

uv add paraboloski-monad
pip install paraboloski-monad

GitHub

uv add git+https://github.com/paraboloski/snippet-toolbox/python#subdirectory=snippet/monad

When to use it

Ideal for operations that can fail predictably: HTTP calls, data parsing, filesystem access, database queries. Instead of scattering try/except blocks throughout your code, failure becomes an explicit part of the return type.

Usage

from monad import Ok, Err, monad, safe, safe_async

@safe
def parse_user(data: dict) -> User:
    return User(
        id=data["id"],
        name=data["name"],
        email=data["email"],
    )

@safe_async
async def fetch_user(user_id: int) -> dict:
    async with aiohttp.ClientSession() as session:
        async with session.get(f"/api/users/{user_id}") as response:
            return await response.json()

Handle results with match

match parse_user(data):
    case Ok(user):
        print(f"Welcome, {user.name}")
    case Err(error):
        print(f"Invalid user data: {error}")

Operation pipelines

Failures propagate automatically without nested try/except blocks:

@safe
def read_file(path: str) -> str:
    with open(path) as f:
        return f.read()

@safe
def parse_json(content: str) -> dict:
    return json.loads(content)

@safe
def parse_user(data: dict) -> User:
    if "name" not in data:
        raise ValueError("missing 'name' field")
    return User(**data)

match read_file("user.json"):
    case Ok(content):
        match parse_json(content):
            case Ok(data):
                match parse_user(data):
                    case Ok(user):
                        print(f"Hello, {user.name}")
                    case Err(e):
                        print(f"Invalid user: {e}")
            case Err(e):
                print(f"Malformed JSON: {e}")
    case Err(e):
        print(f"File not found: {e}")

Or provide a default value with unwrap_or

When failure is acceptable and you want a fallback value:

user = parse_user(data).unwrap_or(
    User(id=0, name="anonymous", email="")
)

users = (await fetch_user(user_id)).unwrap_or([])

Italiano

Un decoratore per l'error handling per Python che sfrutta il design pattern monadico. Rappresenta operazioni che seguono due binari paralleli: possono avere successo (Ok) o fallire (Err), senza sollevare eccezioni.

Installazione

PyPI

uv add paraboloski-monad
pip install paraboloski-monad

GitHub

uv add git+https://github.com/paraboloski/snippet-toolbox/python#subdirectory=snippet/monad

Quando usarla

Ideale per operazioni che possono fallire in modo prevedibile: chiamate HTTP, parsing di dati, accesso al filesystem, query a database. Invece di disseminare try/except nel codice, il fallimento diventa parte esplicita del tipo di ritorno.

Utilizzo

from monad import Ok, Err, monad, safe, safe_async

@safe
def parse_user(data: dict) -> User:
    return User(
        id=data["id"],
        name=data["name"],
        email=data["email"],
    )

@safe_async
async def fetch_user(user_id: int) -> dict:
    async with aiohttp.ClientSession() as session:
        async with session.get(f"/api/users/{user_id}") as response:
            return await response.json()

Gestisci il risultato con match

match parse_user(data):
    case Ok(user):
        print(f"Benvenuto, {user.name}")
    case Err(error):
        print(f"Dati utente non validi: {error}")

Pipeline di operazioni

Il fallimento si propaga automaticamente senza blocchi try/except annidati:

@safe
def read_file(path: str) -> str:
    with open(path) as f:
        return f.read()

@safe
def parse_json(content: str) -> dict:
    return json.loads(content)

@safe
def parse_user(data: dict) -> User:
    if "name" not in data:
        raise ValueError("campo 'name' mancante")
    return User(**data)

match read_file("user.json"):
    case Ok(content):
        match parse_json(content):
            case Ok(data):
                match parse_user(data):
                    case Ok(user):
                        print(f"Ciao, {user.name}")
                    case Err(e):
                        print(f"Utente non valido: {e}")
            case Err(e):
                print(f"JSON malformato: {e}")
    case Err(e):
        print(f"File non trovato: {e}")

Oppure fornisci un valore di default con unwrap_or

Quando il fallimento è accettabile e hai un valore di fallback:

user = parse_user(data).unwrap_or(
    User(id=0, name="anonimo", email="")
)

users = (await fetch_user(user_id)).unwrap_or([])

Metadata

Release files for paraboloski-monad 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for paraboloski-monad 1.0.0
File Size Uploaded Provenance Actions
paraboloski_monad-1.0.0.tar.gz 4.4 kB Without provenance Details for paraboloski_monad-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for paraboloski-monad 1.0.0
File Interpreter ABI Platform Actions
paraboloski_monad-1.0.0-py3-none-any.whl Python 3 none any Details for paraboloski_monad-1.0.0-py3-none-any.whl

Total release size: 8.4 kB

Release files / paraboloski_monad-1.0.0.tar.gz

Download URL paraboloski_monad-1.0.0.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
8bd322a3d9d8d60229d5e698c5e1af021e997779f3c1b002b5fb7dcb6b483c90
BLAKE2b-256 checksum
How to use checksums
2d58731391d3613709591f8c47d91916c1b9a1d1bc3d997b6844cde2c20514f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release files / paraboloski_monad-1.0.0-py3-none-any.whl

Download URL paraboloski_monad-1.0.0-py3-none-any.whl
Size 3.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e27c1317ce12430a9e1df30434f1573d2cf3ff905ea142f7755156c4a3afb84a
BLAKE2b-256 checksum
How to use checksums
ff8e0f1f703c2fb8702eb5effc18e52bc18e65b10247a7dbf5d6d9fe327a4d52
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Security for paraboloski-monad 1.0.0

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.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page