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
Release history Release notifications | RSS feed
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.2.tar.gz
(2.5 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dumb_maybe-0.0.2.tar.gz.
File metadata
- Download URL: dumb_maybe-0.0.2.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
58f1d0823bcc1e8e1e35d6f54426984afc8c59c897289dc66134a38b94508ead
|
|
| MD5 |
4607e7f3c1dbc0bcd0af879c221f5c72
|
|
| BLAKE2b-256 |
6a64a967c9b1c6703d0b4aae46199339c4c7cfe8421b3e31e284583cc040a3aa
|
File details
Details for the file dumb_maybe-0.0.2-py3-none-any.whl.
File metadata
- Download URL: dumb_maybe-0.0.2-py3-none-any.whl
- Upload date:
- Size: 3.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
58f62ec872323791a9747bbc39e51421a2b6d2b92a40d27cd4cff65addbbf4f2
|
|
| MD5 |
fe6bd3e32aad37fc7ea46f31f246decc
|
|
| BLAKE2b-256 |
fbfc12049cb12152567d478c4da261d6c8daab546e42493d37f0dfd44cd5c205
|