Skip to content

gavrielrh/rufous_result

Repository files navigation

Rufous Result

Translation of Rust's Result type to Python 3.

Example Usage

import requests
from models.user import User
from rufous_result.result import Result, Err, Ok

def get_user_from_api(id: int) -> Result<User, UserNotFoundError>:
    res = requests.get(f"https://example.com/api/user/{id}") 
    if res.ok:
        return Ok(User.from_dict(res.json()))
    return Err(UserNotFoundError(res.reason))

user_id = int(input("User to lookup: "))
print(get_user_from_api(user_id).unwrap_or("User not found"))

See the tests for more examples.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published