-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.py
71 lines (47 loc) · 1.61 KB
/
item.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
71
from datetime import datetime
from datetime import timedelta
from datetime import timezone
from functools import partial
now_utc = partial(datetime.now, timezone.utc)
class Item:
def __init__(self, name, price, deadline, winner=None):
self.name = str(name)
self.price = round(float(price), 2)
try:
# deadline from json serialization
self.deadline = datetime.fromisoformat(deadline)
except:
# deadlinen from user input
self.deadline = now_utc() + timedelta(minutes=float(deadline))
self.winner = winner
def bid(self, bid, bidder):
if self.deadline < now_utc():
return False
new_price = round(float(bid), 2)
if self.price < new_price:
self.price = new_price
self.winner = bidder
return True
else:
return False
def is_on_offer(self):
return now_utc() < self.deadline
def deadline_as_HMS(self):
return self.deadline.astimezone().strftime("%H:%M:%S")
def timeout(self):
timeout = self.deadline - now_utc()
return max(0, timeout.total_seconds())
############################################################################
# For WAMP messages
def wamp_pack(self):
item_repr = dict(
name=self.name,
price=self.price,
deadline=self.deadline.isoformat(),
)
if self.winner:
item_repr["winner"] = self.winner
return item_repr
@classmethod
def wamp_unpack(cls, details):
return cls(**details)