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.1.tar.gz
(4.8 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.1.tar.gz.
File metadata
- Download URL: dumb_maybe-0.0.1.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d564244d8f8dd5d712125669a325e0357b22d7dee03f9b3291c61771f7adaade
|
|
| MD5 |
c551aa915891845e0dad4d4988697169
|
|
| BLAKE2b-256 |
a2e7ef38eb23880c365bbd15b588d8598837047e823c295bfb013b6e54dec04b
|
File details
Details for the file dumb_maybe-0.0.1-py3-none-any.whl.
File metadata
- Download URL: dumb_maybe-0.0.1-py3-none-any.whl
- Upload date:
- Size: 4.0 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 |
a61a0ac773b90ab7237464b920dbc8adee8c7b01b099be7441a523523116ce74
|
|
| MD5 |
3bcdd971f3d5924d7b4c5d7162197f27
|
|
| BLAKE2b-256 |
96930bb9377508689ad0717f2cde7e69fabd4be16cf2ad243eeeded4c0c6a838
|