Apply pylint's linting rules to bfxapi/websocket/handlers/*.py.

This commit is contained in:
Davide Casale
2023-03-06 17:53:54 +01:00
parent a7f5aa684a
commit 7e627dd239
5 changed files with 62 additions and 58 deletions

View File

@@ -10,7 +10,6 @@ disable=
multiple-imports,
missing-docstring,
too-few-public-methods,
dangerous-default-value,
[FORMAT]

View File

@@ -1,3 +1,5 @@
#pylint: disable=dangerous-default-value
from typing import Type, Generic, TypeVar, Iterable, Optional, Dict, List, Tuple, Any, cast
from .exceptions import LabelerSerializerException

View File

@@ -1,4 +1,4 @@
from .public_channels_handler import PublicChannelsHandler
from .authenticated_channels_handler import AuthenticatedChannelsHandler
NAME = "handlers"
NAME = "handlers"

View File

@@ -1,18 +1,19 @@
from .. import serializers
from .. types import *
from .. serializers import _Notification
from .. exceptions import HandlerNotFound
class AuthenticatedChannelsHandler(object):
class AuthenticatedChannelsHandler:
__abbreviations = {
"os": "order_snapshot", "on": "order_new", "ou": "order_update", "oc": "order_cancel",
"ps": "position_snapshot", "pn": "position_new", "pu": "position_update", "pc": "position_close",
"te": "trade_executed", "tu": "trade_execution_update",
"fos": "funding_offer_snapshot", "fon": "funding_offer_new", "fou": "funding_offer_update", "foc": "funding_offer_cancel",
"fcs": "funding_credit_snapshot", "fcn": "funding_credit_new", "fcu": "funding_credit_update", "fcc": "funding_credit_close",
"fls": "funding_loan_snapshot", "fln": "funding_loan_new", "flu": "funding_loan_update", "flc": "funding_loan_close",
"ws": "wallet_snapshot", "wu": "wallet_update",
"os": "order_snapshot", "on": "order_new", "ou": "order_update",
"oc": "order_cancel", "ps": "position_snapshot", "pn": "position_new",
"pu": "position_update", "pc": "position_close", "te": "trade_executed",
"tu": "trade_execution_update", "fos": "funding_offer_snapshot", "fon": "funding_offer_new",
"fou": "funding_offer_update", "foc": "funding_offer_cancel", "fcs": "funding_credit_snapshot",
"fcn": "funding_credit_new", "fcu": "funding_credit_update", "fcc": "funding_credit_close",
"fls": "funding_loan_snapshot", "fln": "funding_loan_new", "flu": "funding_loan_update",
"flc": "funding_loan_close", "ws": "wallet_snapshot", "wu": "wallet_update",
"bu": "balance_update",
}
@@ -27,43 +28,42 @@ class AuthenticatedChannelsHandler(object):
("bu",): serializers.Balance
}
EVENTS = [
EVENTS = [
"notification",
"on-req-notification", "ou-req-notification", "oc-req-notification",
"oc_multi-notification",
"fon-req-notification", "foc-req-notification",
*list(__abbreviations.values())
*list(__abbreviations.values())
]
def __init__(self, event_emitter, strict = True):
self.event_emitter, self.strict = event_emitter, strict
def __init__(self, event_emitter):
self.event_emitter = event_emitter
def handle(self, type, stream):
if type == "n":
def handle(self, abbrevation, stream):
if abbrevation == "n":
return self.__notification(stream)
for types, serializer in AuthenticatedChannelsHandler.__serializers.items():
if type in types:
event = AuthenticatedChannelsHandler.__abbreviations[type]
for abbrevations, serializer in AuthenticatedChannelsHandler.__serializers.items():
if abbrevation in abbrevations:
event = AuthenticatedChannelsHandler.__abbreviations[abbrevation]
if all(isinstance(substream, list) for substream in stream):
return self.event_emitter.emit(event, [ serializer.parse(*substream) for substream in stream ])
return self.event_emitter.emit(event, serializer.parse(*stream))
if self.strict:
raise HandlerNotFound(f"No handler found for event of type <{type}>.")
raise HandlerNotFound(f"No handler found for event of type <{abbrevation}>.")
def __notification(self, stream):
type, serializer = "notification", serializers._Notification(serializer=None)
event, serializer = "notification", _Notification(serializer=None)
if stream[1] == "on-req" or stream[1] == "ou-req" or stream[1] == "oc-req":
type, serializer = f"{stream[1]}-notification", serializers._Notification(serializer=serializers.Order)
event, serializer = f"{stream[1]}-notification", _Notification(serializer=serializers.Order)
if stream[1] == "oc_multi-req":
type, serializer = f"{stream[1]}-notification", serializers._Notification(serializer=serializers.Order, iterate=True)
event, serializer = f"{stream[1]}-notification", _Notification(serializer=serializers.Order, iterate=True)
if stream[1] == "fon-req" or stream[1] == "foc-req":
type, serializer = f"{stream[1]}-notification", serializers._Notification(serializer=serializers.FundingOffer)
event, serializer = f"{stream[1]}-notification", _Notification(serializer=serializers.FundingOffer)
return self.event_emitter.emit(type, serializer.parse(*stream))
return self.event_emitter.emit(event, serializer.parse(*stream))

View File

@@ -1,20 +1,19 @@
from .. import serializers
#pylint: disable=inconsistent-return-statements
from .. types import *
from .. import serializers
from .. exceptions import HandlerNotFound
class PublicChannelsHandler(object):
class PublicChannelsHandler:
EVENTS = [
"t_ticker_update", "f_ticker_update",
"t_trade_executed", "t_trade_execution_update", "f_trade_executed", "f_trade_execution_update", "t_trades_snapshot", "f_trades_snapshot",
"t_book_snapshot", "f_book_snapshot", "t_raw_book_snapshot", "f_raw_book_snapshot", "t_book_update", "f_book_update", "t_raw_book_update", "f_raw_book_update",
"candles_snapshot", "candles_update",
"derivatives_status_update",
"t_ticker_update", "f_ticker_update", "t_trade_executed", "t_trade_execution_update", "f_trade_executed",
"f_trade_execution_update", "t_trades_snapshot", "f_trades_snapshot", "t_book_snapshot", "f_book_snapshot",
"t_raw_book_snapshot", "f_raw_book_snapshot", "t_book_update", "f_book_update", "t_raw_book_update",
"f_raw_book_update", "candles_snapshot", "candles_update", "derivatives_status_update",
]
def __init__(self, event_emitter, strict = True):
self.event_emitter, self.strict = event_emitter, strict
def __init__(self, event_emitter):
self.event_emitter = event_emitter
self.__handlers = {
"ticker": self.__ticker_channel_handler,
@@ -25,13 +24,14 @@ class PublicChannelsHandler(object):
}
def handle(self, subscription, *stream):
#pylint: disable-next=unnecessary-lambda-assignment
_clear = lambda dictionary, *args: { key: value for key, value in dictionary.items() if key not in args }
#pylint: disable-next=consider-iterating-dictionary
if (channel := subscription["channel"]) and channel in self.__handlers.keys():
return self.__handlers[channel](_clear(subscription, "event", "channel", "chanId"), *stream)
if self.strict:
raise HandlerNotFound(f"No handler found for channel <{subscription['channel']}>.")
raise HandlerNotFound(f"No handler found for channel <{subscription['channel']}>.")
def __ticker_channel_handler(self, subscription, *stream):
if subscription["symbol"].startswith("t"):
@@ -49,17 +49,17 @@ class PublicChannelsHandler(object):
)
def __trades_channel_handler(self, subscription, *stream):
if (type := stream[0]) and type in [ "te", "tu", "fte", "ftu" ]:
if (event := stream[0]) and event in [ "te", "tu", "fte", "ftu" ]:
if subscription["symbol"].startswith("t"):
return self.event_emitter.emit(
{ "te": "t_trade_executed", "tu": "t_trade_execution_update" }[type],
{ "te": "t_trade_executed", "tu": "t_trade_execution_update" }[event],
subscription,
serializers.TradingPairTrade.parse(*stream[1])
)
if subscription["symbol"].startswith("f"):
return self.event_emitter.emit(
{ "fte": "f_trade_executed", "ftu": "f_trade_execution_update" }[type],
{ "fte": "f_trade_executed", "ftu": "f_trade_execution_update" }[event],
subscription,
serializers.FundingCurrencyTrade.parse(*stream[1])
)
@@ -79,36 +79,39 @@ class PublicChannelsHandler(object):
)
def __book_channel_handler(self, subscription, *stream):
type = subscription["symbol"][0]
event = subscription["symbol"][0]
if subscription["prec"] == "R0":
_trading_pair_serializer, _funding_currency_serializer, IS_RAW_BOOK = serializers.TradingPairRawBook, serializers.FundingCurrencyRawBook, True
else: _trading_pair_serializer, _funding_currency_serializer, IS_RAW_BOOK = serializers.TradingPairBook, serializers.FundingCurrencyBook, False
_trading_pair_serializer, _funding_currency_serializer, is_raw_book = \
serializers.TradingPairRawBook, serializers.FundingCurrencyRawBook, True
else: _trading_pair_serializer, _funding_currency_serializer, is_raw_book = \
serializers.TradingPairBook, serializers.FundingCurrencyBook, False
if all(isinstance(substream, list) for substream in stream[0]):
return self.event_emitter.emit(
type + "_" + (IS_RAW_BOOK and "raw_book" or "book") + "_snapshot",
subscription,
[ { "t": _trading_pair_serializer, "f": _funding_currency_serializer }[type].parse(*substream) for substream in stream[0] ]
return self.event_emitter.emit(
event + "_" + (is_raw_book and "raw_book" or "book") + "_snapshot",
subscription,
[ { "t": _trading_pair_serializer, "f": _funding_currency_serializer }[event] \
.parse(*substream) for substream in stream[0] ]
)
return self.event_emitter.emit(
type + "_" + (IS_RAW_BOOK and "raw_book" or "book") + "_update",
subscription,
{ "t": _trading_pair_serializer, "f": _funding_currency_serializer }[type].parse(*stream[0])
event + "_" + (is_raw_book and "raw_book" or "book") + "_update",
subscription,
{ "t": _trading_pair_serializer, "f": _funding_currency_serializer }[event].parse(*stream[0])
)
def __candles_channel_handler(self, subscription, *stream):
if all(isinstance(substream, list) for substream in stream[0]):
return self.event_emitter.emit(
"candles_snapshot",
subscription,
subscription,
[ serializers.Candle.parse(*substream) for substream in stream[0] ]
)
return self.event_emitter.emit(
"candles_update",
subscription,
"candles_update",
subscription,
serializers.Candle.parse(*stream[0])
)
@@ -118,4 +121,4 @@ class PublicChannelsHandler(object):
"derivatives_status_update",
subscription,
serializers.DerivativesStatus.parse(*stream[0])
)
)