-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspin-abnormal.py
94 lines (81 loc) · 2.92 KB
/
spin-abnormal.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from datetime import datetime, timedelta
import re
MAXWINDOW = timedelta(minutes=10)
def timeOf(stamp):
times = tuple([int(n) for n in re.findall("[0-9]*", stamp) if n])
return datetime(*times)
def hourOf(t):
n = int(t)
if n == 0: return "midnight"
elif n < 12: return "%dam" % n
elif n == 12: return "noon"
return "%dpm" % (n-12)
def parse(filename):
curShow = None
shows = []
with open(filename) as handle:
for line in handle:
if len(line) != 9:
if curShow:
curShow.finish()
shows.append(curShow)
curShow = Show(line)
else:
curShow.addTime(line)
return shows
def findCollisions(shows):
automation = [s for s in shows if s.isAutomation()]
humansAll = [s for s in shows if not s.isAutomation()]
for auto in automation:
humans = [h for h in humansAll if h.date == auto.date and h.hour == auto.hour]
for human in humans:
collisions = sum([human.collides(t) for t in auto.times])
if collisions:
print human, collisions, "times"
class Show:
def __init__(self, header):
tokens = header.split(',')
self.date = tokens[0]
self.hour = "%s-%s" % (hourOf(tokens[1][0:2]), hourOf(tokens[2][0:2]))
self.show = tokens[3]
self.dj = tokens[-1].strip()
self.times = []
self.windows = []
self.offset = timedelta()
def addTime(self, stamp):
t = timeOf("%s-%s" % (self.date, stamp.strip()))
if self.times and t < self.times[-1]:
if t + timedelta(hours=2) > self.times[-1]:
# Out of order DJ logging
self.times.append(t+self.offset)
self.times.sort()
else:
# Account for overlapping midnight
self.offset = timedelta(days=1)
self.times.append(t+self.offset)
else:
self.times.append(t+self.offset)
def finish(self):
if not self.isAutomation():
for i in range(len(self.times)-1):
delta = self.times[i+1] - self.times[i]
if delta < MAXWINDOW:
self.windows.append(i)
def collides(self, time):
return any([self.times[i] < time < self.times[i+1] for i in self.windows])
def isAutomation(self):
return self.dj == "Rick Deckard"
def __repr__(self):
if self.isAutomation():
return "%s %s: Automation with %d songs" % (self.date, self.hour, len(self.times))
return "%s %s: %s on %s," % (self.date, self.hour, self.dj, self.show)
if __name__ == "__main__":
import sys
if len(sys.argv) > 1:
for filename in sys.argv[1:]:
findCollisions(parse(filename))
else:
try:
findCollisions(parse("spin-data.csv"))
except IOError:
print "Please specify file."