kucoin: port to using `rapidfuzz`

Just like the others but also flip to using a `Client.get_mkt_pairs()`
meth name for consistency across clients.
ib_py311_fixes
Tyler Goodlet 2023-09-22 15:55:19 -04:00
parent 1a859bc1a2
commit c312f90c0c
1 changed files with 25 additions and 10 deletions

View File

@ -65,7 +65,10 @@ from piker._cacheables import (
from piker.log import get_logger from piker.log import get_logger
from piker.data.validate import FeedInit from piker.data.validate import FeedInit
from piker.types import Struct from piker.types import Struct
from piker.data import def_iohlcv_fields from piker.data import (
def_iohlcv_fields,
match_from_pairs,
)
from piker.data._web_bs import ( from piker.data._web_bs import (
open_autorecon_ws, open_autorecon_ws,
NoBsWs, NoBsWs,
@ -377,7 +380,7 @@ class Client:
return pairs, fqmes2mktids return pairs, fqmes2mktids
async def cache_pairs( async def get_mkt_pairs(
self, self,
update: bool = False, update: bool = False,
@ -405,16 +408,28 @@ class Client:
) -> dict[str, KucoinMktPair]: ) -> dict[str, KucoinMktPair]:
''' '''
Use fuzzy search to match against all market names. Use fuzzy search engine to match against pairs, deliver
matching ones.
''' '''
data = await self.cache_pairs() if not len(self._pairs):
await self.get_mkt_pairs()
assert self._pairs, '`Client.get_mkt_pairs()` was never called!?'
matches = fuzzy.extractBests(
pattern, data, score_cutoff=35, limit=limit matches: dict[str, Pair] = match_from_pairs(
pairs=self._pairs,
# query=pattern.upper(),
query=pattern.upper(),
score_cutoff=35,
limit=limit,
) )
# repack in dict form # repack in dict form
return {item[0].name: item[0] for item in matches} return {
pair.name: pair
for pair in matches.values()
}
async def last_trades(self, sym: str) -> list[AccountTrade]: async def last_trades(self, sym: str) -> list[AccountTrade]:
trades = await self._request( trades = await self._request(
@ -557,7 +572,7 @@ async def get_client() -> AsyncGenerator[Client, None]:
client = Client() client = Client()
async with trio.open_nursery() as n: async with trio.open_nursery() as n:
n.start_soon(client.cache_pairs) n.start_soon(client.get_mkt_pairs)
await client.get_currencies() await client.get_currencies()
yield client yield client
@ -569,7 +584,7 @@ async def open_symbol_search(
) -> None: ) -> None:
async with open_cached_client('kucoin') as client: async with open_cached_client('kucoin') as client:
# load all symbols locally for fast search # load all symbols locally for fast search
await client.cache_pairs() await client.get_mkt_pairs()
await ctx.started() await ctx.started()
async with ctx.open_stream() as stream: async with ctx.open_stream() as stream:
@ -617,7 +632,7 @@ async def get_mkt_info(
# split off any fqme broker part # split off any fqme broker part
bs_fqme, _, broker = fqme.partition('.') bs_fqme, _, broker = fqme.partition('.')
pairs: dict[str, KucoinMktPair] = await client.cache_pairs() pairs: dict[str, KucoinMktPair] = await client.get_mkt_pairs()
try: try:
# likely search result key which is already in native mkt symbol form # likely search result key which is already in native mkt symbol form