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 AwaitableResultWrapper Typesafe and immutable wrapper for trcks.AwaitableResult objects.
Class AwaitableWrapper Typesafe and immutable wrapper for collections.abc.Awaitable objects.
Class ResultWrapper Typesafe and immutable wrapper for trcks.Result objects.
Class Wrapper Typesafe and immutable wrapper for arbitrary objects.
Class _AwaitableWrapper Base class for all asynchronous wrappers in the trcks.oop module.
Class _Wrapper Base class for all wrappers in the trcks.oop module.
Constant _F Undocumented
Constant _S Undocumented
Constant _T Undocumented
Variable _F_default Undocumented
Variable _F_default_co Undocumented
Variable _S_default Undocumented
Variable _S_default_co Undocumented
Variable _T_co Undocumented

Undocumented

Value
TypeVar('_F')

Undocumented

Value
TypeVar('_S')

Undocumented

Value
TypeVar('_T')
_F_default = (source)

Undocumented

_F_default_co = (source)

Undocumented

_S_default = (source)

Undocumented

_S_default_co = (source)

Undocumented

Undocumented