Skip to main content

Real Optional type in python, not @Nullable annotation.

Project description

IceSpringRealOptional

Real Optional type in python, not @Nullable annotation.

Official sites

Features

  • All Java 8 style Optional API support
  • All Generic Type annotation support

Install

  • PyPI: pip install IceSpringRealOptional

Usage

from IceSpringRealOptional.maybe import Maybe

maybe = Maybe.ofNullable("CommonSense")
print("{}: isPresent={}".format(maybe, maybe.isPresent()))
print("{}: value={}".format(maybe, maybe.get()))
maybe.ifPresent(lambda x: print(f"{x} exist"))
print("{}'s length: {}".format(maybe, maybe.map(len)))

empty = Maybe.empty()
print(empty.orElse("{} is empty".format(empty)))
print(empty.orElseGet(lambda: "{} is empty again".format(empty)))

try:
    Maybe.empty().orElseThrow(lambda: RuntimeError("Unlucky"))
except RuntimeError as e:
    print("Runtime error caught: {}".format(e))

Example Output

<Maybe:CommonSense>: isPresent=True
<Maybe:CommonSense>: value=CommonSense
CommonSense exist
<Maybe:CommonSense>'s length: <Maybe:11>
<Maybe:None> is empty
<Maybe:None> is empty again
Runtime error caught: Unlucky

License

MIT

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

IceSpringRealOptional-1.2.4.tar.gz (2.7 kB view hashes)

Uploaded Source

Built Distribution

IceSpringRealOptional-1.2.4-py3-none-any.whl (6.0 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