-
Notifications
You must be signed in to change notification settings - Fork 309
Closed
Description
Original code
@dataclass
class CryptoTrade:
"CryptoTrade contains trade data for a crypto pair."
event_type: Optional[Union[str, EventType]] = None
symbol: Optional[str] = None
exchange: Optional[int] = None
id: Optional[str] = None
price: Optional[float] = None
size: Optional[float] = None
conditions: Optional[List[int]] = None
timestamp: Optional[int] = None
received_timestamp: Optional[int] = None
@staticmethod
def from_dict(d):
return CryptoTrade(
d.get("ev", None),
d.get("sym", None),
d.get("x", None),
d.get("i", None),
d.get("p", None),
d.get("s", None),
d.get("c", None),
d.get("t", None),
d.get("r", None),
)
Produces the following output
<class 'list'> [CryptoTrade(event_type='XT', symbol=None, exchange=1, id='268292583', price=2668.09, size=0.001, conditions=[1], timestamp=1651897896019, received_timestamp=1651897896029)]
<class 'list'> [CryptoTrade(event_type='XT', symbol=None, exchange=1, id='268292584', price=2668.09, size=0.00346924, conditions=[1], timestamp=1651897896019, received_timestamp=1651897896029), CryptoTrade(event_type='XT', symbol=None, exchange=1, id='268292585', price=2668.1, size=0.02227499, conditions=[1], timestamp=1651897896019, received_timestamp=1651897029), CryptoTrade(event_type='XT', symbol=None, exchange=1, id='268292587', price=2668.11, size=0.02726552, conditions=[1], timestamp=1651897896019, received_timestamp=1651897896029), CryptoTrade(event_type='XT', symbol=None, exchange=1, id='268292588', price=2668.11, size=0.04693849, conditions=[1], timestamp=1651897896019, received_timestamp=1651897896029),
CryptoTrade(event_type='XT', symbol=None, exchange=1, id='268292589', price=2668.43, size=0.798, conditions=[1], timestamp=1651897896019, received_timestamp=1651897896029)]
<class 'list'> [CryptoTrade(event_type='XT', symbol=None, exchange=1, id='268292590', price=2668.5, size=0.001, conditions=[1], timestamp=1651897897236, received_timestamp=1651897897245)]
<class 'list'> [CryptoTrade(event_type='XT', symbol=None, exchange=1, id='268292591', price=2668.5, size=0.001, conditions=[1], timestamp=1651897897236, received_timestamp=1651897897245), CryptoTrade(event_type='XT', symbol=None, exchange=1, id='268292592', price=2668.5, size=0.00173462, conditions=[1], timestamp=1651897897236, received_timestamp=1651897897245), CryptoTrade(event_type='XT', symbol=None, exchange=1, id='268292593', price=2668.63, size=0.02728372, conditions=[1], timestamp=1651897897236, received_timestamp=1651897897245),
CryptoTrade(event_type='XT', symbol=None, exchange=1, id='268292594', price=2668.66, size=0.22281884, conditions=[1], timestamp=1651897897236, received_timestamp=1651897897245)]
<class 'list'> [CryptoTrade(event_type='XT', symbol=None, exchange=1, id='268292595', price=2668.54, size=0.001, conditions=[1], timestamp=1651897898157, received_timestamp=1651897898165)]
<class 'list'> [CryptoTrade(event_type='XT', symbol=None, exchange=1, id='268292596', price=2668.54, size=0.001, conditions=[1], timestamp=1651897898157, received_timestamp=1651897898165), CryptoTrade(event_type='XT', symbol=None, exchange=1, id='268292597', price=2668.54, size=0.00086731, conditions=[1], timestamp=1651897898157, received_timestamp=1651897898165), CryptoTrade(event_type='XT', symbol=None, exchange=1, id='268292598', price=2668.54, size=0.00434975, conditions=[1], timestamp=1651897898157, received_timestamp=1651897898165)]
<class 'list'> [CryptoTrade(event_type='XT', symbol=None, exchange=1, id='268292599', price=2668.53, size=0.001, conditions=[2], timestamp=1651897898476, received_timestamp=1651897898484)]
<class 'list'> [CryptoTrade(event_type='XT', symbol=None, exchange=1, id='268292600', price=2668.53, size=0.001, conditions=[2], timestamp=1651897898476, received_timestamp=1651897898485), CryptoTrade(event_type='XT', symbol=None, exchange=1, id='268292601', price=2668.53, size=0.001, conditions=[2], timestamp=1651897898476, received_timestamp=1651897898485),
CryptoTrade(event_type='XT', symbol=None, exchange=1, id='268292602', price=2668.53, size=0.001, conditions=[2], timestamp=1651897898476, received_timestamp=1651897898485)]
<class 'list'> [CryptoTrade(event_type='XT', symbol=None, exchange=2, id='1085319591', price=35906, size=0.00011582, conditions=[1], timestamp=1651897898715, received_timestamp=1651897898770)]
<class 'list'> [CryptoTrade(event_type='XT', symbol=None, exchange=2, id='1085319591', price=35906, size=0.00011582, conditions=[1], timestamp=1651897898715, received_timestamp=1651897898831)]
<class 'list'> [CryptoTrade(event_type='XT', symbol=None, exchange=1, id='327314603', price=35865.06, size=0.00277459, conditions=[1], timestamp=1651897898938, received_timestamp=1651897898949)]
<class 'list'> [CryptoTrade(event_type='XT', symbol=None, exchange=23, id='9a80e301-cb88-4bbe-b7cf-3e413d7f3714', price=2665.99, size=0.10262799, conditions=[2], timestamp=1651897898915, received_timestamp=1651897898962)]
<class 'list'> [CryptoTrade(event_type='XT', symbol=None, exchange=1, id='268292603', price=2668.51, size=0.19709093, conditions=[1], timestamp=1651897899222, received_timestamp=1651897899230)]
<class 'list'> [CryptoTrade(event_type='XT', symbol=None, exchange=23, id='943715f5-6603-4e55-b541-be84cc8e1f19', price=2666.42, size=0.04301937, conditions=[2], timestamp=1651897899482, received_timestamp=1651897899529)]
should change d.get("sym", None)
to d.get("pair", None)
@dataclass
class CryptoTrade:
"CryptoTrade contains trade data for a crypto pair."
event_type: Optional[Union[str, EventType]] = None
symbol: Optional[str] = None
exchange: Optional[int] = None
id: Optional[str] = None
price: Optional[float] = None
size: Optional[float] = None
conditions: Optional[List[int]] = None
timestamp: Optional[int] = None
received_timestamp: Optional[int] = None
@staticmethod
def from_dict(d):
return CryptoTrade(
d.get("ev", None),
d.get("pair", None),
d.get("x", None),
d.get("i", None),
d.get("p", None),
d.get("s", None),
d.get("c", None),
d.get("t", None),
d.get("r", None),
)
Now output is okay
<class 'list'> [CryptoTrade(event_type='XT', symbol='BTC-USD', exchange=1, id='327315546', price=35803.34, size=0.00058953, conditions=[2], timestamp=1651898146638, received_timestamp=1651898146647)]
<class 'list'> [CryptoTrade(event_type='XT', symbol='BTC-USD', exchange=1, id='327315547', price=35804.81, size=0.001, conditions=[1], timestamp=1651898146642, received_timestamp=1651898146652)]
<class 'list'> [CryptoTrade(event_type='XT', symbol='BTC-USD', exchange=1, id='327315548', price=35804.82, size=0.00169632, conditions=[1], timestamp=1651898146642, received_timestamp=1651898146652)]
<class 'list'> [CryptoTrade(event_type='XT', symbol='BTC-USD', exchange=1, id='327315549', price=35804.81, size=0.01368515, conditions=[1], timestamp=1651898147259, received_timestamp=1651898147268)]
<class 'list'> [CryptoTrade(event_type='XT', symbol='BTC-USD', exchange=1, id='327315550', price=35803.81, size=2.358e-05, conditions=[2], timestamp=1651898147312, received_timestamp=1651898147320)]
<class 'list'> [CryptoTrade(event_type='XT', symbol='ETH-USD', exchange=1, id='268293802', price=2663.29, size=0.001, conditions=[1], timestamp=1651898148429, received_timestamp=1651898148437)]
<class 'list'> [CryptoTrade(event_type='XT', symbol='ETH-USD', exchange=1, id='268293803', price=2663.35, size=0.02727959, conditions=[2], timestamp=1651898148472, received_timestamp=1651898148480)]
<class 'list'> [CryptoTrade(event_type='XT', symbol='BTC-USD', exchange=1, id='327315551', price=35805.95, size=0.00065328, conditions=[1], timestamp=1651898148604, received_timestamp=1651898148612)]
<class 'list'> [CryptoTrade(event_type='XT', symbol='BTC-USD', exchange=1, id='327315552', price=35805.95, size=0.00025053, conditions=[1], timestamp=1651898148808, received_timestamp=1651898148818)]
Metadata
Metadata
Assignees
Labels
No labels