-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumdenom_impl.py
More file actions
executable file
·190 lines (156 loc) · 6.73 KB
/
numdenom_impl.py
File metadata and controls
executable file
·190 lines (156 loc) · 6.73 KB
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
#!/usr/bin/python
import sys
import gzip
import csv
import json
import base64
from collections import defaultdict
from pathlib import Path
from typing import List, Set, Dict, Any, Tuple
HTML_TEMPLATE=b"%{html_template}"
def quicktable(inputfile: str, outputfile: str, table_title: str, description_file: str|None) -> None:
header_parsed = False
header : List[str]
filters : List[str] = []
main_denom: int | None = None
valcolumns : List[str] = []
filter_indexes : Dict[str, int] = dict()
value_num_indexes : Dict[str, int] = dict()
value_denom_indexes : Dict[str, int] = dict()
first_unused_filter_index = 0;
first_unused_value_index = 0;
filtervals: Dict[str, Set[str]] = defaultdict(lambda:set())
filters_data: List[List[str]] = []
values_data: List[List[float]] = []
linectr = 0
description=""
description_autopre=False
if df := description_file:
if df.startswith("pre:"):
description_autopre=True
df=description_file[4:]
with open(df, "r") as f:
description = f.read()
if description_autopre:
description = "<pre>"+description+"</pre>"
with open(inputfile, newline='') as f:
ff = csv.reader(f)
for row in ff:
linectr+=1
if not header_parsed:
header = row
filters_tmp : Set[str] = set([])
valcolumns_tmp : Set[str] = set([])
for x in row:
if x == "denom":
main_denom = first_unused_value_index
first_unused_value_index+=1
elif x.endswith("_num"):
rawcolname = x.removesuffix("_num")
valcolumns_tmp.add(rawcolname)
value_num_indexes[rawcolname] = first_unused_value_index
first_unused_value_index+=1
elif x.endswith("_denom"):
rawcolname = x.removesuffix("_denom")
valcolumns_tmp.add(rawcolname)
value_denom_indexes[rawcolname] = first_unused_value_index
first_unused_value_index+=1
else:
filters_tmp.add(x)
header_parsed = True
filters = sorted(list(filters_tmp))
valcolumns = sorted(list(valcolumns_tmp))
for ffff in filters:
filter_indexes[ffff] = first_unused_filter_index
first_unused_filter_index+=1
#print(filters, valcolumns)
else:
# iterated csv row is not the header
d_filters = [""] * first_unused_filter_index
d_values = [0.0] * first_unused_value_index
def parsenum(x : str) -> float:
x = x.strip()
if x == "" or x == "NULL":
raise ValueError
try:
v = float(x)
return v
except:
print(f"Invalid number {x} for {h} on line {linectr}")
sys.exit(2)
for (i, x) in enumerate(row):
if i >= len(header): pass
h = header[i]
try:
if h == "denom":
assert main_denom is not None
d_values[main_denom] = parsenum(x)
elif h.endswith("_num"):
rawcolname = h.removesuffix("_num")
d_values[value_num_indexes[rawcolname]] = parsenum(x)
elif h.endswith("_denom"):
rawcolname = h.removesuffix("_denom")
d_values[value_denom_indexes[rawcolname]] = parsenum(x)
else:
filtervals[h].add(x)
d_filters[filter_indexes[h]] = x
except ValueError:
# ignore explicit ValueErrors from `parsenum`, but not other things
pass
filters_entry : List[str] = []
values_entry : List[float] = []
filters_data.append(d_filters)
values_data.append(d_values)
# end iterating rows
filtervals2 : List[Dict[str, Any]] = []
valcolinfos: List[Dict[str, Any]] = []
for ffff in filters:
vals = sorted(list(filtervals[ffff]))
filtervals2.append({'name': ffff, 'vals': vals})
for valcol in valcolumns:
if valcol not in value_denom_indexes and main_denom is None:
print(f"Value column `{valcol}` has only numerator (`{valcol}_num`), but not denominator (`{valcol}_denom`) and there is no main denominator (`denom`)")
sys.exit(2)
if valcol not in value_num_indexes:
print(f"Value column `{valcol}` has only denominator (`{valcol}_denum`), but not numerator (`{valcol}_num`)")
sys.exit(2)
numindex = value_num_indexes[valcol]
denomindex : int
if valcol in value_denom_indexes:
denomindex =value_denom_indexes[valcol]
else:
assert main_denom is not None
denomindex = main_denom
valcolinfos.append({'name': valcol, 'num' : numindex, 'denom': denomindex})
if not filters:
print("There are no filter columns. Usefullness of the table is questionable.")
if not valcolumns:
print("There are no value columns (ending with `_num` or `_denom`). Usefullness of the table is questionable.")
datablock = json.dumps({
'filters': filtervals2,
'values':valcolinfos,
'filters_data':filters_data,
'values_data':values_data,
'main_denom': main_denom,
})
#print(datablock)
subst = base64.standard_b64encode(gzip.compress(datablock.encode("UTF-8"))).decode("UTF-8")
#print(subst)
with open(outputfile, "wt") as of:
txt : str = base64.standard_b64decode(HTML_TEMPLATE).decode("UTF-8")
txt = txt.replace('%{data}', subst)
txt = txt.replace('%{title}', table_title)
txt = txt.replace('%{description}', description)
of.write(txt)
if __name__ == '__main__':
if len(sys.argv)<3:
print("Usage: quicktable.py input.csv output.html [title]")
else:
inputfile = sys.argv[1]
table_title = Path(inputfile).stem
description_file = None
if len(sys.argv)>=4:
table_title = sys.argv[3]
if len(sys.argv)>=5:
description_file = sys.argv[4]
quicktable(sys.argv[1], sys.argv[2], table_title, description_file)