Skip to main content

opytional makes working with values that might be None safer and easier

Project description

opytional

https://img.shields.io/pypi/v/opytional.svg https://img.shields.io/travis/mmore500/opytional.svg Documentation Status

opytional makes working with values that might be None safer and easier

Inspired by C++’s std::optional.

import opytional as opyt



# opyt.or_value
# provides a fallback value when value is None

opyt.or_value(None, 'fallback') # returns 'fallback'

opyt.or_value('value', 'fallback') # returns 'value'



# opyt.or_else
# provides a fallback callable when value is None

opyt.or_else(None, lambda: 'fallback') # returns 'fallback'

opyt.or_else('value', lambda: 'fallback') # returns 'value'



# opyt.apply_if
# applies an operator to value when value is not None

opyt.apply_if(None, lambda x: x + ' world') # returns None

opyt.apply_if('hello', lambda x: x + ' world') # returns 'hello world'



# opyt.apply_if_or_value
# applies an operator to value when value is not None
# with a fallback value for when value is None

opyt.apply_if_or_value(None, lambda x: x + ' world', 'fallback')
# returns 'fallback'

opyt.apply_if_or_value('hello', lambda x: x + ' world', 'fallback')
# returns 'hello world'

opyt.apply_if_or_value('hello', lambda x: None, 'fallback') # returns None



# opyt.apply_if_or_else
# applies an operator to value when value is not None
# with a fallback callable for when value is None

opyt.apply_if_or_else(None, lambda x: x + ' world', lambda: 'fallback')
# returns 'fallback'

opyt.apply_if_or_value('hello', lambda x: x + ' world', lambda: 'fallback')
# returns 'hello world'

opyt.apply_if_or_value('hello', lambda x: None, lambda: 'fallback')
# returns None

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

History

0.1.0 (2022-02-22)

  • First release on PyPI.

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

opytional-0.1.0.tar.gz (11.1 kB view hashes)

Uploaded Source

Built Distribution

opytional-0.1.0-py2.py3-none-any.whl (5.8 kB view hashes)

Uploaded Python 2 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