Skip to main content

A Python implementation of the Maybe pattern.

Project description

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['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

Parsing deep JSON objects where some items might be missing. Without maybe:

stores = json.get('stores')
if stores:
    products = stores[0].get('products')
    if products:
        product_name = products[0].get('details', {}).get('name') or 'unknown'

With maybe:

product_name = maybe(stores)[0]['products'][0]['details']['name'].or_else('unknown')

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

def get_user_name():
    current_user = request.user
    if current_user:
        return current_user.name

    return ''

With maybe:

def get_user_name():
    return maybe(request.user).name.or_else('')

Further Reading

History

0.1.0 (2015-01-11)

  • 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

pymaybe-0.1.5.tar.gz (16.5 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.5-py2.py3-none-any.whl (7.6 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

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

File hashes

Hashes for pymaybe-0.1.5.tar.gz
Algorithm Hash digest
SHA256 d01d183fa656ca06ccb5be05396d000be12efc8ea9f479446e39adea4855552d
MD5 ccd225bc71e63cdcfcb93c5c7ef1dc7b
BLAKE2b-256 00414fe3ee0ba2ad2350f5352593b9f94b5b1003585d9cc3999c2aeab7a4cb56

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pymaybe-0.1.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 581f9fa5d9ae6176ef4cf40c1ecbab4eea274e8cdd554484957750b31e55814c
MD5 a597f496b437bc269db736ec6aba4bb0
BLAKE2b-256 bb2a4e07b4813f67ff615b0954885196b82b5c02eff2c7a6af2b55b6011196d9

See more details on using hashes here.

Supported by

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