class documentation
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 |
Apply an asynchronous function to the wrapped object. |
Method | map |
Apply an asynchronous function with return type trcks.Result . |
Method | map |
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 |
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[ | The synchronous function to be applied. |
Returns | |
Wrapper[ | A new Wrapper instance with the result of the function application. |
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[ | The asynchronous function to be applied. |
Returns | |
AwaitableWrapper[ | 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[ | The asynchronous function to be applied. |
Returns | |
AwaitableResultWrapper[ | An AwaitableResultWrapper instance with
the result of the function application. |
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[ | The synchronous function to be applied. |
Returns | |
ResultWrapper[ | A ResultWrapper instance with the result of the function application. |