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

$ poetry add "git+https://github.com/HicaroD/result.py"

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.0.tar.gz (3.2 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.0-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytresult-1.0.0.tar.gz
  • Upload date:
  • Size: 3.2 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.0.tar.gz
Algorithm Hash digest
SHA256 ac2e9b7c3edf631e6b5f163b850c411c5e020bcef3bdc944ebc09d8ba717094c
MD5 78e3791fa5824aaa9f7ccb0e237dd601
BLAKE2b-256 135da5a06d7aa6cec1e9aa34b6f89c2d2dc4c893e397d48042944b93e9194cfa

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pytresult-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 3.8 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 602031da9fb1413d8bdf73ef7ab8f335b14030116ce92d0aad8f6501cacd9945
MD5 0df27775918667579b3675a085d70b3e
BLAKE2b-256 5398d411f435a63216149fba541b387cbc1ef5b0e12a8085709899e146db90f3

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