Skip to main content

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


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 hashes)

Uploaded Source

Built Distribution

fnsafe-0.0.1.post1-py3-none-any.whl (2.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page