Skip to main content

Monads used for handling Errors. Contains Attempt and Option.

Project description

Monadic Error

by Ian Kollipara

This is a small library containing 2 monads for handling errors in Python: Attempt (Either) and Option (Maybe). These two are chosen for their usefulness in handling errors gracefully, and for the ability to slot in nicely with python. Both monads work with Python 3.10 pattern matching, and as well as MyPy exhaustive pattern matching.

All monads implement map, fmap, and unwrap_or. These all aid in their use in python. In addition there are a few utility functions for working with the objects during the execution of the program.

Installation

To install, run the following command from your terminal:

$ pip install monadic-error

Or modify your requirements.txt with the following line:

monadic-error==3.0.0

And run

$ pip install -r requirements.txt

Attempt

Attempt is the Either monad. The name was chosen to signify how it should be used. There are two constructors for this: Success and Failure. Use them as their name denotes.

from monadic_error import Attempt, Success, Failure

def div(x: int, y: int) -> Attempt[ZeroDivisionError, float]:
    if y == 0:
        return Failure(ZeroDivisionError("division by zero"))
    return Success(x / y)

# Or even shorter
from monadic_error import attempt

@attempt
def div(x: int, y: int):
    return x / y

Option

Option is the Maybe Monad. The name was chosen to signify how it should be used. There are two constructors for this: Some and Nothing. Use them as their name denotes.

from monadic_error import Option, Some, Nothing

def div(x: int, y: int) -> Option[float]:
    if y == 0:
        return Nothing()
    return Some(x / y)

# Or even shorter

from monadic_error import option

@option
def div(x: int, y: int):
    return x / y

Conversion Between Option and Attempt

There are two utility functions to convert between the two types: hush and note.

hush

Hush takes an attempt and "hushes" the error, returning an option instead

from monadic_error import hush, Success, Failure

x = Failure(1)
hush(x) # => Nothing()

y = Success(1)
hush(y) # => Some(1)

note

Note is the inverse, allowing one to state a reason for the failure, thus turning an option into an attempt.

from monadic_error import note, Some, Nothing

x = Nothing()
note(x, "This is an error") # => Failure("This is an error")

y = Some(1)
note(y, "This is an error") # => Success(1)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

monadic_error-3.0.1.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

monadic_error-3.0.1-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file monadic_error-3.0.1.tar.gz.

File metadata

  • Download URL: monadic_error-3.0.1.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.1 Linux/6.2.0-1018-azure

File hashes

Hashes for monadic_error-3.0.1.tar.gz
Algorithm Hash digest
SHA256 61167a2b8a7d021a85d6cd84557bab8aec87c1af338ece1141fbe9c5d946ef7e
MD5 ac9b23de1bf1f733a5e68f69b2687b1c
BLAKE2b-256 0facb0f589f8d81519d8eace767b2860f60e07f523347ba27d08a5a62456f10f

See more details on using hashes here.

File details

Details for the file monadic_error-3.0.1-py3-none-any.whl.

File metadata

  • Download URL: monadic_error-3.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.1 Linux/6.2.0-1018-azure

File hashes

Hashes for monadic_error-3.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a16489649289da6e649fa8110a5d8b38a8c1c3caa429f7262f3ca0e0a53420ac
MD5 c306e3eceac5faeafe273401506dad29
BLAKE2b-256 e7d8d1705ae12e59edb383901bacfd3b9668916c8eabb7dbef7650edc5636bc8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page