gluonts.maybe 模块#
此模块包含处理 Optional
类型值的函数。它支持将值包装到专用类型 (Maybe
) 中,但也适用于普通的 Python 值,这些值的类型为 Optional[T]
。
每个函数都实现了两次,一次是简单的函数,一次是 maybe.Maybe
类的方法。
maybe.Some(1).map(fn) -> Maybe[T]
maybe.map(1, fn) -> Optional[T]
Maybe
类的方法返回 Maybe
类型,而函数返回 Optional
类型的值。
这些名称取自 Rust,详见:https://doc.rust-lang.net.cn/stable/std/option/enum.Option.html
注意
与 Rust 中的对应函数相比,map_or
和 map_or_else
的参数顺序是相反的。
do
在 Rust 中没有实现,而是模仿了 toolz.do
。
- class gluonts.maybe.Maybe(*args, **kwds)[源代码]#
基类:
abc.ABC
,Generic
[gluonts.maybe.T
]- abstract and_(other: Union[gluonts.maybe.U, None, Maybe[T]]) gluonts.maybe.Maybe[gluonts.maybe.U] [源代码]#
类似于 Python 中的
a and b
,但只考虑None
的情况。其实现与
unwrap_or
相同,但在类型方面有所不同。>>> Some(1).and_(2) Some(2) >>> Some(1).and_(None) Nothing >>> Nothing.and_(2) Nothing
- abstract and_then(fn: Callable[Concatenate[T, P], OptionalOrMaybe[U]], *args: P.args, **kwargs: P.kwargs) Maybe[U] [源代码]#
如果
val
不是None
,则将fn
应用于val
并返回结果。与
map
不同,fn
总是返回一个Optional
,然后将其展平。>>> Some([42]).and_then(lambda xs: xs[0] if xs else None) Some(42) >>> Some([]).and_then(lambda xs: xs[0] if xs else None) Nothing >>> Nothing.and_then(lambda xs: xs[0] if xs else None) Nothing
- abstract contains(other: gluonts.maybe.U) bool [源代码]#
检查
val
是否等于other
,如果val
是None
,则始终返回False
。>>> Some(1).contains(1) True >>> Some(1).contains(2) False >>> Nothing.contains(3) False
- abstract do(fn: Callable[[gluonts.maybe.T], gluonts.maybe.U]) gluonts.maybe.Maybe[gluonts.maybe.T] [源代码]#
如果
val
不是None
,则将fn
应用于val
,然后返回val
。>>> Some("a").do(print) a Some('a') >>> Nothing.do(print) Nothing
- abstract expect(msg: str) gluonts.maybe.T [源代码]#
确保
val
不是None
,否则使用msg
引发ValueError
。>>> Some(1).expect("My message") 1 >>> Nothing.expect("My message") Traceback (most recent call last): ... ValueError: My message
- abstract filter(pred: Callable[[gluonts.maybe.T], bool]) gluonts.maybe.Maybe[gluonts.maybe.T] [源代码]#
如果
val
是None
或pred(val)
返回False
,则返回None
,否则返回val
。>>> is_even = lambda n: n % 2 == 0 >>> Some(1).filter(is_even) Nothing >>> Some(2).filter(is_even) Some(2) >>> Nothing.filter(is_even) Nothing
- abstract flatten() gluonts.maybe.Maybe[gluonts.maybe.T] [源代码]#
展平嵌套的可选值。
注意:这仅返回原始值,但将类型从
Optional[Optional[T]]
更改为Optional[T]
。
- abstract map(fn: Callable[[gluonts.maybe.T, gluonts.maybe.P], gluonts.maybe.U], *args, **kwargs) gluonts.maybe.Maybe[gluonts.maybe.U] [源代码]#
如果
val
不是None
,则将fn
应用于val
。>>> Some(1).map(lambda x: x + 1) Some(2) >>> Nothing.map(lambda x: x + 1) Nothing
允许传递额外的参数给
fn
。>>> Some(10).map(divmod, 3) Some((3, 1))
- abstract map_or(fn: Callable[[gluonts.maybe.T], gluonts.maybe.U], default: gluonts.maybe.U) gluonts.maybe.U [源代码]#
如果
val
不是None
,则将fn
应用于val
并返回结果。如果是None
,则返回提供的default
。这类似于连续调用
map
和unwrap_or
。>>> Some(["x"]).map_or(len, 0) 1 >>> Nothing.map_or(len, 0) 0
- abstract map_or_else(fn: Callable[[gluonts.maybe.T], gluonts.maybe.U], factory: Callable[[], gluonts.maybe.U]) gluonts.maybe.U [源代码]#
类似于
map_or
,但返回的值是延迟计算的。这类似于连续调用
map
和unwrap_or_else
。>>> Some(1).map_or_else(lambda n: [n], list) [1] >>> Nothing.map_or_else(lambda n: [n], list) []
- abstract or_(default: Optional[gluonts.maybe.T]) gluonts.maybe.Maybe[gluonts.maybe.T] [源代码]#
类似于 Python 中的
a or b
,但只考虑None
的情况。>>> Some(1).or_(2) Some(1) >>> Some(1).or_(None) Some(1) >>> Nothing.or_(2) Some(2)
- abstract or_else(factory: Callable[[], Optional[gluonts.maybe.T]]) gluonts.maybe.Maybe[gluonts.maybe.T] [源代码]#
类似于 unwrap_or_else,但它返回一个可选值。
>>> Some([42]).or_else(list) Some([42]) >>> Nothing.or_else(list) Some([])
- abstract unbox() Optional[gluonts.maybe.T] [源代码]#
将
Maybe[T]
转换为Optional[T]
。>>> Some(1).unbox() 1 >>> Some(None).unbox() is None True
- abstract unwrap() gluonts.maybe.T [源代码]#
断言该值不是
None
。>>> Some(1).unwrap() 1 >>> Nothing.unwrap() Traceback (most recent call last): ... ValueError: Trying to unwrap `None` value.
- abstract unwrap_or(default: gluonts.maybe.T) gluonts.maybe.T [源代码]#
如果
val
不是None
,则获取val
,否则获取default
。>>> Some(1).unwrap_or(2) 1 >>> Nothing.unwrap_or(2) 2
- abstract unwrap_or_else(fn: Callable[[], gluonts.maybe.T]) gluonts.maybe.T [源代码]#
如果
val
不是None
,则获取val
,否则调用factory
获取一个备用值。>>> Some([1, 2, 3]).unwrap_or_else(list) [1, 2, 3] >>> Nothing.unwrap_or_else(list) []
- abstract xor(other: Union[gluonts.maybe.T, None, Maybe[T]]) gluonts.maybe.Maybe[gluonts.maybe.T] [源代码]#
如果其中一个为
None
,则返回另一个。如果两者均不为None
,则返回None
。>>> xor(1, None) 1 >>> xor(None, 2) 2 >>> xor(1, 2) >>> xor(None, None)
- abstract zip(other: Union[gluonts.maybe.U, None, Maybe[T]]) gluonts.maybe.Maybe[Tuple[gluonts.maybe.T, gluonts.maybe.U]] [源代码]#
抽象的 zip 方法。
- abstract zip_with(other: Union[gluonts.maybe.U, None, Maybe[T]], fn: Callable[[gluonts.maybe.T, gluonts.maybe.U], gluonts.maybe.R]) gluonts.maybe.Maybe[gluonts.maybe.R] [源代码]#
如果两个可选值均不为
None
,则将函数应用于这两个值。>>> add = lambda left, right: left + right >>> Some(1).zip_with(2, add) Some(3) >>> Some(1).zip_with(None, add) Nothing >>> Nothing.zip_with(2, add) Nothing
- class gluonts.maybe.Some(*args, **kwds)[源代码]#
基类:
gluonts.maybe.Maybe
[gluonts.maybe.T
]- and_(other: Union[gluonts.maybe.U, None, Maybe[T]]) gluonts.maybe.Maybe[gluonts.maybe.U] [源代码]#
类似于 Python 中的
a and b
,但只考虑None
的情况。其实现与
unwrap_or
相同,但在类型方面有所不同。>>> Some(1).and_(2) Some(2) >>> Some(1).and_(None) Nothing >>> Nothing.and_(2) Nothing
- and_then(fn: Callable[Concatenate[T, P], OptionalOrMaybe[U]], *args: P.args, **kwargs: P.kwargs) Maybe[U] [源代码]#
如果
val
不是None
,则将fn
应用于val
并返回结果。与
map
不同,fn
总是返回一个Optional
,然后将其展平。>>> Some([42]).and_then(lambda xs: xs[0] if xs else None) Some(42) >>> Some([]).and_then(lambda xs: xs[0] if xs else None) Nothing >>> Nothing.and_then(lambda xs: xs[0] if xs else None) Nothing
- contains(other: gluonts.maybe.U) bool [源代码]#
检查
val
是否等于other
,如果val
是None
,则始终返回False
。>>> Some(1).contains(1) True >>> Some(1).contains(2) False >>> Nothing.contains(3) False
- do(fn: Callable[[gluonts.maybe.T], gluonts.maybe.U]) gluonts.maybe.Maybe[gluonts.maybe.T] [源代码]#
如果
val
不是None
,则将fn
应用于val
,然后返回val
。>>> Some("a").do(print) a Some('a') >>> Nothing.do(print) Nothing
- expect(msg: str) gluonts.maybe.T [源代码]#
确保
val
不是None
,否则使用msg
引发ValueError
。>>> Some(1).expect("My message") 1 >>> Nothing.expect("My message") Traceback (most recent call last): ... ValueError: My message
- filter(pred: Callable[[gluonts.maybe.T], bool]) gluonts.maybe.Maybe[gluonts.maybe.T] [源代码]#
如果
val
是None
或pred(val)
返回False
,则返回None
,否则返回val
。>>> is_even = lambda n: n % 2 == 0 >>> Some(1).filter(is_even) Nothing >>> Some(2).filter(is_even) Some(2) >>> Nothing.filter(is_even) Nothing
- flatten() gluonts.maybe.Maybe[gluonts.maybe.T] [源代码]#
展平嵌套的可选值。
注意:这仅返回原始值,但将类型从
Optional[Optional[T]]
更改为Optional[T]
。
- map(fn: Callable[[gluonts.maybe.T, gluonts.maybe.P], gluonts.maybe.U], *args, **kwargs) gluonts.maybe.Maybe[gluonts.maybe.U] [源代码]#
如果
val
不是None
,则将fn
应用于val
。>>> Some(1).map(lambda x: x + 1) Some(2) >>> Nothing.map(lambda x: x + 1) Nothing
允许传递额外的参数给
fn
。>>> Some(10).map(divmod, 3) Some((3, 1))
- map_or(fn: Callable[[gluonts.maybe.T], gluonts.maybe.U], default: gluonts.maybe.U) gluonts.maybe.U [源代码]#
如果
val
不是None
,则将fn
应用于val
并返回结果。如果是None
,则返回提供的default
。这类似于连续调用
map
和unwrap_or
。>>> Some(["x"]).map_or(len, 0) 1 >>> Nothing.map_or(len, 0) 0
- map_or_else(fn: Callable[[gluonts.maybe.T], gluonts.maybe.U], factory: Callable[[], gluonts.maybe.U]) gluonts.maybe.U [源代码]#
类似于
map_or
,但返回的值是延迟计算的。这类似于连续调用
map
和unwrap_or_else
。>>> Some(1).map_or_else(lambda n: [n], list) [1] >>> Nothing.map_or_else(lambda n: [n], list) []
- or_(default: Optional[gluonts.maybe.T]) gluonts.maybe.Maybe[gluonts.maybe.T] [源代码]#
类似于 Python 中的
a or b
,但只考虑None
的情况。>>> Some(1).or_(2) Some(1) >>> Some(1).or_(None) Some(1) >>> Nothing.or_(2) Some(2)
- or_else(factory: Callable[[], Optional[gluonts.maybe.T]]) gluonts.maybe.Maybe[gluonts.maybe.T] [源代码]#
类似于 unwrap_or_else,但它返回一个可选值。
>>> Some([42]).or_else(list) Some([42]) >>> Nothing.or_else(list) Some([])
- unbox() Optional[gluonts.maybe.T] [源代码]#
将
Maybe[T]
转换为Optional[T]
。>>> Some(1).unbox() 1 >>> Some(None).unbox() is None True
- unwrap() gluonts.maybe.T [源代码]#
断言该值不是
None
。>>> Some(1).unwrap() 1 >>> Nothing.unwrap() Traceback (most recent call last): ... ValueError: Trying to unwrap `None` value.
- unwrap_or(default: gluonts.maybe.T) gluonts.maybe.T [源代码]#
如果
val
不是None
,则获取val
,否则获取default
。>>> Some(1).unwrap_or(2) 1 >>> Nothing.unwrap_or(2) 2
- unwrap_or_else(fn: Callable[[], gluonts.maybe.T]) gluonts.maybe.T [源代码]#
如果
val
不是None
,则获取val
,否则调用factory
获取一个备用值。>>> Some([1, 2, 3]).unwrap_or_else(list) [1, 2, 3] >>> Nothing.unwrap_or_else(list) []
- val: gluonts.maybe.T#
- xor(other: Union[gluonts.maybe.T, None, Maybe[T]]) gluonts.maybe.Maybe[gluonts.maybe.T] [源代码]#
如果其中一个为
None
,则返回另一个。如果两者均不为None
,则返回None
。>>> xor(1, None) 1 >>> xor(None, 2) 2 >>> xor(1, 2) >>> xor(None, None)
- zip(other: Union[gluonts.maybe.U, None, Maybe[T]]) gluonts.maybe.Maybe[Tuple[gluonts.maybe.T, gluonts.maybe.U]] [源代码]#
抽象的 zip 方法。
- zip_with(other: Union[gluonts.maybe.U, None, Maybe[T]], fn: Callable[[gluonts.maybe.T, gluonts.maybe.U], gluonts.maybe.R]) gluonts.maybe.Maybe[gluonts.maybe.R] [源代码]#
如果两个可选值均不为
None
,则将函数应用于这两个值。>>> add = lambda left, right: left + right >>> Some(1).zip_with(2, add) Some(3) >>> Some(1).zip_with(None, add) Nothing >>> Nothing.zip_with(2, add) Nothing
- gluonts.maybe.and_(val: Union[gluonts.maybe.T, None, Maybe[T]], other: Union[gluonts.maybe.U, None, Maybe[T]]) Optional[gluonts.maybe.U] [源代码]#
类似于 Python 中的
a and b
,但只考虑None
的情况。其实现与
unwrap_or
相同,但在类型方面有所不同。>>> and_(1, 2) 2 >>> and_(1, None) >>> and_(None, 2)
- gluonts.maybe.and_then(val: OptionalOrMaybe[T], fn: Callable[Concatenate[T, P], OptionalOrMaybe[U]], *args: P.args, **kwargs: P.kwargs) Optional[U] [源代码]#
如果
val
不是None
,则将fn
应用于val
并返回结果。与
map
不同,fn
总是返回一个Optional
,然后将其展平。>>> 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.box(val: Union[gluonts.maybe.T, None, Maybe[T]]) gluonts.maybe.Maybe[gluonts.maybe.T] [源代码]#
将
Optional[T]
转换为Maybe[T]
。
- gluonts.maybe.contains(val: Union[gluonts.maybe.T, None, Maybe[T]], other: gluonts.maybe.U) bool [源代码]#
检查
val
是否等于other
,如果val
是None
,则始终返回False
。>>> contains(1, 1) True >>> contains(1, 2) False >>> contains(None, 3) False
- gluonts.maybe.do(val: Union[gluonts.maybe.T, None, Maybe[T]], fn: Callable[[gluonts.maybe.T], gluonts.maybe.U]) Optional[gluonts.maybe.T] [源代码]#
如果
val
不是None
,则将fn
应用于val
,然后返回val
。>>> do("a", print) a 'a' >>> do(None, print)
- gluonts.maybe.expect(val: Union[gluonts.maybe.T, None, Maybe[T]], msg: str) gluonts.maybe.T [源代码]#
确保
val
不是None
,否则使用msg
引发ValueError
。>>> expect(1, "My message") 1 >>> expect(None, "My message") Traceback (most recent call last): ... ValueError: My message
- gluonts.maybe.filter(val: Union[gluonts.maybe.T, None, Maybe[T]], pred: Callable[[gluonts.maybe.T], bool]) Optional[gluonts.maybe.T] [source]#
如果
val
是None
或pred(val)
返回False
,则返回None
,否则返回val
。>>> 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]#
展平嵌套的可选值。
注意:这仅返回原始值,但将类型从
Optional[Optional[T]]
更改为Optional[T]
。
- gluonts.maybe.iter(val: Union[gluonts.maybe.T, None, Maybe[T]]) List[gluonts.maybe.T] [source]#
如果
val
不是None
,则将val
包装到一个列表中。允许对可选值使用 for 循环。
- gluonts.maybe.map(val: OptionalOrMaybe[T], fn: Callable[Concatenate[T, P], U], *args: P.args, **kwargs: P.kwargs) Optional[U] [source]#
如果
val
不是None
,则将fn
应用于val
。>>> map(1, lambda x: x + 1) 2 >>> map(None, lambda x: x + 1)
允许传递额外的参数给
fn
。>>> map(10, divmod, 3) (3, 1)
- gluonts.maybe.map_or(val: Union[gluonts.maybe.T, None, Maybe[T]], fn: Callable[[gluonts.maybe.T], gluonts.maybe.U], default: gluonts.maybe.U) gluonts.maybe.U [source]#
如果
val
不是None
,则将fn
应用于val
并返回结果。如果是None
,则返回提供的default
。这类似于连续调用
map
和unwrap_or
。>>> map_or(["x"], len, 0) 1 >>> map_or(None, len, 0) 0
- gluonts.maybe.map_or_else(val: Union[gluonts.maybe.T, None, Maybe[T]], fn: Callable[[gluonts.maybe.T], gluonts.maybe.U], factory: Callable[[], gluonts.maybe.U]) gluonts.maybe.U [source]#
类似于
map_or
,但返回的值是延迟计算的。这类似于连续调用
map
和unwrap_or_else
。>>> map_or_else(1, lambda n: [n], list) [1] >>> map_or_else(None, lambda n: [n], list) []
- gluonts.maybe.or_(val: Union[gluonts.maybe.T, None, Maybe[T]], default: Optional[gluonts.maybe.T]) Optional[gluonts.maybe.T] [source]#
类似于 Python 中的
a or b
,但只考虑None
的情况。>>> or_(1, 2) 1 >>> or_(1, None) 1 >>> or_(None, 2) 2
- gluonts.maybe.or_else(val: Union[gluonts.maybe.T, None, Maybe[T]], factory: Callable[[], Optional[gluonts.maybe.T]]) Optional[gluonts.maybe.T] [source]#
类似于
unwrap_or_else
,不同之处在于它返回一个可选值。>>> or_else([42], list) [42] >>> or_else(None, list) []
- gluonts.maybe.unbox(val: Union[gluonts.maybe.T, None, Maybe[T]]) Optional[gluonts.maybe.T] [source]#
将
Optional[T]
转换为Maybe[T]
。
- gluonts.maybe.unwrap(val: Union[gluonts.maybe.T, None, Maybe[T]]) gluonts.maybe.T [source]#
断言该值不是
None
。>>> unwrap(1) 1 >>> unwrap(None) Traceback (most recent call last): ... ValueError: Trying to unwrap `None` value.
- gluonts.maybe.unwrap_or(val: Union[gluonts.maybe.T, None, Maybe[T]], default: gluonts.maybe.T) gluonts.maybe.T [source]#
如果
val
不是None
,则获取val
,否则获取default
。>>> unwrap_or(1, 2) 1 >>> unwrap_or(None, 2) 2
- gluonts.maybe.unwrap_or_else(val: Union[gluonts.maybe.T, None, Maybe[T]], factory: Callable[[], gluonts.maybe.T]) gluonts.maybe.T [source]#
如果
val
不是None
,则获取val
,否则调用factory
获取一个备用值。>>> unwrap_or_else([1, 2, 3], list) [1, 2, 3] >>> unwrap_or_else(None, list) []
- gluonts.maybe.xor(val: Union[gluonts.maybe.T, None, Maybe[T]], other: Union[gluonts.maybe.T, None, Maybe[T]]) Optional[gluonts.maybe.T] [source]#
如果其中一个为
None
,则返回另一个。如果两者均不为None
,则返回None
。>>> xor(1, None) 1 >>> xor(None, 2) 2 >>> xor(1, 2) >>> xor(None, None)
- gluonts.maybe.zip(val: Union[gluonts.maybe.T, None, Maybe[T]], other: Union[gluonts.maybe.U, None, Maybe[T]]) Optional[Tuple[gluonts.maybe.T, gluonts.maybe.U]] [source]#
如果
val
和other
都不是None
,则返回它们的元组(val, other)
,否则返回None
。
- gluonts.maybe.zip_with(val: Union[gluonts.maybe.T, None, Maybe[T]], other: Union[gluonts.maybe.U, None, Maybe[T]], fn: Callable[[gluonts.maybe.T, gluonts.maybe.U], gluonts.maybe.R]) Optional[gluonts.maybe.R] [source]#
如果两个可选值均不为
None
,则将函数应用于这两个值。>>> add = lambda left, right: left + right >>> zip_with(1, 2, add) 3 >>> zip_with(1, None, add) >>> zip_with(None, 2, add)