-
Notifications
You must be signed in to change notification settings - Fork 6
/
message.py
70 lines (51 loc) · 1.76 KB
/
message.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from binascii import hexlify
class TraderId(object):
"""Immutable class for representing the id of a trader."""
def __init__(self, trader_id):
"""
:param trader_id: String representing the trader id
:type trader_id: bytes
:raises ValueError: Thrown when one of the arguments are invalid
"""
super(TraderId, self).__init__()
trader_id = trader_id if isinstance(trader_id, bytes) else bytes(trader_id)
if len(trader_id) != 20:
raise ValueError("Trader ID must be 20 bytes")
self.trader_id = trader_id # type: bytes
def __str__(self):
return "%s" % self.trader_id
def __bytes__(self): # type: () -> bytes
return self.trader_id
def as_hex(self):
return hexlify(bytes(self)).decode('utf-8')
def __eq__(self, other):
return self.trader_id == other.trader_id
def __ne__(self, other):
return not self.__eq__(other)
def __hash__(self):
return hash(self.trader_id)
class Message(object):
"""Abstract class for representing a message."""
def __init__(self, trader_id, timestamp):
"""
Don't use this class directly, use on of its implementations
:param trader_id: The trader id of the message sender
:param timestamp: A timestamp when the message was created
:type trader_id: TraderId
:type timestamp: Timestamp
"""
super(Message, self).__init__()
self._trader_id = trader_id
self._timestamp = timestamp
@property
def trader_id(self):
"""
:rtype: TraderId
"""
return self._trader_id
@property
def timestamp(self):
"""
:rtype: Timestamp
"""
return self._timestamp