Skip to main content

A Python module providing `PyOptional`, a Java-like `Optional` type for Python 3.9+.

Project description

PyJOptional

A Python module providing the PyOptional class/type, a Java-like Optional wrapper for Python values.

HOWTO

user@machine:~$ pip install pyjoptional
from pyoptional.pyoptional import PyOptional

p: PyOptional[int] = PyOptional.of(1)
p1: PyOptional = PyOptional.of_nullable(None)
p2: PyOptional = PyOptional.empty()

Comparison with the Java Optional

  • PyOptional.of() corresponds to Optional.of().
  • PyOptional.of_nullable() corresponds to Optional.ofNullable().
  • PyOptional.empty() corresponds to Optional.empty().
  • PyOptional.is_empty() corresponds to Optional.isEmpty().
  • PyOptional.is_present() corresponds to Optional.isPresent().
  • PyOptional.filter() corresponds to Optional.filter().
  • PyOptional.get() corresponds to Optional.get().
  • PyOptional.or_else() corresponds to Optional.orElse().
  • PyOptional.or_else_get() corresponds to Optional.orElseGet().
  • PyOptional.or_else_raise() corresponds to Optional.orElseThrow() (both with and without a supplied Exception).
  • PyOptional.or_else_throw is an alias of Optional.or_else_raise.
  • PyOptional.or_new_pyoptional() corresponds to Optional.or() (because or is a Python keyword).
  • PyOptional.if_present() corresponds to Optional.ifPresent().
  • PyOptional.if_present_or_else() corresponds to Optional.ifPresentOrElse().
  • PyOptional.map() corresponds to Optional.map().
  • PyOptional.flatMap() corresponds to Optional.flatMap().

Additionally, PyOptional.__eq__, PyOptional.__hash__, PyOptional.__str__, and PyOptional.__repr__ override the default Python implementation of such methods, much like the Java Optional.equals(), Optional.hashCode(), and Optional.toString() methods.

Finally, there is no correspondent of the Java Optional.stream() method, as Python has no native Stream class.

Comparison with the Python Optional

The native Python Optional[SomeType] type is just syntactic sugar for SomeType | None. Therefore, it does not provide any of the API methods of PyOptional.

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

pyjoptional-1.1.3.tar.gz (18.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyjoptional-1.1.3-py3-none-any.whl (18.1 kB view details)

Uploaded Python 3

File details

Details for the file pyjoptional-1.1.3.tar.gz.

File metadata

  • Download URL: pyjoptional-1.1.3.tar.gz
  • Upload date:
  • Size: 18.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for pyjoptional-1.1.3.tar.gz
Algorithm Hash digest
SHA256 3520655c04bb63839ad52a68960179a5c74c4f03dc8d1f74dc578f7ffbd05bfd
MD5 3623b831739f251ca18232f87e3351ec
BLAKE2b-256 813ac595e4d5df6a1fb0af30dcb402bc4bb7e9247ba2789ff3dcf0a7f99a58b1

See more details on using hashes here.

File details

Details for the file pyjoptional-1.1.3-py3-none-any.whl.

File metadata

  • Download URL: pyjoptional-1.1.3-py3-none-any.whl
  • Upload date:
  • Size: 18.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for pyjoptional-1.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 79dac11b714fa53d3ad8bbff38f208f7801455cbab329837aa9827d45ffd427b
MD5 c8109a1e157956f5af7ad6656f6f6121
BLAKE2b-256 781783c0d7cd9bae57515e91928f60aa81d3d81301db0b8dc67981d29ec1818c

See more details on using hashes here.

Supported by

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