Skip to main content

Rust-style Result and Option implementation for Python

Project description

reop

reop is a lightweight Python library inspired by Rust's Result and Option patterns. The name combines "Result" and "Option", representing the two core concepts of the library.

Why Use reop?

The Problem

Python's try/except for error handling and the frequent use of None as a placeholder for missing values are flexible, but they often lack clarity:

Implicit failures: A function might fail, but this is not explicit in its return type. Unclear intent: None values can propagate silently, making debugging harder. Ad-hoc error handling: Handling errors consistently across large codebases is challenging.

The Solution

With reop, you can leverage Rust-inspired patterns for safer and clearer error handling:

Explicit Error Handling: Functions return Result[ValueType, ErrorType] to indicate success or failure explicitly. No unexpected exceptions—errors are part of the function's return type. Structured Value Management: Use Option to represent values that may or may not exist. No more unchecked None values—intent is always explicit. Composable and Readable Code: Chain operations on Result and Option objects with methods like map, and_then, and or_else.

Installation

pip install reop

Usage

Using Result for Explicit Error Handling

from reop import Result, result_wrapper

@result_wrapper
def divide(a: int, b: int) -> float:
    return a / b

# Example 1: Successful division
result = divide(10, 2)
if result.is_ok:
    print(f"Success: {result.unwrap()}")  # Output: Success: 5.0

# Example 2: Division by zero
result = divide(10, 0)
if result.is_err:
    print(f"Error: {result.unwrap_err()}")  # Output: Error: division by zero

Using Option for Optional Values

from reop import Option, option_wrapper

@option_wrapper
def find_value(key: str, data: dict) -> Optional[int]:
    return data.get(key)

# Example 1: Key exists
option = find_value("a", {"a": 42})
if option.is_some:
    print(f"Found: {option.unwrap()}")  # Output: Found: 42

# Example 2: Key does not exist
option = find_value("b", {"a": 42})
print(option.unwrap_or(0))  # Output: 0

Key Benefits of reop

Improved Clarity: Make function intentions and possible failure cases explicit. Safer Code: Avoid unexpected None values or unhandled exceptions. Rust-inspired: Adopt proven patterns from Rust for Python programming. Composable Methods: Chain operations for clean and concise workflows.

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

reop-1.0.0.tar.gz (2.3 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: reop-1.0.0.tar.gz
  • Upload date:
  • Size: 2.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.10

File hashes

Hashes for reop-1.0.0.tar.gz
Algorithm Hash digest
SHA256 03e16357784aa4a943b070a7a3469c0ed7d48b291db0cc0044e399d2c5f5b18c
MD5 529391cbff63b32487532ad72c978ed3
BLAKE2b-256 d26a5d92e43c8f02fbae9c8ba20d0ddb6381d9f8122b0418b566478b12526cad

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