项目作者: MaT1g3R

项目描述 :
Rust like Option and Result types in Python
高级语言: Python
项目地址: git://github.com/MaT1g3R/option.git
创建时间: 2018-09-10T19:58:10Z
项目社区:https://github.com/MaT1g3R/option

开源协议:MIT License

下载


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

  1. from option import Result, Option, Ok, Err
  2. from requests import get
  3. def call_api(url, params) -> Result[dict, int]:
  4. result = get(url, params)
  5. code = result.status_code
  6. if code == 200:
  7. return Ok(result.json())
  8. return Err(code)
  9. def calculate(url, params) -> Option[int]:
  10. return call_api(url, params).ok().map(len)
  11. dict_len = calculate('https://example.com', {})

Install

Option can be installed from PyPi:

  1. pip install option

Documentation

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

License

MIT