From a97a0ced8cbfd8302e757aefb3d037c48059e346 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Thu, 21 Sep 2023 19:49:10 -0400 Subject: [PATCH] kraken: switch to `rapidfuzz` API --- piker/brokers/kraken/api.py | 19 +++++++++++++------ piker/brokers/kraken/symbols.py | 13 ++----------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/piker/brokers/kraken/api.py b/piker/brokers/kraken/api.py index 8e4cce40..c596e073 100644 --- a/piker/brokers/kraken/api.py +++ b/piker/brokers/kraken/api.py @@ -38,7 +38,10 @@ import base64 import trio from piker import config -from piker.data import def_iohlcv_fields +from piker.data import ( + def_iohlcv_fields, + match_from_pairs, +) from piker.accounting._mktinfo import ( Asset, digits_to_dec, @@ -548,13 +551,17 @@ class Client: await self.get_mkt_pairs() assert self._pairs, '`Client.get_mkt_pairs()` was never called!?' - matches = fuzzy.extractBests( - pattern, - self._pairs, + matches: dict[str, Pair] = match_from_pairs( + pairs=self._pairs, + query=pattern.upper(), score_cutoff=50, ) - # repack in dict form - return {item[0].altname: item[0] for item in matches} + + # repack in .altname-keyed output table + return { + pair.altname: pair + for pair in matches.values() + } async def bars( self, diff --git a/piker/brokers/kraken/symbols.py b/piker/brokers/kraken/symbols.py index 64e79f80..80176b4d 100644 --- a/piker/brokers/kraken/symbols.py +++ b/piker/brokers/kraken/symbols.py @@ -136,19 +136,10 @@ async def open_symbol_search(ctx: tractor.Context) -> None: await ctx.started(cache) async with ctx.open_stream() as stream: - async for pattern in stream: - - matches = fuzzy.extractBests( - pattern, - client._pairs, - score_cutoff=50, + await stream.send( + await client.search_symbols(pattern) ) - # repack in dict form - await stream.send({ - pair[0].altname: pair[0] - for pair in matches - }) @async_lifo_cache()