option package¶
Modules¶
option.option_ module¶
This module contains the Option class.
- option.option_.NONE¶
Represents a None value.
- class option.option_.Option(value: T, is_some: bool, *, _force: bool = False)[source]¶
Bases:
Generic
[T
]Option
represents an optional value. EveryOption
is eitherSome
and contains a value, orNONE
and does not.To create a
Some
value, please useOption.Some()
orSome()
.To create a
NONE
value, please useOption.NONE()
or import the constantNONE
directly.To let
Option
guess the type ofOption
to create, please useOption.maybe()
ormaybe()
.Calling the
__init__
method directly will raise aTypeError
.- Examples:
>>> Option.Some(1) Some(1) >>> Option.NONE() NONE >>> Option.maybe(1) Some(1) >>> Option.maybe(None) NONE
- expect(msg: object) T [source]¶
Unwraps the option. Raises an exception if the value is
NONE
.- Args:
msg: The exception message.
- Returns:
The wrapped value.
- Raises:
ValueError
with message provided bymsg
if the value isNONE
.- Examples:
>>> Some(0).expect('sd') 0 >>> try: ... NONE.expect('Oh No!') ... except ValueError as e: ... print(e) Oh No!
- filter(predicate: Callable[[T], bool]) Option[T] [source]¶
Returns
NONE
if theOption
isNONE
, otherwise filter the contained value bypredicate
.- Args:
predicate: The fitler function.
- Returns:
- Examples:
>>> Some(0).filter(lambda x: x % 2 == 1) NONE >>> Some(1).filter(lambda x: x % 2 == 1) Some(1) >>> NONE.filter(lambda x: True) NONE
- flatmap(callback: Callable[[T], Option[U]]) Option[U] [source]¶
Applies the callback to the contained value if the option is not
NONE
.This is different than
Option.map()
because the result of the callback isn’t wrapped in a newOption
- Args:
callback: The callback to apply to the contained value.
- Returns:
-
otherwise calls callback with the contained value and returns the result.
- Examples:
>>> def square(x): return Some(x * x) >>> def nope(x): return NONE >>> Some(2).flatmap(square).flatmap(square) Some(16) >>> Some(2).flatmap(square).flatmap(nope) NONE >>> Some(2).flatmap(nope).flatmap(square) NONE >>> NONE.flatmap(square).flatmap(square) NONE
- get(key: K, default: Optional[V] = None) Option[V] [source]¶
Gets a mapping value by key in the contained value or returns
default
if the key doesn’t exist.- Args:
key: The mapping key. default: The defauilt value.
- Returns:
Some
variant of the mapping value if the key existsand the value is not None.
Some(default)
ifdefault
is not None.NONE
ifdefault
is None.
- Examples:
>>> Some({'hi': 1}).get('hi') Some(1) >>> Some({}).get('hi', 12) Some(12) >>> NONE.get('hi', 12) Some(12) >>> NONE.get('hi') NONE
- property is_none: bool¶
Returns
True
if the option is aNONE
value.- Examples:
>>> Some(0).is_none False >>> NONE.is_none True
- property is_some: bool¶
Returns
True
if the option is aSome
value.- Examples:
>>> Some(0).is_some True >>> NONE.is_some False
- map(callback: Callable[[T], U]) Option[U] [source]¶
Applies the
callback
with the contained value as its argument or returnsNONE
.
- map_or(callback: Callable[[T], U], default: A) Union[U, A] [source]¶
Applies the
callback
to the contained value or returnsdefault
.- Args:
callback: The callback to apply to the contained value. default: The default value.
- Returns:
The
callback
result if the contained value isSome
, otherwisedefault
.- Notes:
If you wish to use the result of a function call as
default
, it is recommended to usemap_or_else()
instead.- Examples:
>>> Some(0).map_or(lambda x: x + 1, 1000) 1 >>> NONE.map_or(lambda x: x * x, 1) 1
- map_or_else(callback: Callable[[T], U], default: Callable[[], A]) Union[U, A] [source]¶
Applies the
callback
to the contained value or computes a default withdefault
.- Args:
callback: The callback to apply to the contained value. default: The callback fot the default value.
- Returns:
The
callback
result if the contained value isSome
, otherwise the result ofdefault
.- Examples:
>>> Some(0).map_or_else(lambda x: x * x, lambda: 1) 0 >>> NONE.map_or_else(lambda x: x * x, lambda: 1) 1
- classmethod maybe(val: Optional[T]) Option[T] [source]¶
Shortcut method to return
Some
orNONE
based onval
.- Args:
val: Some value.
- Returns:
Some(val)
if theval
is not None, otherwiseNONE
.- Examples:
>>> Option.maybe(0) Some(0) >>> Option.maybe(None) NONE
- unwrap_or(default: U) Union[T, U] [source]¶
Returns the contained value or
default
.- Args:
default: The default value.
- Returns:
The contained value if the
Option
isSome
, otherwisedefault
.- Notes:
If you wish to use a result of a function call as the default, it is recommnded to use
unwrap_or_else()
instead.- Examples:
>>> Some(0).unwrap_or(3) 0 >>> NONE.unwrap_or(0) 0
- unwrap_or_else(callback: Callable[[], U]) Union[T, U] [source]¶
Returns the contained value or computes it from
callback
.- Args:
callback: The the default callback.
- Returns:
The contained value if the
Option
isSome
, otherwisecallback()
.- Examples:
>>> Some(0).unwrap_or_else(lambda: 111) 0 >>> NONE.unwrap_or_else(lambda: 'ha') 'ha'
- option.option_.Some(val: T) Option[T] [source]¶
Shortcut function to
Option.Some()
.
- option.option_.maybe(val: Optional[T]) Option[T] [source]¶
Shortcut function to
Option.maybe()
.
option.result module¶
This module contains the Result type.
- option.result.Err(err: E) Result[Any, E] [source]¶
Shortcut function for
Result.Err()
.
- option.result.Ok(val: T) Result[T, Any] [source]¶
Shortcut function for
Result.Ok()
.
- class option.result.Result(val: Union[T, E], is_ok: bool, *, _force: bool = False)[source]¶
Bases:
Generic
[T
,E
]Result
is a type that either success (Result.Ok()
) or failure (Result.Err()
).To create an Ok value, use
Result.Ok()
orOk()
.To create a Err value, use
Result.Err()
orErr()
.Calling the
Result
constructor directly will raise aTypeError
.- Examples:
>>> Result.Ok(1) Ok(1) >>> Result.Err('Fail!') Err('Fail!')
- classmethod Err(err: E) Result[Any, E] [source]¶
Contains the error value.
- Args:
err: The error value.
- Returns:
The
Result
containing the error value.- Examples:
>>> res = Result.Err('Oh No') >>> res Err('Oh No') >>> res.is_err True
- classmethod Ok(val: T) Result[T, Any] [source]¶
Contains the success value.
- Args:
val: The success value.
- Returns:
The
Result
containing the success value.- Examples:
>>> res = Result.Ok(1) >>> res Ok(1) >>> res.is_ok True
- err() Option[E] [source]¶
Converts from
Result
[T, E] tooption.option_.Option
[E].- Returns:
Option
containing the error value if self isResult.Err()
, otherwiseoption.option_.NONE
.- Examples:
>>> Ok(1).err() NONE >>> Err(1).err() Some(1)
- expect(msg: object) T [source]¶
Returns the success value in the
Result
or raises aValueError
with a provided message.- Args:
msg: The error message.
- Returns:
The success value in the
Result
if it is aResult.Ok()
value.- Raises:
ValueError
withmsg
as the message if theResult
is aResult.Err()
value.- Examples:
>>> Ok(1).expect('no') 1 >>> try: ... Err(1).expect('no') ... except ValueError as e: ... print(e) no
- expect_err(msg: object) E [source]¶
Returns the error value in a
Result
, or raises aValueError
with the provided message.- Args:
msg: The error message.
- Returns:
The error value in the
Result
if it is aResult.Err()
value.- Raises:
ValueError
with the message provided bymsg
if theResult
is aResult.Ok()
value.- Examples:
>>> try: ... Ok(1).expect_err('Oh No') ... except ValueError as e: ... print(e) Oh No >>> Err(1).expect_err('Yes') 1
- flatmap(op: Callable[[T], Result[U, E]]) Result[U, E] [source]¶
Applies a function to the contained
Result.Ok()
value.This is different than
Result.map()
because the function result is not wrapped in a newResult
.- Args:
op: The function to apply to the contained
Result.Ok()
value.- Returns:
- The result of the function if self is an
Result.Ok()
value, otherwise returns self.
- The result of the function if self is an
- Examples:
>>> def sq(x): return Ok(x * x) >>> def err(x): return Err(x) >>> Ok(2).flatmap(sq).flatmap(sq) Ok(16) >>> Ok(2).flatmap(sq).flatmap(err) Err(4) >>> Ok(2).flatmap(err).flatmap(sq) Err(2) >>> Err(3).flatmap(sq).flatmap(sq) Err(3)
- property is_err: bool¶
Returns True if the result is
Result.Err()
.- Examples:
>>> Ok(1).is_err False >>> Err(1).is_err True
- property is_ok: bool¶
Returns True if the result is
Result.Ok()
.- Examples:
>>> Ok(1).is_ok True >>> Err(1).is_ok False
- map(op: Callable[[T], U]) Union[Result[U, E], Result[T, E]] [source]¶
Applies a function to the contained
Result.Ok()
value.- Args:
op: The function to apply to the
Result.Ok()
value.- Returns:
A
Result
with its success value as the function result if self is anResult.Ok()
value, otherwise returns self.- Examples:
>>> Ok(1).map(lambda x: x * 2) Ok(2) >>> Err(1).map(lambda x: x * 2) Err(1)
- map_err(op: Callable[[E], F]) Union[Result[T, F], Result[T, E]] [source]¶
Applies a function to the contained
Result.Err()
value.- Args:
op: The function to apply to the
Result.Err()
value.- Returns:
A
Result
with its error value as the function result if self is aResult.Err()
value, otherwise returns self.- Examples:
>>> Ok(1).map_err(lambda x: x * 2) Ok(1) >>> Err(1).map_err(lambda x: x * 2) Err(2)
- ok() Option[T] [source]¶
Converts from
Result
[T, E] tooption.option_.Option
[T].- Returns:
Option
containing the success value if self isResult.Ok()
, otherwiseoption.option_.NONE
.- Examples:
>>> Ok(1).ok() Some(1) >>> Err(1).ok() NONE
- unwrap() T [source]¶
Returns the success value in the
Result
.- Returns:
The success value in the
Result
.- Raises:
ValueError
with the message provided by the error valueif the
Result
is aResult.Err()
value.
- Examples:
>>> Ok(1).unwrap() 1 >>> try: ... Err(1).unwrap() ... except ValueError as e: ... print(e) 1
- unwrap_err() E [source]¶
Returns the error value in a
Result
.- Returns:
The error value in the
Result
if it is aResult.Err()
value.- Raises:
ValueError
with the message provided by the success valueif the
Result
is aResult.Ok()
value.
- Examples:
>>> try: ... Ok(1).unwrap_err() ... except ValueError as e: ... print(e) 1 >>> Err('Oh No').unwrap_err() 'Oh No'
- unwrap_or(optb: T) T [source]¶
Returns the success value in the
Result
oroptb
.- Args:
optb: The default return value.
- Returns:
The success value in the
Result
if it is aResult.Ok()
value, otherwiseoptb
.- Notes:
If you wish to use a result of a function call as the default, it is recommnded to use
unwrap_or_else()
instead.- Examples:
>>> Ok(1).unwrap_or(2) 1 >>> Err(1).unwrap_or(2) 2
- unwrap_or_else(op: Callable[[E], U]) Union[T, U] [source]¶
Returns the sucess value in the
Result
or computes a default from the error value.- Args:
op: The function to computes default with.
- Returns:
- The success value in the
Result
if it is a
Result.Ok()
value, otherwiseop(E)
.
- The success value in the
- Examples:
>>> Ok(1).unwrap_or_else(lambda e: e * 10) 1 >>> Err(1).unwrap_or_else(lambda e: e * 10) 10