Skip to content

Commit 49ecbac

Browse files
committed
Support multi-run modes in combine-exec.py
1 parent 7a2702c commit 49ecbac

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

util/CPU2006/combine-exec.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import sys
77
import argparse
88
from contextlib import ExitStack
9-
from typing import Iterable, List, Tuple
9+
from typing import Dict, Iterable, List, Tuple
10+
from collections import Counter
1011
from openpyxl import Workbook
1112
from openpyxl.utils import get_column_letter
1213

@@ -25,28 +26,33 @@ def is_blank_row(row: List[str]) -> bool:
2526

2627

2728
def merge_tables(str_tables: Iterable[str]) -> str:
28-
data = dict()
29+
data: Dict[str, List[List[str]]] = dict()
2930
tables = [list(csv.reader(table.splitlines())) for table in str_tables]
3031

3132
for row in tables[0]:
3233
if row:
33-
data[row[0]] = row
34+
data.setdefault(row[0], []).append(row)
3435

3536
for table in tables:
37+
nth: Dict[str, int] = Counter()
3638
for row in table:
3739
if not is_blank_row(row):
40+
index = nth[row[0]]
3841
if row[0] in data:
39-
if not is_blank_row(data[row[0]]) and data[row[0]] != row:
40-
raise DuplicateDataError(data[row[0]], row, f'Duplicate data for {row[0]}.')
41-
data[row[0]] = row
42+
if not is_blank_row(data[row[0]][index]) and data[row[0]][index] != row:
43+
raise DuplicateDataError(data[row[0]][index], row, f'Duplicate data for {row[0]}.')
44+
data[row[0]][index] = row
45+
nth[row[0]] += 1
4246

4347
out = StringIO()
4448
writer = csv.writer(out)
49+
nth: Dict[str, int] = Counter()
4550
for row in tables[0]:
4651
if not row:
4752
continue
48-
best_row = data[row[0]]
49-
writer.writerow(best_row)
53+
index = nth[row[0]]
54+
writer.writerow(data[row[0]][index])
55+
nth[row[0]] += 1
5056

5157
return out.getvalue()
5258

@@ -64,7 +70,6 @@ def extract_tables(contents: str) -> Iterable[Tuple[str, str]]:
6470
def main(files, out: str):
6571
wb = Workbook()
6672
files = [f.read() for f in files]
67-
xy = list(extract_tables(files[0]))
6873
tbls = map(extract_tables, files)
6974
for tbl_group in zip(*tbls):
7075
assert len(set(name for name, _ in tbl_group)) == 1

0 commit comments

Comments
 (0)