diff --git a/piker/brokers/binance/api.py b/piker/brokers/binance/api.py index 9f5476a0..f9ecc8fd 100644 --- a/piker/brokers/binance/api.py +++ b/piker/brokers/binance/api.py @@ -31,6 +31,8 @@ from pprint import pformat from typing import ( Any, Callable, + Hashable, + Sequence, Type, ) import hmac @@ -549,7 +551,7 @@ class Client: if sym: return pair_table[sym] else: - self._pairs + return self._pairs async def get_assets( self, @@ -596,14 +598,26 @@ class Client: fq_pairs: dict = await self.exch_info() - matches = fuzzy.extractBests( - pattern, - fq_pairs, + # TODO: cache this list like we were in + # `open_symbol_search()`? + keys: list[str] = list(fq_pairs) + + matches: list[tuple[ + Sequence[Hashable], # matching input key + Any, # scores + Any, + ]] = fuzzy.extract( + query=pattern.upper(), # since all keys are uppercase + choices=keys, score_cutoff=50, ) # repack in dict form - return {item[0]['symbol']: item[0] - for item in matches} + matched_pairs: dict[str, Pair] = {} + for item in matches: + pair_key: str = item[0] + matched_pairs[pair_key] = self._pairs[pair_key] + + return matched_pairs async def bars( self, diff --git a/piker/brokers/binance/feed.py b/piker/brokers/binance/feed.py index 713f76c6..1416d6a7 100644 --- a/piker/brokers/binance/feed.py +++ b/piker/brokers/binance/feed.py @@ -42,7 +42,7 @@ from trio_typing import TaskStatus from pendulum import ( from_timestamp, ) -from fuzzywuzzy import process as fuzzy +from rapidfuzz import process as fuzzy import numpy as np import tractor @@ -533,14 +533,15 @@ async def open_symbol_search( pattern: str async for pattern in stream: - matches = fuzzy.extractBests( + # NOTE: pattern fuzzy-matching is done within + # the methd impl. + pairs: dict[str, Pair] = await client.search_symbols( pattern, - client._pairs, - score_cutoff=50, ) - # repack in dict form - await stream.send({ - item[0].bs_fqme: item[0] - for item in matches - }) + # repack in fqme-keyed table + byfqme: dict[start, Pair] = {} + for pair in pairs.values(): + byfqme[pair.bs_fqme] = pair + + await stream.send(byfqme)