Maybe¶
Classes¶
Empty ¶
Maybe ¶
Bases: ABC, Generic[VALUE]
Wrapper class inspired by the well-known Optional API from Java.
A Maybe is container, a non-none value may be inside or may be not (hence the name Maybe as an alternative to Optional, which is already used by Python).
If a value is present: is_present will return True and get will return the value.
It useful when working with data that can be none.
Examples:
>>> import json
>>> beer = json.loads(json_beer)
>>> # Without maybe
>>> price = beer.get("BeerPrice")
>>> if price:
>>> if price > 0:
>>> price = convertDollarsToEuro(price)
>>> else:
>>> raise MyCustomException()
>>> # with Maybe
>>> Maybe.of(beer.get("BeerPrice")).
>>> filter(lambda price: price > 0).
>>> map(convertDollarsToEuro).
>>> or_else_raise(MyCustomException())
>>>
Functions¶
filter
abstractmethod
¶
Filter the wrapped value (if present).
If it matches the predicate, returns a Maybe describing the value, otherwise return an empty Maybe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Predicate[VALUE]
|
predicate function to apply to the value. |
required |
Returns:
| Type | Description |
|---|---|
Maybe[VALUE]
|
A Maybe containing the value if it matched the predicate, else an empty Maybe. |
get
abstractmethod
¶
Return the value if present, else raise EmptyElementException.
Returns:
| Type | Description |
|---|---|
VALUE
|
The non-None value contained in this Maybe. |
Raises:
| Type | Description |
|---|---|
EmptyElementException
|
if no value present. |
if_present
abstractmethod
¶
Invoke the given consumer with the value if present, do nothing otherwise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
consumer
|
Callable[[VALUE], None]
|
function to be executed if value present. |
required |
map
abstractmethod
¶
Map the wrapped value (if present).
Apply the given mapping function to the value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapper
|
Mapper[VALUE, Optional[OUTPUT]]
|
mapping function to apply to the value. |
required |
Returns:
| Type | Description |
|---|---|
Maybe[OUTPUT]
|
A Maybe containing the result of applying the mapping function on the value, |
Maybe[OUTPUT]
|
if value is present else an empty Maybe. |
of
staticmethod
¶
Returns a Maybe instance depends on the value provided.
If it's a none value, an empty Maybe will be returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
val
|
Optional[VALUE]
|
the provided value to wrap. |
required |
Returns:
| Type | Description |
|---|---|
Maybe[VALUE]
|
A Maybe containing the value, if non-None value, otherwise an empty Maybe. |
or_else
abstractmethod
¶
Returns the value if present, else return other.
If other is a supplier, it returns the result of the invocation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Union[VALUE, Supplier[VALUE]]
|
value to be return if no value present. if other is a supplier function, returns the invocation instead. |
required |
Returns:
| Type | Description |
|---|---|
VALUE
|
The value held by this Maybe if non-None value, otherwise either other or other invocation. |
or_else_raise
abstractmethod
¶
Returns the value if present, otherwise raise the given exception.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exception
|
Exception
|
The exception to be raised if no value present. |
required |
or_none
abstractmethod
¶
Returns the value if present, else return None.
Returns:
| Type | Description |
|---|---|
Optional[VALUE]
|
The value held by this |
Functions¶
maybe ¶
Returns a Maybe instance depends on the value provided.
If it's a none value, an empty Maybe will be returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
val
|
Optional[VALUE]
|
the provided value to wrap. |
required |
Returns:
| Type | Description |
|---|---|
Maybe[VALUE]
|
A Maybe containing the value, if non-None value, otherwise an empty Maybe. |