-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgs2csv.py
230 lines (188 loc) · 5.06 KB
/
gs2csv.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/python
# NAME, VERSION, DOCUMENTATION
#
# gs2csv.py
# Version 1.0
# gs2csv.doc
#
# DESCRIPTION
#
# Constructs a .csv file from a .gs file that contains a table
# summarizing the number of times each game state led to each other game
# state.
# USAGE
#
# ./gs2csv [-ert] input.gs output.csv
#
# OPTIONS
#
# If no options are passed, all options are assumed.
#
# -e Produces tables for events
# -r Produces a table for runs
# -t Produces a table for transitions
# does output redirection work with argparse?
# use csv.writer or redirect prints? or write to file explicitly?
import argparse
import csv
PROGRAM = "gs2csv"
VERSION = "1.0"
parser = argparse.ArgumentParser(description="Produce a .csv file summarizing game state transitions")
# Positional Arguments
parser.add_argument("inFile", help="Specify input file")
parser.add_argument("outFile", help="Specify output file")
# Optional Arguments
parser.add_argument("-e", "--events", help="Produces a table for events", action="store_true")
parser.add_argument("-r", "--runs", help="Produces a table for runs", action="store_true")
parser.add_argument("-t", "--transitions", help="Produces a table for transitions", action="store_true")
args = parser.parse_args()
# if no options specified, print all tables
if args.events or args.runs or args.transitions:
pass
else:
args.events = True
args.runs = True
args.transitions = True
s1 = '' # the first state of a transition
s2 = '' # the second state
evt = '' # the event that caused the transition
sp = '' # whether a run scored
states = [ '0000', '0003', '0020', '0023', '0100', '0103', '0120', '0123',
'1000', '1003', '1020', '1023', '1100', '1103', '1120', '1123',
'2000', '2003', '2020', '2023', '2100', '2103', '2120', '2123',
'3000', '----'] # all possible game states
# used for dict initialization and table column headers
results = {}
inputFile = open(args.inFile, 'rb')
inputReader = csv.reader(inputFile)
for row in inputReader:
s1, s2 = row[0], row[1]
try:
results[s1]
except KeyError:
results[s1] = {}
try:
results[s1][s2]
except KeyError:
results[s1][s2] = {}
try:
results[s1][s2]['count'] += 1
except KeyError:
results[s1][s2]['count'] = 1
if args.events:
evt = row[2]
try:
results[s1][s2][evt] += 1
except KeyError:
results[s1][s2][evt] = 1
if args.runs:
# row[3] is boolean scoring play
if row[3] == '1':
try:
results[s1][s2]['run'] += 1
except KeyError:
results[s1][s2]['run'] = 1
else: # do this or KeyErrors at runtime
results[s1][s2]['run'] = 0
inputFile.close()
outputFile = open(args.outFile, 'wb')
outputWriter = csv.writer(outputFile)
# output
# brief header, standard for all output
outputWriter.writerow(["Results for input file %s" %(args.inFile)])
outputWriter.writerow(["Generated by %s version %s" %(PROGRAM, VERSION)])
outputWriter.writerow(["On Transition ('TXNS') table and runs table, initial state is shown in leftmost column, following state is shown in rows."])
#writer.writerow([a,x,b,'dingo','wingtree %s' %('yabber'),VERSION])
s1List = sorted(results.keys())
if args.transitions:
outputWriter.writerow([])
# table cols
row = ["TXNS"]
for s in states:
row.append("\'%s" %(s))
row.append("TOTAL")
outputWriter.writerow(row)
#table rows
s1Count, s2Count = 0, 0
for s1 in s1List:
row = []
s1Count = 0
row.append("\'%s" %(s1))
for s2 in states:
if s2 in results[s1]:
s2Count = results[s1][s2]['count']
row.append(s2Count)
s1Count += s2Count
else:
row.append('0')
# total
if s1Count:
row.append(s1Count)
else:
row.append('0')
outputWriter.writerow(row)
outputWriter.writerow([])
if args.runs:
# table cols
row = ["RUNS"]
for s in states:
row.append("\'%s" %(s))
row.append("TOTAL")
outputWriter.writerow(row)
#table rows
runTxCount = 0
for s1 in s1List:
row = []
runTxCount = 0
row.append("\'%s" %(s1))
for s2 in states:
if s2 in results[s1]:
runTxCount += results[s1][s2]['run']
row.append(results[s1][s2]['run'])
else:
row.append('0')
# total
if runTxCount:
row.append(runTxCount)
else:
row.append('0')
outputWriter.writerow(row)
outputWriter.writerow([])
if args.events:
# corresponds to events{} variable in s2hl.py
events = ['UE', 'NE', 'GO', 'K',
'SB', 'DI', 'CS', 'EPO',
'PO', 'WP', 'PB', 'BK',
'OA', 'EF', 'W', 'IW',
'HBP', 'I', 'E', 'FC',
'S', 'D', 'T', 'HR',
'MP']
# a table for each event
for s1 in s1List:
# table cols
row = []
row.append("From %s to:" %(s1))
for e in events:
row.append(e)
row.append("TOTAL")
outputWriter.writerow(row)
# table rows
eventCount = 0
for s2 in states:
if s2 in results[s1]:
row = []
row.append("\'%s" %(s2))
for v in range(0,25):
v = str(v)
if v in results[s1][s2]:
row.append("%s" %(results[s1][s2][v]))
else:
row.append('0')
eventCount = results[s1][s2]['count']
if eventCount:
row.append(eventCount)
else:
row.append('0')
outputWriter.writerow(row)
outputWriter.writerow([])
outputFile.close()