/
table.py
44 lines (33 loc) · 1.24 KB
/
table.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
# -*- coding: utf-8 -*-
from collections import OrderedDict
from ..chemistry import Substance
from .numbers import number_to_scientific_html
class Table(object):
def __init__(self, rows, headers=None):
self.rows, self.headers = rows, headers
def _html(self, printer, **kwargs):
def map_fmt(cont, fmt, joiner="\n"):
return joiner.join(map(lambda x: fmt % printer._print(x, **kwargs), cont))
rows = [map_fmt(self.headers, "<th>%s</th>")] + [
map_fmt(row, "<td>%s</td>") for row in self.rows
]
return "<table>%s</table>" % map_fmt(rows, "<tr>%s</tr>")
def _repr_html_(self):
from .web import html
return html(self)
def as_per_substance_html_table(
cont, substances=None, header=None, substance_factory=Substance.from_formula
):
""" """
if substances is None:
substances = OrderedDict([(k, substance_factory(k)) for k in cont])
def _elem(k):
try:
return cont[k]
except (IndexError, TypeError):
return cont[list(substances.keys()).index(k)]
rows = [
(v.html_name, number_to_scientific_html(_elem(k)))
for k, v in substances.items()
]
return Table(rows, ["Substance", header or ""])