-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable-ssl.py
More file actions
executable file
·211 lines (194 loc) · 6.25 KB
/
table-ssl.py
File metadata and controls
executable file
·211 lines (194 loc) · 6.25 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env python3
# Outputs a table for SHAPE, in the style of Table 1 in the paper:
# - includes a percentage with each x count
# - sedes following the anchor sedes are also colored, for as far as SHAPE
# reaches.
import collections
import csv
import html
import sys
import common
SHAPE = "⏑⏑–"
STYLE_TABLE = (
("border-collapse", "collapse"),
("border-spacing", "1pt"),
)
STYLE_X = (
("font-size", "8pt"),
)
STYLE_Z = (
("font-size", "7pt"),
)
STYLE_PERCENT = (
("font-size", "7pt"),
)
STYLE_CELL = (
("font-size", "8pt"),
("vertical-align", "middle"),
("padding", "1pt"),
("min-width", "12pt"),
("line-height", "100%"),
("border", "solid 1pt #d3d3d3"),
)
STYLE_IMPERMISSIBLE = (
("color", "#d3d3d3"),
("text-align", "center"),
("background-color", "cornsilk"),
) + STYLE_CELL
STYLE_HEADER = (
("background-color", "lavender"),
) + STYLE_CELL
STYLE_TH = (
("font-weight", "bold"),
) + STYLE_HEADER
STYLE_LEFT = (
("text-align", "left"),
)
STYLE_RIGHT = (
("text-align", "right"),
)
# Format 1.0 as "100%", other less percentages with 1 digit of precision.
def format_percent(x):
if x >= 1.0:
return f"{x*100:g}%"
else:
return f"{x*100:.1f}%"
Entry = collections.namedtuple("Entry", ("x", "z"))
M = {}
r = csv.DictReader(sys.stdin)
for row in r:
key = (row["metrical_shape"], row["work"], float(row["sedes"]))
assert key not in M
M[key] = Entry(int(row["x"]), float(row["z"]) if row["z"] != "" else None)
def metrical_length(shape):
return sum({"⏑": 0.5, "–": 1.0}[c] for c in shape)
print("""\
<html>
<head>
<meta charset=utf-8>
</head>
<body>
""")
print(common.html_start_tag_style("table", STYLE_TABLE))
print(common.html_start_tag("tr"))
print(
common.html_start_tag_style("th", STYLE_TH + STYLE_LEFT) +
html.escape("Work") +
common.html_end_tag("th")
)
for sedes in common.KNOWN_SEDES:
print(
common.html_start_tag_style("th", STYLE_TH + STYLE_RIGHT) +
html.escape(sedes) +
common.html_end_tag("th")
)
print(
common.html_start_tag_style("th", STYLE_TH + STYLE_RIGHT) +
"Total (Σ<var>x</var>)" +
common.html_end_tag("th")
)
print(common.html_end_tag("tr"))
for sedes in map(float, common.KNOWN_SEDES):
if common.is_metrically_permissible(SHAPE, sedes):
M[(SHAPE, "total", sedes)] = Entry(0, 0)
for work in common.KNOWN_WORKS + (common.Work("total", "TOTAL", "TOTAL", "Book"),):
print(common.html_start_tag("tr"))
print(
common.html_start_tag_style("td", STYLE_HEADER + STYLE_LEFT) +
work.html_name +
common.html_end_tag("td")
)
xvec = []
for sedes in map(float, common.KNOWN_SEDES):
entry = M.get((SHAPE, work.id, sedes))
if entry is not None:
xvec.append(entry.x)
recent_sedes = None
recent_entry = None
for sedes in map(float, common.KNOWN_SEDES):
styles = []
entry = M.get((SHAPE, work.id, sedes))
if not common.is_metrically_permissible(SHAPE, sedes):
assert entry is None, entry
if recent_entry is not None and sedes - recent_sedes < metrical_length(SHAPE):
styles.extend(common.z_css(recent_entry.z))
styles.extend(STYLE_CELL)
styles.append(("border-left-style", "dashed"))
styles.append(("border-right-style", "none"))
contents = ""
else:
styles.extend(STYLE_IMPERMISSIBLE)
contents = html.escape("✖\ufe0e")
else:
if entry is None:
entry = Entry(0, common.expectancy(0, xvec))
try:
del M[(SHAPE, work.id, sedes)]
except KeyError:
pass
if work.id != "total":
M[(SHAPE, "total", sedes)] = Entry(M[(SHAPE, "total", sedes)].x + entry.x, 0)
contents = ""
contents += (
common.html_start_tag_style("span", STYLE_X) +
html.escape(f"{entry.x:,}") +
common.html_end_tag("span")
)
contents += common.html_start_tag("br")
if sum(xvec) > 0:
contents += (
common.html_start_tag_style("span", STYLE_PERCENT) +
html.escape(format_percent(entry.x/sum(xvec))) +
common.html_end_tag("span")
)
else:
contents += "\u200c"
contents += common.html_start_tag("br")
if entry.z is not None:
contents += (
common.html_start_tag_style("span", STYLE_Z) +
html.escape("{:+.02f}".format(entry.z).replace("-", "−")) +
common.html_end_tag("span")
)
else:
contents += "\u200c"
styles.extend(common.z_css(entry.z))
styles.extend(STYLE_CELL)
styles.extend(STYLE_RIGHT)
try:
next_sedes = float(common.KNOWN_SEDES[common.KNOWN_SEDES.index(f"{sedes:g}") + 1])
if not common.is_metrically_permissible(SHAPE, next_sedes) and next_sedes - sedes < metrical_length(SHAPE):
styles.append(("border-right-style", "none"))
except ValueError:
pass
recent_sedes = sedes
recent_entry = entry
print(
common.html_start_tag_style("td", styles) +
contents +
common.html_end_tag("td")
)
print(common.html_start_tag_style("td", STYLE_CELL + STYLE_RIGHT))
print(
common.html_start_tag_style("span", STYLE_X) +
html.escape('{:,}'.format(sum(xvec))) +
common.html_end_tag("span")
)
print(common.html_end_tag("td"))
print(common.html_end_tag("tr"))
total_xvec = []
for sedes in map(float, common.KNOWN_SEDES):
try:
total_xvec.append(M[(SHAPE, "total", sedes)].x)
except KeyError:
pass
for sedes in map(float, common.KNOWN_SEDES):
try:
M[(SHAPE, "total", sedes)] = Entry(M[(SHAPE, "total", sedes)].x, common.expectancy(M[(SHAPE, "total", sedes)].x, total_xvec))
except KeyError:
pass
print("</table>")
print("""\
</body>
</html>
""")