gluonts.maybe module#
This module contains functions that work on Optional values. In contrast to
other approaches, this does not wrap values into a dedicated type, but works
on normal Python values, which are of type Optional[T].
Thus, some functions are implemented identically but have different type
signatures. For example, both map and and_then both just apply a
function to a value if it is not None, but the result of map is T
and the result of and_then is Optional[T].
Each function is implemented twice, as a simple function and as a method on
maybe.Maybe:
maybe.Maybe(1).map(fn)
maybe.map(1, fn)
The names are taken from Rust, see: https://doc.rust-lang.org/stable/std/option/enum.Option.html
Note
The argument order for map_or and map_or_else is reversed
compared to their Rust counterparts.
do is not implemented in Rust but mimics toolz.do instead.
- class gluonts.maybe.Maybe(*args, **kwds)[source]#
Bases:
Generic[gluonts.maybe.T]- and_(other: Optional[gluonts.maybe.U]) Optional[gluonts.maybe.U][source]#
Like
a and bin Python, except only consideringNone.This is implement identical to
unwrap_or, but different with respect to types.>>> Maybe(1).and_(2) 2 >>> Maybe(1).and_(None) >>> Maybe(None).and_(2)
- and_then(fn: Callable[[gluonts.maybe.T], Optional[gluonts.maybe.U]]) Optional[gluonts.maybe.U][source]#
Apply
fntovalif it is notNoneand return the result.In contrast to
map,fnalways returns anOptional, which is consequently flattened.>>> Maybe([42]).and_then(lambda xs: xs[0] if xs else None) 42 >>> Maybe([]).and_then(lambda xs: xs[0] if xs else None) >>> Maybe(None).and_then(lambda xs: xs[0] if xs else None)
- contains(other: gluonts.maybe.U) bool[source]#
Check if
valequalsother, always returnFalseifvalisNone.>>> Maybe(1).contains(1) True >>> Maybe(1).contains(2) False >>> Maybe(None).contains(3) False
- do(fn: Callable[[gluonts.maybe.T], gluonts.maybe.U]) Optional[gluonts.maybe.T][source]#
Apply
fntovalthen returnval, ifvalis notNone.>>> Maybe("a").do(print) a 'a' >>> Maybe(None).do(print)
- expect(msg: str) gluonts.maybe.T[source]#
Ensure that
valis notNone, raises aValueErrorusingmsgotherwise.>>> Maybe(1).expect("My message") 1 >>> Maybe(None).expect("My message") Traceback (most recent call last): ... ValueError: My message
- filter(pred: Callable[[gluonts.maybe.T], bool]) Optional[gluonts.maybe.T][source]#
Return
NoneifvalisNoneor ifpred(val)does not returnTrue, otherwise returnval.>>> is_even = lambda n: n % 2 == 0 >>> Maybe(1).filter(is_even) >>> Maybe(2).filter(is_even) 2 >>> Maybe(None).filter(is_even)
- flatten() Optional[gluonts.maybe.T][source]#
Flatten nested optional value.
Note: This just returns the value, but changes the type from
Optional[Optional[T]]toOptional[T].
- map(fn: Callable[[gluonts.maybe.T], gluonts.maybe.U]) Optional[gluonts.maybe.U][source]#
Apply
fntovalifvalis notNone.>>> Maybe(1).map(lambda x: x + 1) 2 >>> Maybe(None).map(lambda x: x + 1)
- map_or(fn: Callable[[gluonts.maybe.T], gluonts.maybe.U], default: gluonts.maybe.U) gluonts.maybe.U[source]#
Apply
fntovalifvalis notNoneand return the result. In case ofNonethe provideddefaultis returned instead.This is similar to calling
mapandunwrap_orin succession.>>> Maybe(["x"]).map_or(len, 0) 1 >>> Maybe(None).map_or(len, 0) 0
- map_or_else(fn: Callable[[gluonts.maybe.T], gluonts.maybe.U], factory: Callable[[], gluonts.maybe.U]) gluonts.maybe.U[source]#
Similar to
map_or, except that the returned value is lazily evaluated.This is similar to calling
mapandunwrap_or_elsein succession.>>> Maybe(1).map_or_else(lambda n: [n], list) [1] >>> Maybe(None).map_or_else(lambda n: [n], list) []
- or_(default: Optional[gluonts.maybe.T]) Optional[gluonts.maybe.T][source]#
Like
a or bin Python, except only consideringNone.>>> Maybe(1).or_(2) 1 >>> Maybe(1).or_(None) 1 >>> Maybe(None).or_(2) 2
- or_else(factory: Callable[[], Optional[gluonts.maybe.T]]) Optional[gluonts.maybe.T][source]#
Like unwrap_or_else, except that it returns an optional value.
>>> Maybe([42]).or_else(list) [42] >>> Maybe(None).or_else(list) []
- unwrap() gluonts.maybe.T[source]#
Assert that the value is not
None.>>> Maybe(1).unwrap() 1 >>> Maybe(None).unwrap() Traceback (most recent call last): ... ValueError: Trying to unwrap `None` value.
- unwrap_or(default: gluonts.maybe.T) gluonts.maybe.T[source]#
Get
valif it is notNone, ordefaultotherwise.>>> Maybe(1).unwrap_or(2) 1 >>> Maybe(None).unwrap_or(2) 2
- unwrap_or_else(fn: Callable[[], gluonts.maybe.T]) gluonts.maybe.T[source]#
Get
valif it is notNone, or invokefactoryto get a fallback.>>> Maybe([1, 2, 3]).unwrap_or_else(list) [1, 2, 3] >>> Maybe(None).unwrap_or_else(list) []
- val: Optional[gluonts.maybe.T]#
- gluonts.maybe.and_(val: Optional[gluonts.maybe.T], other: Optional[gluonts.maybe.U]) Optional[gluonts.maybe.U][source]#
Like
a and bin Python, except only consideringNone.This is implement identical to
unwrap_or, but different with respect to types.>>> and_(1, 2) 2 >>> and_(1, None) >>> and_(None, 2)
- gluonts.maybe.and_then(val: Optional[gluonts.maybe.T], fn: Callable[[gluonts.maybe.T], Optional[gluonts.maybe.U]]) Optional[gluonts.maybe.U][source]#
Apply
fntovalif it is notNoneand return the result.In contrast to
map,fnalways returns anOptional, which is consequently flattened.>>> and_then([42], lambda xs: xs[0] if xs else None) 42 >>> and_then(None, lambda xs: xs[0] if xs else None)
- gluonts.maybe.contains(val: Optional[gluonts.maybe.T], other: gluonts.maybe.U) bool[source]#
Check if
valequalsother, always returnFalseifvalisNone.>>> contains(1, 1) True >>> contains(1, 2) False >>> contains(None, 3) False
- gluonts.maybe.do(val: Optional[gluonts.maybe.T], fn: Callable[[gluonts.maybe.T], gluonts.maybe.U]) Optional[gluonts.maybe.T][source]#
Apply
fntovalthen returnval, ifvalis notNone.>>> do("a", print) a 'a' >>> do(None, print)
- gluonts.maybe.expect(val: Optional[gluonts.maybe.T], msg: str) gluonts.maybe.T[source]#
Ensure that
valis notNone, raises aValueErrorusingmsgotherwise.>>> expect(1, "My message") 1 >>> expect(None, "My message") Traceback (most recent call last): ... ValueError: My message
- gluonts.maybe.filter(val: Optional[gluonts.maybe.T], pred: Callable[[gluonts.maybe.T], bool]) Optional[gluonts.maybe.T][source]#
Return
NoneifvalisNoneor ifpred(val)does not returnTrue, otherwise returnval.>>> is_even = lambda n: n % 2 == 0 >>> filter(1, is_even) >>> filter(2, is_even) 2 >>> filter(None, is_even)
- gluonts.maybe.flatten(val: Optional[gluonts.maybe.T]) Optional[gluonts.maybe.T][source]#
Flatten nested optional value.
Note: This just returns the value, but changes the type from
Optional[Optional[T]]toOptional[T].
- gluonts.maybe.map(val: Optional[gluonts.maybe.T], fn: Callable[[gluonts.maybe.T], gluonts.maybe.U]) Optional[gluonts.maybe.U][source]#
Apply
fntovalifvalis notNone.>>> map(1, lambda x: x + 1) 2 >>> map(None, lambda x: x + 1)
- gluonts.maybe.map_or(val: Optional[gluonts.maybe.T], fn: Callable[[gluonts.maybe.T], gluonts.maybe.U], default: gluonts.maybe.U) gluonts.maybe.U[source]#
Apply
fntovalifvalis notNoneand return the result. In case ofNonethe provideddefaultis returned instead.This is similar to calling
mapandunwrap_orin succession.>>> map_or(["x"], len, 0) 1 >>> map_or(None, len, 0) 0
- gluonts.maybe.map_or_else(val: Optional[gluonts.maybe.T], fn: Callable[[gluonts.maybe.T], gluonts.maybe.U], factory: Callable[[], gluonts.maybe.U]) gluonts.maybe.U[source]#
Similar to
map_or, except that the returned value is lazily evaluated.This is similar to calling
mapandunwrap_or_elsein succession.>>> map_or_else(1, lambda n: [n], list) [1] >>> map_or_else(None, lambda n: [n], list) []
- gluonts.maybe.or_(val: Optional[gluonts.maybe.T], default: Optional[gluonts.maybe.T]) Optional[gluonts.maybe.T][source]#
Like
a or bin Python, except only consideringNone.>>> or_(1, 2) 1 >>> or_(1, None) 1 >>> or_(None, 2) 2
- gluonts.maybe.or_else(val: Optional[gluonts.maybe.T], factory: Callable[[], Optional[gluonts.maybe.T]]) Optional[gluonts.maybe.T][source]#
Like
unwrap_or_else, except that it returns an optional value.>>> or_else([42], list) [42] >>> or_else(None, list) []
- gluonts.maybe.unwrap(val: Optional[gluonts.maybe.T]) gluonts.maybe.T[source]#
Assert that the value is not
None.>>> unwrap(1) 1 >>> unwrap(None) Traceback (most recent call last): ... ValueError: Trying to unwrap `None` value.
- gluonts.maybe.unwrap_or(val: Optional[gluonts.maybe.T], default: gluonts.maybe.T) gluonts.maybe.T[source]#
Get
valif it is notNone, ordefaultotherwise.>>> unwrap_or(1, 2) 1 >>> unwrap_or(None, 2) 2