Skip to content

Maybe


Classes

Empty

Bases: Maybe[VALUE]

Empty Maybe.

It doesn't wrap any value.

Functions

if_present
if_present(consumer: Callable[[VALUE], None]) -> None

Do nothing.

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

empty staticmethod
empty() -> Maybe[Any]

Returns a empty Maybe instance.

filter abstractmethod
filter(predicate: Predicate[VALUE]) -> Maybe[VALUE]

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
get() -> VALUE

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
if_present(consumer: Callable[[VALUE], None]) -> None

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
is_empty abstractmethod
is_empty() -> bool

Returns True if value is not present, otherwise False.

is_present abstractmethod
is_present() -> bool

Returns True if value is present, otherwise False.

map abstractmethod
map(
    mapper: Mapper[VALUE, Optional[OUTPUT]],
) -> Maybe[OUTPUT]

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
of(val: Optional[VALUE]) -> Maybe[VALUE]

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
or_else(other: Union[VALUE, Supplier[VALUE]]) -> VALUE

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
or_else_raise(exception: Exception) -> VALUE

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
or_none() -> Optional[VALUE]

Returns the value if present, else return None.

Returns:

Type Description
Optional[VALUE]

The value held by this Maybe if non-None value, otherwise None.

Some

Bases: Maybe[VALUE]

Valuated Maybe.

Functions

maybe

maybe(val: Optional[VALUE]) -> Maybe[VALUE]

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.