Skip to main content

Rust like Option and Result types in Python

Project description

Option

CircleCI

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

Uploaded Source

Built Distribution

option-2.1.0-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: option-2.1.0.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.4 Linux/5.13.0-1021-aws

File hashes

Hashes for option-2.1.0.tar.gz
Algorithm Hash digest
SHA256 9fe95a231e54724d2382a5124b55cd84b82339edf1d4e88d6977cedffbfeadf1
MD5 32ac8e5eb0e1cf4eacc186e6a916d6a2
BLAKE2b-256 157a3622379bd82f70a0b88779566c4847f167ae54103187922b69ad63d3c3b2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: option-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.4 Linux/5.13.0-1021-aws

File hashes

Hashes for option-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 21ccd9a437dbee0341700367efb68e82065fd7a7dba09f8c3263cf2dc1a2b0e0
MD5 c0d8751de7743a5027989ea596923adf
BLAKE2b-256 b0e7a4ece49ebcb33fc43577538f85406598ec93d5675dfb30331ec586351656

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