Rust like `Result[T, E]` support for python!
Project description
Rsult
Rsult (rust result = rs + result) is a python library for adding rust like
Result[T, E]support to python.
Background
In rust, rather than throw exceptions up some side channel, you return them directly as part of a functions response.
These responses are wrapped in what rust refers to as a Result.
Result's are simple objects which contain either the result of the function call, or an exception which was raised by the function.
Usage
There are many ways you can choose to use the rsult Result class.
The most common use is to just unpack the response into individual error and response variables.
However, the unwrap() functio can also be used much like unwrap in rust.
When called from a result that does not contain an error, unwrap(result) will return the response from the function.
If unwrap(result) is called with a result that contains an error, that error will be raised as an exception.
Examples
from rsult import Result, unwrap, wrap, wrap_error
class LessThanZeroError(Exception):
pass
def add_numbers(x: int, y: int) -> Result[int, LessThanZeroError]:
z = x + y
if z < 0:
return wrap_error(LessThanZeroError())
return wrap(z)
# a regular call to the function that returns the response
error, answer = add_numbers(2, 2)
assert error is None
assert answer == 4
# a call to the function that results in an error
error, answer = add_numbers(2, -4)
assert type(error) is LessThanZeroError
assert answer is None
# unwrap can be used to throw the error, rather than unpacking the result
result = add_numbers(2, -4)
answer = unwrap(result) # <-- LessThanZeroError gets throw
Authors
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rsult-1.0.1.tar.gz.
File metadata
- Download URL: rsult-1.0.1.tar.gz
- Upload date:
- Size: 19.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.6.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a6e7535a8189261f6803cab67b3da1319f26594e5d918ce236a1e702b4689aca
|
|
| MD5 |
2c787dd7ec0e1dacbab97f073bfa94e4
|
|
| BLAKE2b-256 |
4ca68251b45f1fc1219d9d73b156db6e9009e98f2028d2cb9bc18c40f3a347c1
|
File details
Details for the file rsult-1.0.1-py3-none-any.whl.
File metadata
- Download URL: rsult-1.0.1-py3-none-any.whl
- Upload date:
- Size: 3.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.6.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5cb20f0c3232576a4aacd8898172a3e841aa932b706411fadbe7740d2ec11f60
|
|
| MD5 |
d461812749029c304963ac8096964c8b
|
|
| BLAKE2b-256 |
2fe8a10f5ee2fd92f41dc03dea59f753ce2cd59a41b0bc695ebb50e671de3ddc
|