-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeal_window.py
61 lines (49 loc) · 2.75 KB
/
deal_window.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
import collections
import csv
from datetime import datetime
defaultDate = datetime.today()
def get_time_with_ms(time): # formatting shortcut
return (datetime.fromtimestamp(time).strftime('%H:%M:%S.%f')[:-3])
def parserow(row): # row parser
time = datetime.strptime(row['Time'], '%H:%M:%S.%f').time() ##convert terrible string to a nice timestamp
time = datetime.combine(defaultDate, time) # append a fictious date to avoid negative timestamps
timestamp = time.timestamp() * 1000 # store miliseconds
return (timestamp, row['EXCHANGE']) # return timestamp and exchange name
def step(window, timestamp, interval=1000): # main algorithm
window.append(timestamp) # append timestamp to theend of list
if window: # if not empty
while timestamp - window[0] > interval: # While the list contains the timestamps outside the interval
if window: # if not empty
left = window.popleft() # pop another timestamp until we done
class DealWindow:
def __init__(self):
self.dealLog = {}
self.maxDeals = {}
self.topTimestamp = {}
def add_exchange(self, name):
self.dealLog[name] = collections.deque()
self.maxDeals[name] = 0
self.topTimestamp[name] = 0
def update_deal_counter(self, exchange, timestamp):
if len(self.dealLog[exchange]) > self.maxDeals[
exchange]: # check if we have more deals in the interval than before
self.maxDeals[exchange] = len(self.dealLog[exchange]) # upd counter
self.topTimestamp[exchange] = timestamp # upd timestamp
def analyze(self, reader):
for row in reader:
(timestamp, exchange) = parserow(row) # parse row
if not (exchange in self.dealLog): # If met new exchange name
self.add_exchange(exchange)
step(self.dealLog[exchange], timestamp) # put timestamp in the corresponding list and do the maths
DealWindow.update_deal_counter(self, exchange, timestamp)
def format_answer(self):
out_strings = {}
for exchange in self.dealLog:
endOfWindow = get_time_with_ms(self.topTimestamp[exchange] / 1000) # get back to readable format
beginningOfWindow = get_time_with_ms(
(self.topTimestamp[exchange] - 1000) / 1000 + 0.001) # get the beginning of the current second
out_strings[
exchange] = "Maximum deals during one-second window was registered at exchange {} between {} and {}. {} deals were performed at that second".format(
exchange, beginningOfWindow, endOfWindow, self.maxDeals[exchange])
# TODO: ask, how do they prefer to define the interval? By the median, left or right border.
return out_strings