Skip to main content

Optional object for python

Project description

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

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

optional_python-0.0.2.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

optional_python-0.0.2-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file optional_python-0.0.2.tar.gz.

File metadata

  • Download URL: optional_python-0.0.2.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.0 CPython/3.10.9 Darwin/19.5.0

File hashes

Hashes for optional_python-0.0.2.tar.gz
Algorithm Hash digest
SHA256 0132c1493650e9815a7e962b97ca8236326eadb819c58b8e1a25109b1c93469b
MD5 a9a5ed42e6a72d41e5aef2377c91c42d
BLAKE2b-256 7d806b0bfa13418ad4111fa1dc955d79dac60cee54fc2ed897ccc42731849bca

See more details on using hashes here.

Provenance

File details

Details for the file optional_python-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: optional_python-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.0 CPython/3.10.9 Darwin/19.5.0

File hashes

Hashes for optional_python-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 235d575232248e54e4959a4de1f5a4b6aeb6b54cb18f6b4aca0132526411e78d
MD5 8a421bc9fe3871252ddc3a550de5b1f4
BLAKE2b-256 1be132ea49e2d158900cb2f61af27b40fe7b103b98b37ec3baa2e3ecffca8229

See more details on using hashes here.

Provenance

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