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
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
optional_python-0.0.2.tar.gz
(5.9 kB
view hashes)
Built Distribution
Close
Hashes for optional_python-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 235d575232248e54e4959a4de1f5a4b6aeb6b54cb18f6b4aca0132526411e78d |
|
MD5 | 8a421bc9fe3871252ddc3a550de5b1f4 |
|
BLAKE2b-256 | 1be132ea49e2d158900cb2f61af27b40fe7b103b98b37ec3baa2e3ecffca8229 |