class documentation

class Wrapper(_Wrapper[_T_co]): (source)

Constructor: Wrapper(core)

View In Hierarchy

Typesafe and immutable wrapper for arbitrary objects.

The wrapped object can be accessed via the attribute Wrapper.core. The Wrapper.map* methods allow method chaining.

Example

The string "Hello" is wrapped and manipulated in the following example. Finally, the result is unwrapped:

>>> wrapper = (
...     Wrapper(core="Hello").map(len).map(lambda n: f"Length: {n}")
... )
>>> wrapper
Wrapper(core='Length: 5')
>>> wrapper.core
'Length: 5'
Static Method construct Alias for the default constructor.
Method map Apply a synchronous function to the wrapped object.
Method map_to_awaitable Apply an asynchronous function to the wrapped object.
Method map_to_awaitable_result Apply an asynchronous function with return type trcks.Result.
Method map_to_result Apply a sync. function with ret. type trcks.Result to the wrapped object.

Inherited from _Wrapper:

Method __init__ Construct wrapper.
Method __repr__ Return a string representation of the wrapper.
Property core The wrapped object.
Instance Variable _core Undocumented
def construct(value: _T) -> Wrapper[_T]: (source)

Alias for the default constructor.

Example

>>> Wrapper.construct(5)
Wrapper(core=5)
Parameters
value:_TThe object to be wrapped.
Returns
Wrapper[_T]A new Wrapper instance with the wrapped object.
def map(self, f: Callable[[_T_co], _T]) -> Wrapper[_T]: (source)

Apply a synchronous function to the wrapped object.

Example

>>> Wrapper.construct(5).map(lambda n: f"The number is {n}.")
Wrapper(core='The number is 5.')
Parameters
f:Callable[[_T_co], _T]The synchronous function to be applied.
Returns
Wrapper[_T]A new Wrapper instance with the result of the function application.
def map_to_awaitable(self, f: Callable[[_T_co], Awaitable[_T]]) -> AwaitableWrapper[_T]: (source)

Apply an asynchronous function to the wrapped object.

Example

>>> import asyncio
>>> from trcks.oop import Wrapper
>>> async def stringify_slowly(o: object) -> str:
...     await asyncio.sleep(0.001)
...     return str(o)
...
>>> awaitable_wrapper = Wrapper.construct(3.14).map_to_awaitable(
...     stringify_slowly
... )
>>> awaitable_wrapper
AwaitableWrapper(core=<coroutine object stringify_slowly at 0x...>)
>>> asyncio.run(awaitable_wrapper.core_as_coroutine)
'3.14'
Parameters
f:Callable[[_T_co], Awaitable[_T]]The asynchronous function to be applied.
Returns
AwaitableWrapper[_T]An AwaitableWrapper instance with the result of the function application.
def map_to_awaitable_result(self, f: Callable[[_T_co], AwaitableResult[_F, _S]]) -> AwaitableResultWrapper[_F, _S]: (source)

Apply an asynchronous function with return type trcks.Result.

Example

>>> import asyncio
>>> from trcks import Result
>>> from trcks.oop import Wrapper
>>> async def slowly_assert_non_negative(
...     x: float,
... ) -> Result[str, float]:
...     await asyncio.sleep(0.001)
...     if x < 0:
...         return "failure", "negative value"
...     return "success", x
...
>>> awaitable_result_wrapper = (
...     Wrapper
...     .construct(42.0)
...     .map_to_awaitable_result(slowly_assert_non_negative)
... )
>>> awaitable_result_wrapper
AwaitableResultWrapper(core=<coroutine object ...>)
>>> asyncio.run(awaitable_result_wrapper.core_as_coroutine)
('success', 42.0)
Parameters
f:Callable[[_T_co], AwaitableResult[_F, _S]]The asynchronous function to be applied.
Returns
AwaitableResultWrapper[_F, _S]An AwaitableResultWrapper instance with the result of the function application.
def map_to_result(self, f: Callable[[_T_co], Result[_F, _S]]) -> ResultWrapper[_F, _S]: (source)

Apply a sync. function with ret. type trcks.Result to the wrapped object.

Example

>>> Wrapper.construct(-1).map_to_result(
...     lambda x: ("success", x)
...     if x >= 0
...     else ("failure", "negative value")
... )
ResultWrapper(core=('failure', 'negative value'))
Parameters
f:Callable[[_T_co], Result[_F, _S]]The synchronous function to be applied.
Returns
ResultWrapper[_F, _S]A ResultWrapper instance with the result of the function application.