Wishful thinking with conditional mngrs

basic_orders
Tyler Goodlet 2021-01-09 10:53:40 -05:00
parent 5a4f5c35ae
commit 620f5fee6e
1 changed files with 17 additions and 0 deletions

View File

@ -17,7 +17,9 @@
"""
Async utils no one seems to have built into a core lib (yet).
"""
from typing import AsyncContextManager
from collections import OrderedDict
from contextlib import asynccontextmanager
def async_lifo_cache(maxsize=128):
@ -47,3 +49,18 @@ def async_lifo_cache(maxsize=128):
return wrapper
return decorator
@asynccontextmanager
async def _just_none():
# noop -> skip entering context
yield None
@asynccontextmanager
async def maybe_with_if(
predicate: bool,
context: AsyncContextManager,
) -> AsyncContextManager:
async with context if predicate else _just_none() as output:
yield output