Skip to main content

Rust like Option and Result types in Python

Project description

Option

Build Status codecov

Rust-like Option and Result types in Python, slotted and fully typed.

An Option type represents an optional value, every Option is either Some and contains Some value, or NONE

A Result type represents a value that might be an error. Every Result is either Ok and contains a success value, or Err and contains an error value.

Using an Option type forces you to deal with None values in your code and increase type safety.

Using a Result type simplifies error handling and reduces try except blocks.

Quick Start

from option import Result, Option, Ok, Err
from requests import get


def call_api(url, params) -> Result[dict, int]:
    result = get(url, params)
    code = result.status_code
    if code == 200:
        return Ok(result.json())
    return Err(code)


def calculate(url, params) -> Option[int]:
    return call_api(url, params).ok().map(len)


dict_len = calculate('https://example.com', {})

Install

Option can be installed from PyPi:

pip install option

Documentation

The documentation lives at https://mat1g3r.github.io/option/

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

option-0.3.0.tar.gz (7.5 kB view hashes)

Uploaded Source

Built Distribution

option-0.3.0-py3-none-any.whl (33.2 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