python: get type from str

This commit is contained in:
なるみ 2022-04-15 15:16:36 +08:00
parent 7dcdecc4d7
commit c9792e21ee
6 changed files with 20 additions and 4 deletions

View File

@ -11,6 +11,6 @@ class ChannelType(Enum):
BALANCE = 4
ORDER = 5
@staticmethod
def from_str(s: str) -> ChannelType:
return {t.name.lower(): t for t in ChannelType}[s]
@classmethod
def from_str(cls, s: str) -> ChannelType:
return {t.name.lower(): t for t in cls}[s.lower()]

View File

@ -10,3 +10,7 @@ class DepthType(Enum):
DEPTH_1 = '1'
DEPTH_5 = '5'
DEPTH_20 = '20'
@classmethod
def from_str(cls, s: str) -> DepthType:
return {t.name.lower(): t for t in cls}[s.lower()]

View File

@ -11,3 +11,7 @@ class EventType(Enum):
UPDATE = 4
AUTHENTICATED = 5
ERROR = 99
@classmethod
def from_str(cls, s: str) -> EventType:
return {t.name.lower(): t for t in cls}[s.lower()]

View File

@ -10,3 +10,7 @@ class OrderType(Enum):
STOP_LIMIT = 3
POST_ONLY = 4
IOC_LIMIT = 5
@classmethod
def from_str(cls, s: str) -> OrderType:
return {t.name.lower(): t for t in cls}[s.lower()]

View File

@ -6,3 +6,7 @@ from enum import Enum
class SideType(Enum):
BUY = 0
SELL = 1
@classmethod
def from_str(cls, s: str) -> SideType:
return {t.name.lower(): t for t in cls}[s.lower()]

View File

@ -27,7 +27,7 @@ class Stream(object):
self.event_handlers = []
def subscribe(self, exchange: str, channel: str, symbol: str, depth: str = None, interval: str = None):
subscription = Subscription(exchange=exchange, channel=ChannelType(channel), symbol=symbol)
subscription = Subscription(exchange=exchange, channel=ChannelType.from_str(channel), symbol=symbol)
if depth is not None:
subscription.depth = DepthType(depth)