-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprocesscumulativeballsstrikesstats.py
executable file
·84 lines (81 loc) · 3.47 KB
/
processcumulativeballsstrikesstats.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
#!/usr/bin/python3
import sys, re, os, os.path, functools
lineRe = re.compile(r'^\((\d+), (\d+), (\d+), \((\d+), (\d+), (\d+)\), (-?\d+), \((\d+), (\d+)\)\): \((\d+), (\d+)\)')
fileNameRe = re.compile(r'^statswithballsstrikes\.(\d+)$')
def main(directory, isQuiet):
fileNames = [x for x in os.listdir(directory) if fileNameRe.match(x)]
fileNames = sorted(fileNames)
if not isQuiet:
print(fileNames)
existingLineMap = {}
startYear = int(fileNameRe.match(fileNames[0]).group(1))
endYear = int(fileNameRe.match(fileNames[-1]).group(1))
for year in range(startYear, endYear+1):
fileName = 'statswithballsstrikes.' + str(year)
f = None
try:
f = open(os.path.join(directory, fileName), 'r')
except:
pass
linesToWrite = []
newExistingLineMap = {}
if f != None:
for line in f.readlines():
lineMatch = lineRe.match(line)
if lineMatch:
if (lineMatch.group(2) == '1'):
keyString = '"H",'
else:
keyString = '"V",'
# Starting at one to make compliant with other file
baseSum = 1
if (lineMatch.group(4) == '1'):
baseSum = baseSum + 1
if (lineMatch.group(5) == '1'):
baseSum = baseSum + 2
if (lineMatch.group(6) == '1'):
baseSum = baseSum + 4
keyString = keyString + "%s,%s,%s,%s,%s,%s" % (lineMatch.group(1), lineMatch.group(3), baseSum, lineMatch.group(7), lineMatch.group(8), lineMatch.group(9))
total = int(lineMatch.group(11))
wins = int(lineMatch.group(10))
if keyString in existingLineMap:
total += existingLineMap[keyString][1]
wins += existingLineMap[keyString][0]
newExistingLineMap[keyString] = (wins, total)
if keyString in existingLineMap:
del existingLineMap[keyString]
stringToPrint = keyString + ",%s,%s" % (total, wins)
linesToWrite.append(stringToPrint)
else:
print(("ERROR - couldn't parse line %s" %line))
f.close()
if not isQuiet:
print((len(existingLineMap)))
for existingKey in existingLineMap:
linesToWrite.append(existingKey + ",%s,%s" % (existingLineMap[existingKey][1], existingLineMap[existingKey][0]))
newExistingLineMap[existingKey] = existingLineMap[existingKey]
existingLineMap = newExistingLineMap
linesToWrite.sort(key=functools.cmp_to_key(cmpWithCommaFirst))
with open(os.path.join(directory, 'statswithballsstrikescumulative.' + str(year)), 'w') as outFile:
for line in linesToWrite:
outFile.write(line + '\n')
def cmpWithCommaFirst(x, y):
if x[:3] < y[:3]:
return -1
if x[:3] > y[:3]:
return 1
xIsComma = (x[5] == ',')
yIsComma = (y[5] == ',')
if (x < y and yIsComma and not xIsComma):
return 1
elif (x > y and xIsComma and not yIsComma):
return -1
else:
if x < y:
return -1
if x > y:
return 1
return 0
if (__name__ == '__main__'):
isQuiet = len(sys.argv) > 2 and sys.argv[2] == '-q'
main(sys.argv[1], isQuiet)