module documentation
Object-oriented interface for trcks
.
This module provides wrapper classes for processing values of the following types in a method-chaining style:
Example
This example uses the classes Wrapper
and ResultWrapper
to create and further process a value of type trcks.Result
:
>>> import enum >>> import math >>> from trcks import Result >>> from trcks.oop import Wrapper >>> class GetSquareRootError(enum.Enum): ... NEGATIVE_INPUT = enum.auto() ... >>> def get_square_root(x: float) -> Result[GetSquareRootError, float]: ... return ( ... Wrapper(core=x) ... .map_to_result( ... lambda x: ("success", x) ... if x >= 0 ... else ("failure", GetSquareRootError.NEGATIVE_INPUT) ... ) ... .map_success(math.sqrt) ... .core ... ) ... >>> get_square_root(25.0) ('success', 5.0) >>> get_square_root(-25.0) ('failure', <GetSquareRootError.NEGATIVE_INPUT: 1>)
Variable and type assignments for intermediate values might help to clarify what is going on:
>>> import enum >>> import math >>> from trcks import Result >>> from trcks.oop import ResultWrapper, Wrapper >>> class GetSquareRootError(enum.Enum): ... NEGATIVE_INPUT = enum.auto() ... >>> def get_square_root(x: float) -> Result[GetSquareRootError, float]: ... wrapper: Wrapper[float] = Wrapper(core=x) ... result_wrapper: ResultWrapper[ ... GetSquareRootError, float ... ] = wrapper.map_to_result( ... lambda x: ("success", x) ... if x >= 0 ... else ("failure", GetSquareRootError.NEGATIVE_INPUT) ... ) ... mapped_result_wrapper: ResultWrapper[GetSquareRootError, float] = ( ... result_wrapper.map_success(math.sqrt) ... ) ... result: Result[GetSquareRootError, float] = mapped_result_wrapper.core ... return result ... >>> get_square_root(25.0) ('success', 5.0) >>> get_square_root(-25.0) ('failure', <GetSquareRootError.NEGATIVE_INPUT: 1>)
Class |
|
Typesafe and immutable wrapper for trcks.AwaitableResult objects. |
Class |
|
Typesafe and immutable wrapper for collections.abc.Awaitable objects. |
Class |
|
Typesafe and immutable wrapper for trcks.Result objects. |
Class |
|
Typesafe and immutable wrapper for arbitrary objects. |
Class | _ |
Base class for all asynchronous wrappers in the trcks.oop module. |
Class | _ |
Base class for all wrappers in the trcks.oop module. |
Constant | _F |
Undocumented |
Constant | _S |
Undocumented |
Constant | _T |
Undocumented |
Variable | _ |
Undocumented |
Variable | _ |
Undocumented |
Variable | _ |
Undocumented |
Variable | _ |
Undocumented |
Variable | _ |
Undocumented |