67 lines
1.9 KiB
Python
67 lines
1.9 KiB
Python
# piker: trading gear for hackers
|
|
# Copyright (C) 2018-present Tyler Goodlet (in stewardship of piker0)
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
"""
|
|
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):
|
|
"""Async ``cache`` with a LIFO policy.
|
|
|
|
Implemented my own since no one else seems to have
|
|
a standard. I'll wait for the smarter people to come
|
|
up with one, but until then...
|
|
"""
|
|
cache = OrderedDict()
|
|
|
|
def decorator(fn):
|
|
|
|
async def wrapper(*args):
|
|
key = args
|
|
try:
|
|
return cache[key]
|
|
except KeyError:
|
|
if len(cache) >= maxsize:
|
|
# discard last added new entry
|
|
cache.popitem()
|
|
|
|
# do it
|
|
cache[key] = await fn(*args)
|
|
return cache[key]
|
|
|
|
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
|