Skip to main content

A practical Result type for Python — Ok, Err, and pattern matching.

Project description

crashbytes-result

A practical Result type for Python — Ok, Err, and pattern matching.

Install

pip install crashbytes-result

Usage

from crashbytes_result import Ok, Err, Result, safe

def divide(a: float, b: float) -> Result[float, str]:
    if b == 0:
        return Err("division by zero")
    return Ok(a / b)

result = divide(10, 3)
match result:
    case Ok(value):
        print(f"Result: {value}")
    case Err(error):
        print(f"Error: {error}")

# Or use .match()
msg = result.match(
    ok=lambda v: f"Got {v:.2f}",
    err=lambda e: f"Failed: {e}",
)

# @safe decorator
@safe
def parse_int(s: str) -> int:
    return int(s)

parse_int("42")   # Ok(42)
parse_int("abc")  # Err(ValueError(...))

API

Method Ok Err
.unwrap() Returns value Raises UnwrapError
.unwrap_or(default) Returns value Returns default
.unwrap_err() Raises UnwrapError Returns error
.map(fn) Ok(fn(value)) self
.map_err(fn) self Err(fn(error))
.bind(fn) fn(value) self
.match(ok, err) ok(value) err(error)
.is_ok True False
.is_err False True

License

MIT

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

crashbytes_result-1.1.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

crashbytes_result-1.1.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file crashbytes_result-1.1.0.tar.gz.

File metadata

  • Download URL: crashbytes_result-1.1.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for crashbytes_result-1.1.0.tar.gz
Algorithm Hash digest
SHA256 30ecb1b76e02ff31219f9cfd6236afe299f1e1980035b2b6704c22aaf93a07e3
MD5 be5a642df9e7b70b95298fe751299ae8
BLAKE2b-256 2219ffeb703cb2aae1c70c9bdd9b466a11da92a716ccf0278c1fd8708265c4bb

See more details on using hashes here.

Provenance

The following attestation bundles were made for crashbytes_result-1.1.0.tar.gz:

Publisher: publish.yml on CrashBytes/crashbytes-result

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file crashbytes_result-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for crashbytes_result-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 41f16f25ca8fac89ca5a5c0d90102be9e0d24a12952575d7459f6ae5421e322e
MD5 f5ffa3fabcff4e0004a2178da0dfee9c
BLAKE2b-256 d17ed349ccc018abd3b533d635bca3b2d8605d9b1016505e008fee5997cad33a

See more details on using hashes here.

Provenance

The following attestation bundles were made for crashbytes_result-1.1.0-py3-none-any.whl:

Publisher: publish.yml on CrashBytes/crashbytes-result

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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