Tangled Result
Project description
thresult
Overview
Python Result
library for handling returned values (Result
, Ok
, Err
) from functions/methods and handling errors. It is error handling library which is alternative to try/except
style of programming.
It is inspired by great Rust Result
, Ok
, Err
types.
Installation
pip install thresult
Simple Usage
Traditional Python try-except example
def div(x: float, y: float) -> float:
z: float = x / y
return z
z0: float = div(1.0, 2.0) # 0.5
z1: float = div(1.0, 0.0) # raises "ZeroDivisionError: division by zero" exception
Using Result
as Context Manager
from thresult import Result, Ok, Err
@Result[float]
def div(x: float, y: float) -> float:
z: float = x / y
return z
try:
with div(1.0, 0.0) as z:
# unreachable
pass
except ZeroDivisionError as e:
# exception happened
pass
Unwrapping Result in unwrap
Context Manager
@Result[float]
def f(x: float, y: float) -> float:
z: float = x / y
return z
try:
with unwrap():
r: float = f(1, 0)
# unreachable
except Exception as e:
# exception happened
raise e
Advanced Usage
Manually create Result value, and Structural Pattern Matching
from thresult import Result, Ok, Err
def div(x: float, y: float) -> Result[float, Exception]:
res: Result[float, Exception]
try:
# can raise "ZeroDivisionError: division by zero" exception
z: float = x / y
res = Ok[float](z)
except Exception as e:
res = Err[Exception](e)
return res
r0: Result = div(1.0, 2.0) # Ok
r1: Result = div(1.0, 0.0) # Err
match r0:
case Ok(v):
print('Ok, value:', v)
case Err(e):
print('Err, error:', e) # "ZeroDivisionError: division by zero"
match r1:
case Ok(v):
print('Ok, value:', v)
case Err(e):
print('Err, error:', e) # "ZeroDivisionError: division by zero"
z0: float = r0.unwrap() # 0.5
z1: float = r1.unwrap_or(float('inf')) # inf
z1: float = r1.unwrap() # raises "ZeroDivisionError: division by zero" exception
Decorate function with Result, and Structural Pattern Matching
from thresult import Result, Ok, Err
@Result[float, Exception]
def div(x: float, y: float) -> float:
# can raise "ZeroDivisionError: division by zero" exception
z: float = x / y
return z
r0: Result = div(1.0, 2.0) # Ok
r1: Result = div(1.0, 0.0) # Err
match r0:
case Ok(v):
print('Ok, value:', v)
case Err(e):
print('Err, error:', e) # "ZeroDivisionError: division by zero"
match r1:
case Ok(v):
print('Ok, value:', v)
case Err(e):
print('Err, error:', e) # "ZeroDivisionError: division by zero"
z0: float = r0.unwrap() # 0.5
z1: float = r1.unwrap_or(float('inf')) # inf
z1: float = r1.unwrap() # raises "ZeroDivisionError: division by zero" exception
Run / Develop Cycle
docker run --rm -it -v $PWD/thresult:/code -w /code python:3.11-alpine python -B result.py
Testing
docker-compose build thresult-test ; docker-compose run --rm -v $PWD:/test thresult-test
Coverage
docker-compose build thresult-coverage ; docker-compose run --rm -v $PWD:/test thresult-coverage
Building
docker-compose build thresult-build ; docker-compose run --rm thresult-build
Licensing
thresult
is licensed under the BSD 3 license.
Check the LICENSE for details
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
thresult-0.9.19.tar.gz
(6.7 kB
view details)
Built Distribution
File details
Details for the file thresult-0.9.19.tar.gz
.
File metadata
- Download URL: thresult-0.9.19.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.2 CPython/3.11.0 Linux/5.4.109+
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d9ab4973d82c05fc80b974d6e2d6ccd4c2705386ccfcc46c2831e180eb21aa5a |
|
MD5 | a0f9a6c333b78805a11e8b7b232e2167 |
|
BLAKE2b-256 | 71ccd8444f0cc3f2aa64eadadf1edaa17f775c925583b1076fd0414a2de25af1 |
File details
Details for the file thresult-0.9.19-py3-none-any.whl
.
File metadata
- Download URL: thresult-0.9.19-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.2 CPython/3.11.0 Linux/5.4.109+
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 52e1d5296e25f399420db88a3520f692acd17143d3bb53d2c43ba0c32375ce71 |
|
MD5 | 244ca369338021a7fb93a8c45415582f |
|
BLAKE2b-256 | 7b2a78bbfb7ec9ad2cb3775f58277bdaaa10cdee04d19ec3fb9780f0675929cd |