-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge.py
226 lines (203 loc) · 7.08 KB
/
merge.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
'''
A script to merge two tables generated by Piskvork
Kai Sun
'''
import sys
def cmp_to_key(mycmp):
class K:
def __init__(self, obj, *args):
self.obj = obj
def __lt__(self, other):
return mycmp(self.obj, other.obj) < 0
def __gt__(self, other):
return mycmp(self.obj, other.obj) > 0
def __eq__(self, other):
return mycmp(self.obj, other.obj) == 0
def __le__(self, other):
return mycmp(self.obj, other.obj) <= 0
def __ge__(self, other):
return mycmp(self.obj, other.obj) >= 0
def __ne__(self, other):
return mycmp(self.obj, other.obj) != 0
return K
def table_merge_fill(A, result):
head = A[1]
body = A[2]
for i in xrange(len(body)):
line = body[i]
x = line[0]
for j in xrange(1, len(line)):
y = head[j-1]
score = line[j]
if '-' not in score:
win, loss = score.replace(' ','').split(':')
win, loss = int(win), int(loss)
result[x][y][0] += win
result[x][y][1] += loss
def table_merge(A, B):
title = A[0]
date = A[3]
col = []
for x in A[1] + B[1]:
if x not in col:
col += [x]
row = []
for x in A[2] + B[2]:
if x[0] not in row:
row += [x[0]]
result = {}
for x in row:
result[x] = {}
for y in col:
result[x][y] = [0, 0]
table_merge_fill(A, result)
table_merge_fill(B, result)
for x in result:
for y in result[x]:
if y not in result: continue
if x not in result[y]: continue
if result[x][y][0] != 0 or result[x][y][1] != 0:
if result[y][x][0] == 0 and result[y][x][1] == 0:
result[y][x][0] = result[x][y][1]
result[y][x][1] = result[x][y][0]
order = {}
i = 0
for x in A[2]:
if x[0] not in order:
order[x[0]] = [i,0,0,0]
i += 1
for x in B[2]:
if x[0] not in order:
order[x[0]] = [i,0,0,0]
i += 1
for x in A[1]:
if x not in order:
order[x] = [i,0,0,0]
i += 1
for x in B[1]:
if x not in order:
order[x] = [i,0,0,0]
i += 1
for x in row:
for y in col:
if x == y: continue
order[y][1] += result[x][y][0]
order[y][2] += result[x][y][1]
if result[x][y][0] > result[x][y][1]:
order[y][3] += 3
elif result[x][y][0] == result[x][y][1]:
order[y][3] += 1
ordercol = [x for x in col]
orderrow = [x for x in row]
cmpf = lambda x,y: \
order[x][3] - order[y][3] if order[x][3] != order[y][3] else \
order[x][1] * order[y][2] - order[y][1] * order[x][2] if order[x][1] * order[y][2] != order[y][1] * order[x][2] else \
order[x][0] - order[y][0]
ordercol.sort(key = cmp_to_key(cmpf), reverse=True)
orderrow.sort(key = cmp_to_key(cmpf), reverse=True)
head = [x for x in ordercol]
body = []
for x in orderrow:
line = []
line += [x]
for y in ordercol:
if x == y and result[x][y][0] == 0 and result[x][y][1] == 0:
line += ['-']
else:
line += [str(result[x][y][0]) + ' : ' + str(result[x][y][1])]
body += [line]
return [title, head, body, date]
def table_load(fn):
f = open(fn, "r")
title = f.readline()
head = f.readline().replace('\n', '').replace('\r', '')
head = head.replace('<TR><TD class="dash">-</TD>', '')
head = head.replace('<TH>', '')
head = head.replace('</TH></TR>', '')
head = head.split('</TH>')
body = []
line = f.readline()
while line:
if '</HTML>' in line:
date = line
break
if '<TR><TH>Total</TH>' not in line and \
'<TR><TH>Ratio</TH>' not in line and \
'<TR><TH>Points</TH>' not in line:
line = line.replace('\n', '').replace('\r', '')
line = line.replace('<TR><TH>', '')
line = line.replace('</TH>', '</TD>')
line = line.replace('</TD></TR>', '')
line = line.replace('<TD class="dash">', '')
line = line.replace('<TD class="win">', '')
line = line.replace('<TD class="loss">', '')
line = line.replace('<TD class="draw">', '')
line = line.split('</TD>')
body += [line]
line = f.readline()
f.close()
return [title, head, body, date]
def table_dump(fn, C):
f = open(fn, "w")
title, head, body, date = C
f.write(title)
stat = []
f.write('<TR><TD class="dash">-</TD>')
for i in xrange(len(head)):
stat += [[0,0,0]]
f.write('<TH>' + head[i] + '</TH>')
f.write('</TR>\n')
for i in xrange(len(body)):
line = body[i]
f.write('<TR><TH>' + line[0] + '</TH>')
for j in xrange(1, len(line)):
score = line[j]
if '-' in score:
f.write('<TD class="dash">' + score + '</TD>')
else:
win, loss = score.replace(' ','').split(':')
win, loss = int(win), int(loss)
if win > loss:
f.write('<TD class="win">' + score + '</TD>')
stat[j-1][2] += 3
elif win < loss:
f.write('<TD class="loss">' + score + '</TD>')
stat[j-1][2] += 0
else:
f.write('<TD class="draw">' + score + '</TD>')
stat[j-1][2] += 1
stat[j-1][0] += win
stat[j-1][1] += loss
f.write('</TR>\n')
f.write('<TR><TH>Total</TH>')
for i in xrange(len(stat)):
score = str(stat[i][0]) + ' : ' + str(stat[i][1])
if stat[i][0] > stat[i][1]:
f.write('<TD class="win">' + score + '</TD>')
elif stat[i][0] < stat[i][1]:
f.write('<TD class="loss">' + score + '</TD>')
else:
f.write('<TD class="draw">' + score + '</TD>')
f.write('</TR>\n')
f.write('<TR><TH>Ratio</TH>')
for i in xrange(len(stat)):
score = '-' if int(stat[i][1]) == 0 else "%.3f" % (float(stat[i][0]) / float(stat[i][1]))
f.write('<TD>' + score + '</TD>')
f.write('</TR>\n')
f.write('<TR><TH>Points</TH>')
for i in xrange(len(stat)):
f.write('<TD>' + str(stat[i][2]) + '</TD>')
f.write('</TR>\n')
f.write(date)
f.close()
def main():
if len(sys.argv) != 4:
print 'Merge file A and file B, and output the result to file C'
print ' python merge.py A B C'
return
A = table_load(sys.argv[1])
B = table_load(sys.argv[2])
C = table_merge(A, B)
table_dump(sys.argv[3], C)
if __name__ == '__main__':
main()