Monadic error handling implementation for python functions.
Project description
Py Monadic Error Handling Documentation
Instalation
The library is available via pip through the following command:
pip install py-monadic-error-handling
How to use
First import the safe decorator from py_monadic_error_handling.
from py_monadic_error_handling import safe
Then decorate your functions with the safe decorator.
@safe
def func1(n1, n2):
return n1 + n2
@safe
def func2(n1, n2):
return n1 / n2
Each of those functions will return a Ok or Err object, witch are subclasses of Result and may take Ok or Err objects as parameters.
If they return a Ok object, the function executed with no errors and the value can be accessed via the unwrap method.
res = func1(1, 0).unwrap()
print(res)
>>> 1
If they return a Err object, the function executed with an error and the error can be raised via the unwrap method.
unwrap takes no arguments and raise an exception or returns any
res = func2(1, 0).unwrap()
>>> ZeroDivisionError: division by zero
You can access the value of the Ok object directly by using the Ok property or the error type in an Err object directly by using the Err property.
res = func1(1, 0).Ok
print(res)
>>> 1
res = func2(1, 0).Err
print(res)
>>> division by zero
You can know if the function returned a Ok or Err object by using the is_ok and is_err methods.
Both methods take no arguments and return a bool
res = func1(1, 0).is_ok()
>>> True
res = func2(1, 0).is_err()
>>> True
You can safely unwrap an Ok or Err object with the unwrap_or method.
unwrap_or takes one argument of any type.
If the object is an instance of Ok, it returns object.Ok.
If the object is an instance of Err, it returns the argument you entered.
res = func1(1, 0).unwrap_or(-1)
print(res)
>>> 1
res = func2(1, 0).unwrap_or(-1)
print(res)
>>> -1
You can safely unwrap an Ok or Err object with the unwrap_or_else method.
unwrap_or_else takes a function as its argument.
The function takes an exception as its argument and may or may not return a value.
If the object is an instance of Ok, it returns object.Ok.
If the object is an instance of Err, it executes the function with the object.Err as its parameter and return its return value.
def handle_error(e):
print(e)
return -1
res = func1(1, 0).unwrap_or_else(handle_error)
print(res)
>>> 1
res = func2(1, 0).unwrap_or_else(handle_error)
>>> division by zero
print(res)
>>> -1
Railway Error Handling
By chaining functions decorated with safe, each function will excecute and if there is an error, it will return an Err object that will be caried through every function that depends on its return without executing them, othewise it will return an Ok value that can be taken as normal as argument for any other function decorated with safe
n1 = func1(1, 0)
>>> Ok(1)
n2 = func1(2, -2)
>>> Ok(0)
n3 = func2(n1, n2)
>>> Err(ZeroDivisionError)
n4 = func1(n3, 3)
>>> Err(ZeroDivisionError)
n5 = func1(n2, n1)
>>> Ok(0)
In this exemple, func1(n3, 3) is never executed because it depends on the return of a function that executed with an error, but func1(n1, 4) executes, because even though it executes later on the chain, it depends only on the return of functions that executed without any errors
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
File details
Details for the file py_monadic_error_handling-1.1.1.tar.gz.
File metadata
- Download URL: py_monadic_error_handling-1.1.1.tar.gz
- Upload date:
- Size: 2.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
47b0359bd586468c270049fb14218a560bb68e0a3c1ca12fd2fb8a3a5c4ec371
|
|
| MD5 |
3e7e6a91eb7b72200284670fa1954395
|
|
| BLAKE2b-256 |
e5455a14e0e5cf00e01fed11175a0a68b84e042ab72e66a259845bb1385db4ac
|