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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 2 Python 3

File details

Details for the file opytional-0.1.0.tar.gz.

File metadata

  • Download URL: opytional-0.1.0.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for opytional-0.1.0.tar.gz
Algorithm Hash digest
SHA256 84eab1deca9fda36e8ed0c5743615f670264c479d73ae4cd5416b4701c927034
MD5 a16c801d831b7a375099b6bb7bbcc94a
BLAKE2b-256 8d870d2aa933b3689c57a9e431288d50fb618250c57ccb3aca03a62150733950

See more details on using hashes here.

File details

Details for the file opytional-0.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: opytional-0.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for opytional-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5504d36d3fa9b466d171336e92a11bde4dae52bf6878f26d539f8b688503f8e4
MD5 b1017884b362884caa4b7d9d7b315786
BLAKE2b-256 28f1d282b5fdecd6b579b7fcc09ea2cb8a69837dd197acfe4a8d0806f5f3f931

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