class Pair: def __init__( self, ttl: int, exchange_name: str, currency: str, market: str, gauge: Gauge, ) -> None: self.cache = Cache(default_ttl=ttl) self.gauge = gauge self.exchange = exchanges.integrations[exchange_name]() self.currency = currency self.market = market log.debug(f"new pair {self}") def __repr__(self) -> str: return ( f"Pair({self.cache.default_ttl}, {self.exchange.name}, " f"{self.currency}, {self.market})" ) def get(self) -> None: try: candle = self.cache.get("candle") except KeyError: candle = self.exchange.get( currency=self.currency, market=self.market ) self.cache.set("candle", candle) for olhcv in helpers.EMPTY_CANDLE.keys(): self.gauge.labels( exchange=self.exchange.name, currency=self.currency, market=self.market, olhcv=olhcv, ).set(candle.__dict__[olhcv]) log.info(f"fetched {candle} for {self}")
def test_get_with_expiration_expired(): cache = Cache() item = Item(value="test", expiration=datetime.now()) cache._items["one"] = item assert not cache.get("one")
def test_get_with_expiration_not_expired(): cache = Cache() item = Item(value="test", expiration=datetime.now() + timedelta(seconds=60)) cache._items["one"] = item assert cache.get("one") == "test"
def test_get_with_no_expiration(): cache = Cache() item = Item(value="test", expiration=None) cache._items["one"] = item assert cache.get("one") == "test"