Skip to main content

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 ...

Metadata

Release files for fnsafe 0.0.1.post1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fnsafe 0.0.1.post1
File Size Uploaded
fnsafe-0.0.1.post1.tar.gz 2.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fnsafe 0.0.1.post1
File Interpreter ABI Platform
fnsafe-0.0.1.post1-py3-none-any.whl Python 3 none any Details

Total release size: 5.2 kB

Release files / fnsafe-0.0.1.post1.tar.gz

Download URL fnsafe-0.0.1.post1.tar.gz
Size 2.5 kB
Tags Source
SHA-256 checksum
How to use checksums
f13d0926e95c2a21e15839f971dc3b31ca6476fb7880385f5d58f60f191e57d6
BLAKE2b-256 checksum
How to use checksums
51b7ead32abbaf4ede7e1f74a25bfef4ffc1374f0e4a9e8547b934e000ad1109
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Release files / fnsafe-0.0.1.post1-py3-none-any.whl

Download URL fnsafe-0.0.1.post1-py3-none-any.whl
Size 2.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1f189c55f4afeaed0de3c60ba0646e622c4d380ee80414d99744c6a518474f46
BLAKE2b-256 checksum
How to use checksums
ba5b849762415a05b1468e6358015531786b32d32afa4c8576a1e04bb378ce2f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Release history Release notifications | RSS feed

This release

0.0.1.post1 This release

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page