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.5.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

option-0.5.0-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file option-0.5.0.tar.gz.

File metadata

  • Download URL: option-0.5.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.17 CPython/3.7.1 Linux/4.15.0-1028-gcp

File hashes

Hashes for option-0.5.0.tar.gz
Algorithm Hash digest
SHA256 ffb784953918553dfd45de712cde562b12eb451244b32c54dae7356d24fb6425
MD5 64dc1ecbc3369e3741fc7be20de2d4d2
BLAKE2b-256 2b7a4bf7c523e575803e4906cf9a7fd729e985dc053cb0826c3dbb8adbb3383f

See more details on using hashes here.

File details

Details for the file option-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: option-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.17 CPython/3.7.1 Linux/4.15.0-1028-gcp

File hashes

Hashes for option-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 941a19bde0691706885be8494668c7085f93faa753823ce1c7a49e2b7d25a229
MD5 be5937438c4cc81b7540adbcbc099106
BLAKE2b-256 d2366b2432b20902525bd10f678eee94a88f7b72fb8146ba7ce0a51200063366

See more details on using hashes here.

Supported by

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