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 toOptional.of()
.PyOptional.of_nullable()
corresponds toOptional.ofNullable()
.PyOptional.empty()
corresponds toOptional.empty()
.PyOptional.is_empty()
corresponds toOptional.isEmpty()
.PyOptional.is_present()
corresponds toOptional.isPresent()
.PyOptional.filter()
corresponds toOptional.filter()
.PyOptional.get()
corresponds toOptional.get()
.PyOptional.or_else()
corresponds toOptional.orElse()
.PyOptional.or_else_get()
corresponds toOptional.orElseGet()
.PyOptional.or_else_raise()
corresponds toOptional.orElseThrow()
(both with and without a suppliedException
).PyOptional.or_else_throw
is an alias ofOptional.or_else_raise
.PyOptional.or_new_pyoptional()
corresponds toOptional.or()
(becauseor
is a Python keyword).PyOptional.if_present()
corresponds toOptional.ifPresent()
.PyOptional.if_present_or_else()
corresponds toOptional.ifPresentOrElse()
.PyOptional.map()
corresponds toOptional.map()
.PyOptional.flatMap()
corresponds toOptional.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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pyjoptional-1.0.3.tar.gz
.
File metadata
- Download URL: pyjoptional-1.0.3.tar.gz
- Upload date:
- Size: 16.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.10.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5fd1ba96db93f60ef8b2295067d9eac1a982eea96467c8df1dac8d314364fd31 |
|
MD5 | d82d4f15eb4f925c5a610e4685a63186 |
|
BLAKE2b-256 | 7282e281bb574d59848776e001a6c6d61b0f8146ed3307c30b98f1ebbd8ae5ab |
File details
Details for the file pyjoptional-1.0.3-py3-none-any.whl
.
File metadata
- Download URL: pyjoptional-1.0.3-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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a23bd60ec2fb8b86880459508c3c735bd86cc1074f39b8f04dbedbe3af84503 |
|
MD5 | 1d993937c5c0789fc1fc8a9348cfda12 |
|
BLAKE2b-256 | 958e1ab1f161872f62be7fe106f2abc6261cc5afc41b884c23712dff6ca8bf50 |