Skip to main content
https://travis-ci.org/ekampf/pymaybe.svg?branch=master https://coveralls.io/repos/ekampf/pymaybe/badge.svg?branch=master&service=github https://img.shields.io/pypi/v/pymaybe.svg

A Python implementation of the Maybe pattern.

Installation

pip install pymaybe

Getting Started

from pymaybe import maybe
first_name = maybe(deep_hash)['account']['user_profile']['first_name'].or_else("<unknown>")

Documentation

Maybe monad is a programming pattern that allows to treat None values that same way as non-none values. This is done by wrapping the value, which may or may not be None to, a wrapper class.

The implementation includes two classes: Maybe and Something. Something represents a value while Nothing represents a None value. There’s also a method maybe which wraps a regular value and and returns Something or Nothing instance.

>>> maybe("I'm a value")
"I'm a value"

>>> maybe(None);
None

Both Something and Nothing implement 4 methods allowing you to test their real value: is_some, is_none, get and or_else

>>> maybe("I'm a value").is_some()
True

>>> maybe("I'm a value").is_none()
False

>>> maybe(None).is_some()
False

>>> maybe(None).is_none()
True

>>> maybe("I'm a value").get()
"I'm a value"

>>> maybe("I'm a value").or_else(lambda: "No value")
"I'm a value"

>>> maybe(None).get()
Traceback (most recent call last):
...
Exception: No such element

>>> maybe(None).or_else(lambda: "value")
'value'

>>> maybe(None).or_else("value")
'value'

In addition, Something and Nothing implement the Python magic methods allowing you to treat them as dictionaries:

::
>>> nested_dict = maybe(nested_dict)
>>> nested_dict['store']['name']
'MyStore'
>>> nested_dict['store']['address']
None
>>> nested_dict['store']['address']['street'].or_else('No Address Specified')
'No Address Specified'

All other method calls on Something are forwarded to its real value:

>>> maybe('VALUE').lower()
'value'

>>> maybe(None).invalid().method().or_else('unknwon')
'unknwon'

Examples & Use Cases

The Maybe pattern helps you avoid nasty try..except blocks. Consider the following code:

::
try:

url = rss.load_feeds()[0].url.domain

except (TypeError, IndexError, KeyError, AttributeError):

url = “planetpython.org”

With Maybe you could simply do:

url = maybe(rss).load_feeds()[0]['url'].domain.or_else("planetpython.org")

Getting the current logged in user’s name. Without maybe:

def get_user_zipcode():
    address = getattr(request.user, 'address', None)
    if address:
        return getattr(address, 'zipcode', '')

    return ''

With maybe:

def get_user_zipcode():
    return maybe(request.user).address.zipcode.or_else('')

Further Reading

History

0.1.0 (2015-01-11)

  • First release on PyPI.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pymaybe-0.1.6.tar.gz (16.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pymaybe-0.1.6-py2.py3-none-any.whl (7.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pymaybe-0.1.6.tar.gz.

File metadata

  • Download URL: pymaybe-0.1.6.tar.gz
  • Upload date:
  • Size: 16.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pymaybe-0.1.6.tar.gz
Algorithm Hash digest
SHA256 77ed1602e1b9e8b05dccd0d84891ede4ee2004728170e52af7f9c08d4c62378c
MD5 30c7a1af361f327abb0e58191072ae01
BLAKE2b-256 fe860bbefee601f7f5ce5fdd11ee59cbae36ce9cb48b96c44f960f38cbfe6d0a

See more details on using hashes here.

File details

Details for the file pymaybe-0.1.6-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pymaybe-0.1.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 01d0f71b98e1c155ecfecd9db44ce49f37ae374b5cb8f1d27c0ee00c3d7aa4dd
MD5 5e37221b3280109ea436700a996ec747
BLAKE2b-256 83f83bd91975bfbed25e453589d079cbf4f8f0b44a9cf6a82bfebbd69506b060

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.6 This release

2 files

0.1.5

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page