-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse-data-file.py
54 lines (42 loc) · 1.74 KB
/
parse-data-file.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
import os
import glob
import re
def parse_all_files(resultVar):
for filename in glob.iglob('*.txt'):
with open(filename, "r") as handler:
for line in handler:
if resultVar in line.lower():
digits = re.findall(r'\d', line)
result = ''.join(digits)
strategy, num, arena, seed = os.path.basename(filename).split('_')
yield dict(strategy=strategy, num=num, arena=arena, seed=seed, result=int(result))
def build_dict_sorted(field, resultVar):
parsed_dict = parse_all_files(resultVar)
return sorted(parsed_dict, key=lambda k: k[field])
def printFinalResultForField(field, resultVar):
dictList = build_dict_sorted(field, resultVar)
finalDict = {}
finalDictList = []
count = 0
for d in dictList:
if not finalDict:
finalDict[field] = d[field]
if resultVar in finalDict:
if d[field] == finalDict[field]:
count = count + 1
finalDict[resultVar] = int(finalDict[resultVar]) + int(d[resultVar])
else:
finalDict['average'] = round(float(finalDict[resultVar]) / float(count), 2)
finalDictList.append(finalDict)
count = 1
finalDict = {}
finalDict[field] = d[field]
finalDict[resultVar] = int(d[resultVar])
else:
count = count + 1
finalDict[resultVar] = int(d[resultVar])
finalDict['average'] = round(float(finalDict[resultVar]) / float(count), 2)
finalDictList.append(finalDict)
print(finalDictList)
printFinalResultForField('arena', 'result')
printFinalResultForField('strategy', 'result')