Skip to main content

Yet another Rust's Option<T> and Result<T, E> simple implementation in Python.

Project description

optres

Yet another Rust's Option<T> and Result<T, E> simple implementation in Python.

Python package PyPI version License: MIT pre-commit Github pages

Installation

python3 -m pip install -U optres

Example

from optres import unwrap, Result, Err, is_ok, is_err, unwrap_err
from typing import LiteralString


def return_result(x: int | None) -> Result[int, LiteralString]:
    return Err("not int") if x is None else x


def example() -> None:
    a: int | None = 1
    c: int = unwrap(a)
    print(c)
    assert is_ok(return_result(a))
    a = None
    # unwrap(a) # error := panic in Rust.
    may_be_err = return_result(a)
    assert is_err(may_be_err)
    print(unwrap_err(may_be_err))


if __name__ == "__main__":
    example()

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

optres-0.1.0.tar.gz (7.1 kB view hashes)

Uploaded Source

Built Distribution

optres-0.1.0-py3-none-any.whl (7.5 kB view hashes)

Uploaded Python 3

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