Skip to main content

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

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.0.4.tar.gz (16.2 kB view details)

Uploaded Source

Built Distribution

pyjoptional-1.0.4-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyjoptional-1.0.4.tar.gz
  • Upload date:
  • Size: 16.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.9

File hashes

Hashes for pyjoptional-1.0.4.tar.gz
Algorithm Hash digest
SHA256 1dbffda1de6b29dcb07d7e4a312b10d8d227b96b49b2273d5fc81702f3da5bd3
MD5 8ac4ecf3e061a50a004319a279c2e028
BLAKE2b-256 8c5dda4a259cd4f39d7b4563ff26673db1362315e5019aa4a78991a10b58a6e8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyjoptional-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 16.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.9

File hashes

Hashes for pyjoptional-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 1e7294e0d0aa1f49e2377fd41139c8fc79cf80f56f9234af9634b813de275abe
MD5 8ade1f8cc0cf7b6cdc7b24d930c28482
BLAKE2b-256 fe126fe9af22e5e1fbcd93cba486f26caf37637b1ccf5ef143016302ad2d8c91

See more details on using hashes here.

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