From 3754f5cca70092ff537a76dfe3f180dd8de1775b Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sat, 17 Apr 2021 15:51:12 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20AsyncLambda?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- royalnet/engineer/wrench.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/royalnet/engineer/wrench.py b/royalnet/engineer/wrench.py index a28728cb..28d7dd95 100644 --- a/royalnet/engineer/wrench.py +++ b/royalnet/engineer/wrench.py @@ -304,6 +304,21 @@ class Check(CheckBase): return self.error +class AsyncLambda(Wrench): + """ + Apply an asyncronous function over the received objects. + """ + + def __init__(self, func: t.Callable[[t.Any], t.Awaitable[t.Any]]): + self.func: t.Callable[[t.Any], t.Awaitable[t.Any]] = func + """ + The function to apply. + """ + + async def filter(self, obj: t.Any) -> t.Any: + return await self.func(obj) + + __all__ = ( "Check", "CheckBase", @@ -318,4 +333,5 @@ __all__ = ( "Type", "Wrench", "WrenchException", + "AsyncLambda", )