Skip to main content

A Very Dumb Maybe Type

Project description

A Very Dumb Maybe Type

What Makes It Dumb?

I does, stupid.

How Do I Use It?

Please don't.

OK, But Like, Suppose I Wanted to

Maybe lets you continue to chain calls when they might fail or return None, without long if-else chains:

>>> from maybe import maybe, get, attr
>>> class Order:
...     def __init__(self, customer):
...         self.customer = customer
... 
>>> order_no_contacts = Order({"fullname": "John Doe"})
>>> order_empty_contacts = Order({"contacts": []})
>>> correct_order = Order({"contacts": [{"type": "email", "address": "user@example.com"}]})
>>> 
>>> def get_contact_address(order):
...     return (
...             maybe(order)
...             .then(attr("customer"))
...             .then(get("contacts"))
...             .then(get(0))
...             .then(get("address"))
...             .value()
...         )
... 
>>> print(get_contact_address(None))
None
>>> print(get_contact_address(order_no_contacts))
None
>>> print(get_contact_address(order_empty_contacts))
None
>>> print(get_contact_address(correct_order))
user@example.com
>>> 

It also allows you to act on exceptions:

>>> def get_contact_address(order):
...     return (
...         maybe(order)
...         .then(attr("customer"))
...         # If the order doesn't have .customer, try treating
...         # the order as a customer
...         .catch(AttributeError, lambda e: order)
...         .then(get("contacts"))
...         .then(get(0))
...         .then(get("address"))
...         .value()
...     )
... 
>>> print(get_contact_address({"contacts": [{"type": "email", "address": "second-user@example.com"}]}))
second-user@example.com
>>> print(get_contact_address(correct_order))
user@example.com
>>> 

Or on null values:

>>> def get_contact_address(order):
...     return (
...         maybe(order)
...         .then(attr("customer"))
...         .catch(AttributeError, lambda e: order)
...         .then(get("contacts"))
...         .then(get(0))
...         .then(get("address"))
...         .or_else(lambda: "default-address@example.com")
...         .value()
...     )
... 
>>> order_none_contact = Order({"contacts": [{"type": "email", "address": None}]})
>>> print(get_contact_address(None))
default-address@example.com
>>> print(get_contact_address(order_none_contact))
default-address@example.com
>>> print(get_contact_address(correct_order))
user@example.com

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

dumb_maybe-0.0.4.tar.gz (2.5 kB view details)

Uploaded Source

Built Distribution

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

dumb_maybe-0.0.4-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file dumb_maybe-0.0.4.tar.gz.

File metadata

  • Download URL: dumb_maybe-0.0.4.tar.gz
  • Upload date:
  • Size: 2.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.7

File hashes

Hashes for dumb_maybe-0.0.4.tar.gz
Algorithm Hash digest
SHA256 2c0f474256b6af1d3e2dc0fcbead2d87c5d57a6af98bdce79d165ff606f1d699
MD5 40fc438ca813a5054cd8c1b9aed83bd7
BLAKE2b-256 a68c6a4a4e262a5c0e068248983a2fcd95e0f1c13c0b8553a4165f92f100797c

See more details on using hashes here.

File details

Details for the file dumb_maybe-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: dumb_maybe-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 3.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.7

File hashes

Hashes for dumb_maybe-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 3f1955a6331cdc00d3ba431e4d8f6cbd5d2f7ecbdf431b28bd1a5d61414cc89b
MD5 d6f9b873549c0837d935943c9179bf9b
BLAKE2b-256 77ce4503f724533c76c496a654af2fe115de2668f407401d4e709ad7b6a1e54f

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