Skip to main content

PyOptional

optional object for python

How to install

pip install -U optional_python

How to use

Optional.of & Optional.of_nullable

from pyoptional import Optional

value = 1

final_value = Optional.of(value).map(lambda x: x + 1).or_else(333)
assert final_value == 1

final_value = Optional.of_nullable(None).map(lambda x: x + 1).or_else(333)
assert final_value == 333

Optional.or_raise

from pyoptional import Optional

Optional.of_nullable(None).or_raise(ValueError("value is None"))

Try.of

from pyoptional import Try


def func():
    return 1 / 0


def func2():
    return 2 / 1


def handle_exception(e: ZeroDivisionError):
    return "error"


result = Try.of(func).recover(handle_exception)
assert result.get() == "error"

result = Try.of(func2).recover(handle_exception)
assert result.get() == 2

result = Try.of(lambda: 1 / 000000000)

# no exception handler
assert result.get() is None

result = Try.of(lambda: 1 / 000000000, raise_exception=True)

result.get()  # will raise ZeroDivisionError

Metadata

Release files for optional-python 0.0.2

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

Source distribution (sdist)

Source distribution for optional-python 0.0.2
File Size Uploaded
optional_python-0.0.2.tar.gz 5.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for optional-python 0.0.2
File Interpreter ABI Platform
optional_python-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 12.8 kB

Release files / optional_python-0.0.2.tar.gz

Download URL optional_python-0.0.2.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
0132c1493650e9815a7e962b97ca8236326eadb819c58b8e1a25109b1c93469b
BLAKE2b-256 checksum
How to use checksums
7d806b0bfa13418ad4111fa1dc955d79dac60cee54fc2ed897ccc42731849bca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.0 CPython/3.10.9 Darwin/19.5.0

Release files / optional_python-0.0.2-py3-none-any.whl

Download URL optional_python-0.0.2-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
235d575232248e54e4959a4de1f5a4b6aeb6b54cb18f6b4aca0132526411e78d
BLAKE2b-256 checksum
How to use checksums
1be132ea49e2d158900cb2f61af27b40fe7b103b98b37ec3baa2e3ecffca8229
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.0 CPython/3.10.9 Darwin/19.5.0

Release history Release notifications | RSS feed

This release

0.0.2 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