-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprocesscumulativeballsstrikesrunsperinningstats.py
executable file
·47 lines (45 loc) · 2.03 KB
/
processcumulativeballsstrikesrunsperinningstats.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
#!/usr/bin/python3
import sys, re, os, os.path, functools
lineRe = re.compile(r'^(\(\d+, \(\d+, \d+, \d+\), \(\d+, \d+\)\)): \[(.*?)\]\s*')
fileNameRe = re.compile(r'^runsperinningballsstrikesstats\.(\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 = 'runsperinningballsstrikesstats.' + str(year)
f = None
try:
f = open(os.path.join(directory, fileName), 'r')
except:
pass
linesToWrite = []
if f != None:
for line in f.readlines():
lineMatch = lineRe.match(line)
if lineMatch:
key = lineMatch.group(1)
runDistribution = [int(x.strip()) for x in lineMatch.group(2).split(',')]
if key not in existingLineMap:
existingLineMap[key] = []
existingRuns = existingLineMap[key]
while len(existingRuns) < len(runDistribution):
existingRuns.append(0)
for i in range(len(runDistribution)):
existingRuns[i] += runDistribution[i]
existingLineMap[key] = existingRuns
linesToWrite.append(key + ": [" + ', '.join([str(x) for x in existingRuns]) + "]")
else:
print("ERROR - couldn't parse line %s" % line)
f.close()
linesToWrite.sort()
with open(os.path.join(directory, 'runsperinningballsstrikesstatscumulative.' + str(year)), 'w') as outFile:
for line in linesToWrite:
outFile.write(line + '\n')
if (__name__ == '__main__'):
isQuiet = len(sys.argv) > 2 and sys.argv[2] == '-q'
main(sys.argv[1], isQuiet)