2020-11-06 17:23:14 +00:00
|
|
|
# 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/>.
|
|
|
|
|
2018-11-25 19:55:55 +00:00
|
|
|
"""
|
|
|
|
Async utils no one seems to have built into a core lib (yet).
|
|
|
|
"""
|
2021-01-09 15:53:40 +00:00
|
|
|
from typing import AsyncContextManager
|
2018-11-25 19:55:55 +00:00
|
|
|
from collections import OrderedDict
|
2021-01-09 15:53:40 +00:00
|
|
|
from contextlib import asynccontextmanager
|
2018-11-25 19:55:55 +00:00
|
|
|
|
|
|
|
|
2018-11-26 00:23:07 +00:00
|
|
|
def async_lifo_cache(maxsize=128):
|
2018-11-25 19:55:55 +00:00
|
|
|
"""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
|
2021-01-09 15:53:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
@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
|