Not raising functions - like rust.
Project description
fnsafe
The fnsafe
library provides a decorator to
make your functions not raising errors. Instead
you are forced to handle the error.
The design is inspired by rust's Option and Result enums.
While the class's name is Result
, it provides
most of the functionality from rust's Option
enum
instead of rust's Result
enum.
pip install fnsafe
import fnsafe
@fnsafe.makesafe
def divide(a, b, /):
return a / b
result = divide(10, 0)
# Normally, this would raise an error since we're
# dividing through zero. The decorator turns the
# return value into a `Result` object.
print(result.unwrap())
# If the calculation succeded, it will be printed,
# otherwise it will throw an exception. However we
# can provide a default value:
print(result.unwrap_or(42))
import time
print(result.unwrap_or_else(lambda: time.time_ns()))
# Checking if a function succeded can be done by
# calling one of the `is_*` methods:
print(f"{result.is_none() = }")
print(f"{result.is_some() = }")
print(f"{result.is_some_and(lambda x: x % 2 == 0) = }")
# ... and much more feautures ...
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
fnsafe-0.0.1.post1.tar.gz
(2.5 kB
view details)
Built Distribution
File details
Details for the file fnsafe-0.0.1.post1.tar.gz
.
File metadata
- Download URL: fnsafe-0.0.1.post1.tar.gz
- Upload date:
- Size: 2.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f13d0926e95c2a21e15839f971dc3b31ca6476fb7880385f5d58f60f191e57d6 |
|
MD5 | 9b298b8f7e4959bd4ee75b09e733e771 |
|
BLAKE2b-256 | 51b7ead32abbaf4ede7e1f74a25bfef4ffc1374f0e4a9e8547b934e000ad1109 |
File details
Details for the file fnsafe-0.0.1.post1-py3-none-any.whl
.
File metadata
- Download URL: fnsafe-0.0.1.post1-py3-none-any.whl
- Upload date:
- Size: 2.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1f189c55f4afeaed0de3c60ba0646e622c4d380ee80414d99744c6a518474f46 |
|
MD5 | 1cc19831dbbfaeb0a6d1a96572e40ce5 |
|
BLAKE2b-256 | ba5b849762415a05b1468e6358015531786b32d32afa4c8576a1e04bb378ce2f |