Skip to main content

No project description provided

Project description

result.py

A Python implementation inspired by Rust's Result<T, E>, representing operations that can either succeed (Ok) or fail (Err).

Installation

$ pip install pytresult

Motivation

Using Result helps avoid excessive exception handling and promotes more explicit and robust error handling.

Usage Example

from result import Result, Ok, Err

def divide(a: int, b: int) -> Result[float, str]:
    if b == 0:
        return Err("division by zero")
    return Ok(a / b)

result = divide(10, 2)

if result.is_ok():
    print("Success:", result.unwrap())
else:
    print("Error:", result)  # or result.unwrap_or(0)

Patterns

if let Ok(...) { }

  • Rust

    fn divide(a: i32, b: i32) -> Result<f32, String> {
        if b == 0 {
            Err("division by zero".to_string())
        } else {
            Ok(a as f32 / b as f32)
        }
    }
    
    let result = divide(10, 2);
    
    if let Ok(value) = result {
        println!("Success: {}", value);
    }
    
  • Python

    from result import Result, Ok, Err
    
    def divide(a: int, b: int) -> Result[int, str]:
        if b == 0:
            return Err("division by zero")
        return Ok(a // b)
    
    result = divide(10, 2)
    
    if r := result.ok():
        assert r is not None
        assert r == 5
    

if let Err(...) { }

  • Rust

    fn divide(a: i32, b: i32) -> Result<f32, String> {
        if b == 0 {
            Err("division by zero".to_string())
        } else {
            Ok(a as f32 / b as f32)
        }
    }
    
    let result = divide(10, 0);
    
    if let Err(error) = result {
        println!("Error: {}", error);
    }
    
  • Python

    from result import Result, Ok, Err
    
    def divide(a: int, b: int) -> Result[int, str]:
        if b == 0:
            return Err("division by zero")
        return Ok(a // b)
    
    result = divide(10, 0)
    
    if e := result.err():
        assert e is not None
        assert e == "division by zero"
    

Testing

Tests are written using pytest.

To run the tests:

poetry run pytest

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

pytresult-1.0.1.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytresult-1.0.1-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file pytresult-1.0.1.tar.gz.

File metadata

  • Download URL: pytresult-1.0.1.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.11 Linux/6.11.0-1018-azure

File hashes

Hashes for pytresult-1.0.1.tar.gz
Algorithm Hash digest
SHA256 0d3d3f0ca64013e6d80f30b8bb9686b38f89cd7df9874e455176f32e8d38a779
MD5 ad00c42f617ef8e99f29380fe568eee9
BLAKE2b-256 0c63b6015ea4c642dbea8e9105e5669bc8609e244495a9bba3e7ff96dc4d6577

See more details on using hashes here.

File details

Details for the file pytresult-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: pytresult-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.11 Linux/6.11.0-1018-azure

File hashes

Hashes for pytresult-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c5ba279cf558e05b6310e8b28c70bc9c492d9cfa756287cabe1c4f07296b1453
MD5 e801c66e16ebfd539eeea59a67cb69ea
BLAKE2b-256 1f4edcbafcdb39da6f5df2d9aef441aa794baff248d19407b317a3ddee286a20

See more details on using hashes here.

Supported by

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