onnxslim 0.1.80__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- onnxslim/__init__.py +16 -0
- onnxslim/__main__.py +4 -0
- onnxslim/argparser.py +215 -0
- onnxslim/cli/__init__.py +1 -0
- onnxslim/cli/_main.py +180 -0
- onnxslim/core/__init__.py +219 -0
- onnxslim/core/optimization/__init__.py +146 -0
- onnxslim/core/optimization/dead_node_elimination.py +151 -0
- onnxslim/core/optimization/subexpression_elimination.py +76 -0
- onnxslim/core/optimization/weight_tying.py +59 -0
- onnxslim/core/pattern/__init__.py +249 -0
- onnxslim/core/pattern/elimination/__init__.py +5 -0
- onnxslim/core/pattern/elimination/concat.py +61 -0
- onnxslim/core/pattern/elimination/reshape.py +77 -0
- onnxslim/core/pattern/elimination/reshape_as.py +64 -0
- onnxslim/core/pattern/elimination/slice.py +108 -0
- onnxslim/core/pattern/elimination/unsqueeze.py +92 -0
- onnxslim/core/pattern/fusion/__init__.py +8 -0
- onnxslim/core/pattern/fusion/concat_reshape.py +50 -0
- onnxslim/core/pattern/fusion/convadd.py +70 -0
- onnxslim/core/pattern/fusion/convbn.py +86 -0
- onnxslim/core/pattern/fusion/convmul.py +69 -0
- onnxslim/core/pattern/fusion/gelu.py +47 -0
- onnxslim/core/pattern/fusion/gemm.py +330 -0
- onnxslim/core/pattern/fusion/padconv.py +89 -0
- onnxslim/core/pattern/fusion/reduce.py +67 -0
- onnxslim/core/pattern/registry.py +28 -0
- onnxslim/misc/__init__.py +0 -0
- onnxslim/misc/tabulate.py +2681 -0
- onnxslim/third_party/__init__.py +0 -0
- onnxslim/third_party/_sympy/__init__.py +0 -0
- onnxslim/third_party/_sympy/functions.py +205 -0
- onnxslim/third_party/_sympy/numbers.py +397 -0
- onnxslim/third_party/_sympy/printers.py +491 -0
- onnxslim/third_party/_sympy/solve.py +172 -0
- onnxslim/third_party/_sympy/symbol.py +102 -0
- onnxslim/third_party/onnx_graphsurgeon/__init__.py +15 -0
- onnxslim/third_party/onnx_graphsurgeon/exporters/__init__.py +1 -0
- onnxslim/third_party/onnx_graphsurgeon/exporters/base_exporter.py +33 -0
- onnxslim/third_party/onnx_graphsurgeon/exporters/onnx_exporter.py +432 -0
- onnxslim/third_party/onnx_graphsurgeon/graph_pattern/__init__.py +4 -0
- onnxslim/third_party/onnx_graphsurgeon/graph_pattern/graph_pattern.py +466 -0
- onnxslim/third_party/onnx_graphsurgeon/importers/__init__.py +1 -0
- onnxslim/third_party/onnx_graphsurgeon/importers/base_importer.py +33 -0
- onnxslim/third_party/onnx_graphsurgeon/importers/onnx_importer.py +558 -0
- onnxslim/third_party/onnx_graphsurgeon/ir/__init__.py +0 -0
- onnxslim/third_party/onnx_graphsurgeon/ir/function.py +274 -0
- onnxslim/third_party/onnx_graphsurgeon/ir/graph.py +1575 -0
- onnxslim/third_party/onnx_graphsurgeon/ir/node.py +266 -0
- onnxslim/third_party/onnx_graphsurgeon/ir/tensor.py +504 -0
- onnxslim/third_party/onnx_graphsurgeon/logger/__init__.py +1 -0
- onnxslim/third_party/onnx_graphsurgeon/logger/logger.py +261 -0
- onnxslim/third_party/onnx_graphsurgeon/util/__init__.py +0 -0
- onnxslim/third_party/onnx_graphsurgeon/util/exception.py +20 -0
- onnxslim/third_party/onnx_graphsurgeon/util/misc.py +252 -0
- onnxslim/third_party/symbolic_shape_infer.py +3273 -0
- onnxslim/utils.py +794 -0
- onnxslim/version.py +1 -0
- onnxslim-0.1.80.dist-info/METADATA +207 -0
- onnxslim-0.1.80.dist-info/RECORD +65 -0
- onnxslim-0.1.80.dist-info/WHEEL +5 -0
- onnxslim-0.1.80.dist-info/entry_points.txt +2 -0
- onnxslim-0.1.80.dist-info/licenses/LICENSE +21 -0
- onnxslim-0.1.80.dist-info/top_level.txt +1 -0
- onnxslim-0.1.80.dist-info/zip-safe +1 -0
|
@@ -0,0 +1,2681 @@
|
|
|
1
|
+
"""Pretty-print tabular data."""
|
|
2
|
+
|
|
3
|
+
import dataclasses
|
|
4
|
+
import io
|
|
5
|
+
import math
|
|
6
|
+
import re
|
|
7
|
+
import textwrap
|
|
8
|
+
import warnings
|
|
9
|
+
from collections import namedtuple
|
|
10
|
+
from collections.abc import Iterable, Sized
|
|
11
|
+
from functools import partial, reduce
|
|
12
|
+
from html import escape as htmlescape
|
|
13
|
+
from itertools import chain
|
|
14
|
+
from itertools import zip_longest as izip_longest
|
|
15
|
+
|
|
16
|
+
try:
|
|
17
|
+
import wcwidth # optional wide-character (CJK) support
|
|
18
|
+
except ImportError:
|
|
19
|
+
wcwidth = None
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _is_file(f):
|
|
23
|
+
"""Check if an object 'f' is an instance of io.IOBase."""
|
|
24
|
+
return isinstance(f, io.IOBase)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
__all__ = ["simple_separated_format", "tabulate", "tabulate_formats"]
|
|
28
|
+
try:
|
|
29
|
+
from .version import version as __version__ # noqa: F401
|
|
30
|
+
except ImportError:
|
|
31
|
+
pass # running __init__.py as a script, AppVeyor pytests
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
# minimum extra space in headers
|
|
35
|
+
MIN_PADDING = 2
|
|
36
|
+
|
|
37
|
+
# Whether or not to preserve leading/trailing whitespace in data.
|
|
38
|
+
PRESERVE_WHITESPACE = False
|
|
39
|
+
|
|
40
|
+
_DEFAULT_FLOATFMT = "g"
|
|
41
|
+
_DEFAULT_INTFMT = ""
|
|
42
|
+
_DEFAULT_MISSINGVAL = ""
|
|
43
|
+
# default align will be overwritten by "left", "center" or "decimal"
|
|
44
|
+
# depending on the formatter
|
|
45
|
+
_DEFAULT_ALIGN = "default"
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
# if True, enable wide-character (CJK) support
|
|
49
|
+
WIDE_CHARS_MODE = wcwidth is not None
|
|
50
|
+
|
|
51
|
+
# Constant that can be used as part of passed rows to generate a separating line
|
|
52
|
+
# It is purposely an unprintable character, very unlikely to be used in a table
|
|
53
|
+
SEPARATING_LINE = "\001"
|
|
54
|
+
|
|
55
|
+
Line = namedtuple("Line", ["begin", "hline", "sep", "end"])
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
DataRow = namedtuple("DataRow", ["begin", "sep", "end"])
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
# A table structure is supposed to be:
|
|
62
|
+
#
|
|
63
|
+
# --- lineabove ---------
|
|
64
|
+
# headerrow
|
|
65
|
+
# --- linebelowheader ---
|
|
66
|
+
# datarow
|
|
67
|
+
# --- linebetweenrows ---
|
|
68
|
+
# ... (more datarows) ...
|
|
69
|
+
# --- linebetweenrows ---
|
|
70
|
+
# last datarow
|
|
71
|
+
# --- linebelow ---------
|
|
72
|
+
#
|
|
73
|
+
# TableFormat's line* elements can be
|
|
74
|
+
#
|
|
75
|
+
# - either None, if the element is not used,
|
|
76
|
+
# - or a Line tuple,
|
|
77
|
+
# - or a function: [col_widths], [col_alignments] -> string.
|
|
78
|
+
#
|
|
79
|
+
# TableFormat's *row elements can be
|
|
80
|
+
#
|
|
81
|
+
# - either None, if the element is not used,
|
|
82
|
+
# - or a DataRow tuple,
|
|
83
|
+
# - or a function: [cell_values], [col_widths], [col_alignments] -> string.
|
|
84
|
+
#
|
|
85
|
+
# padding (an integer) is the amount of white space around data values.
|
|
86
|
+
#
|
|
87
|
+
# with_header_hide:
|
|
88
|
+
#
|
|
89
|
+
# - either None, to display all table elements unconditionally,
|
|
90
|
+
# - or a list of elements not to be displayed if the table has column headers.
|
|
91
|
+
#
|
|
92
|
+
TableFormat = namedtuple(
|
|
93
|
+
"TableFormat",
|
|
94
|
+
[
|
|
95
|
+
"lineabove",
|
|
96
|
+
"linebelowheader",
|
|
97
|
+
"linebetweenrows",
|
|
98
|
+
"linebelow",
|
|
99
|
+
"headerrow",
|
|
100
|
+
"datarow",
|
|
101
|
+
"padding",
|
|
102
|
+
"with_header_hide",
|
|
103
|
+
],
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _is_separating_line(row):
|
|
108
|
+
"""Determine if a row is a separating line based on its type and specific content conditions."""
|
|
109
|
+
return type(row) in {list, str} and (
|
|
110
|
+
(len(row) >= 1 and row[0] == SEPARATING_LINE) or (len(row) >= 2 and row[1] == SEPARATING_LINE)
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def _pipe_segment_with_colons(align, colwidth):
|
|
115
|
+
"""Return a segment of a horizontal line with optional colons which indicate column's alignment (as in `pipe` output
|
|
116
|
+
format).
|
|
117
|
+
"""
|
|
118
|
+
w = colwidth
|
|
119
|
+
if align in {"right", "decimal"}:
|
|
120
|
+
return ("-" * (w - 1)) + ":"
|
|
121
|
+
elif align == "center":
|
|
122
|
+
return ":" + ("-" * (w - 2)) + ":"
|
|
123
|
+
elif align == "left":
|
|
124
|
+
return ":" + ("-" * (w - 1))
|
|
125
|
+
else:
|
|
126
|
+
return "-" * w
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def _pipe_line_with_colons(colwidths, colaligns):
|
|
130
|
+
"""Return a horizontal line with optional colons to indicate column's alignment (as in `pipe` output format)."""
|
|
131
|
+
if not colaligns: # e.g. printing an empty data frame (github issue #15)
|
|
132
|
+
colaligns = [""] * len(colwidths)
|
|
133
|
+
segments = [_pipe_segment_with_colons(a, w) for a, w in zip(colaligns, colwidths)]
|
|
134
|
+
return "|" + "|".join(segments) + "|"
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _mediawiki_row_with_attrs(separator, cell_values, colwidths, colaligns):
|
|
138
|
+
"""Returns a MediaWiki table row with specific alignment attributes for each cell based on given parameters."""
|
|
139
|
+
alignment = {
|
|
140
|
+
"left": "",
|
|
141
|
+
"right": 'style="text-align: right;"| ',
|
|
142
|
+
"center": 'style="text-align: center;"| ',
|
|
143
|
+
"decimal": 'style="text-align: right;"| ',
|
|
144
|
+
}
|
|
145
|
+
# hard-coded padding _around_ align attribute and value together
|
|
146
|
+
# rather than padding parameter which affects only the value
|
|
147
|
+
values_with_attrs = [" " + alignment.get(a, "") + c + " " for c, a in zip(cell_values, colaligns)]
|
|
148
|
+
colsep = separator * 2
|
|
149
|
+
return (separator + colsep.join(values_with_attrs)).rstrip()
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def _textile_row_with_attrs(cell_values, colwidths, colaligns):
|
|
153
|
+
"""Generate a Textile-formatted table row with specified cell values, column widths, and alignments."""
|
|
154
|
+
cell_values[0] += " "
|
|
155
|
+
alignment = {"left": "<.", "right": ">.", "center": "=.", "decimal": ">."}
|
|
156
|
+
values = (alignment.get(a, "") + v for a, v in zip(colaligns, cell_values))
|
|
157
|
+
return "|" + "|".join(values) + "|"
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def _html_begin_table_without_header(colwidths_ignore, colaligns_ignore):
|
|
161
|
+
"""Generate the beginning of an HTML table without a header row."""
|
|
162
|
+
return "<table>\n<tbody>"
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def _html_row_with_attrs(celltag, unsafe, cell_values, colwidths, colaligns):
|
|
166
|
+
"""Generate an HTML table row with specified attributes for each cell."""
|
|
167
|
+
alignment = {
|
|
168
|
+
"left": "",
|
|
169
|
+
"right": ' style="text-align: right;"',
|
|
170
|
+
"center": ' style="text-align: center;"',
|
|
171
|
+
"decimal": ' style="text-align: right;"',
|
|
172
|
+
}
|
|
173
|
+
if unsafe:
|
|
174
|
+
values_with_attrs = [
|
|
175
|
+
"<{0}{1}>{2}</{0}>".format(celltag, alignment.get(a, ""), c) for c, a in zip(cell_values, colaligns)
|
|
176
|
+
]
|
|
177
|
+
else:
|
|
178
|
+
values_with_attrs = [
|
|
179
|
+
"<{0}{1}>{2}</{0}>".format(celltag, alignment.get(a, ""), htmlescape(c))
|
|
180
|
+
for c, a in zip(cell_values, colaligns)
|
|
181
|
+
]
|
|
182
|
+
rowhtml = f"<tr>{''.join(values_with_attrs).rstrip()}</tr>"
|
|
183
|
+
if celltag == "th": # it's a header row, create a new table header
|
|
184
|
+
rowhtml = f"<table>\n<thead>\n{rowhtml}\n</thead>\n<tbody>"
|
|
185
|
+
return rowhtml
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def _moin_row_with_attrs(celltag, cell_values, colwidths, colaligns, header=""):
|
|
189
|
+
"""Generate a row of HTML table cells with specified tags, values, alignments, and optional headers."""
|
|
190
|
+
alignment = {
|
|
191
|
+
"left": "",
|
|
192
|
+
"right": '<style="text-align: right;">',
|
|
193
|
+
"center": '<style="text-align: center;">',
|
|
194
|
+
"decimal": '<style="text-align: right;">',
|
|
195
|
+
}
|
|
196
|
+
values_with_attrs = [
|
|
197
|
+
f"{celltag}{alignment.get(a, '')} {header + c + header} " for c, a in zip(cell_values, colaligns)
|
|
198
|
+
]
|
|
199
|
+
return "".join(values_with_attrs) + "||"
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def _latex_line_begin_tabular(colwidths, colaligns, booktabs=False, longtable=False):
|
|
203
|
+
"""Generate LaTeX tabular or longtable environment with specified column widths, alignments, and optional booktabs
|
|
204
|
+
formatting.
|
|
205
|
+
"""
|
|
206
|
+
alignment = {"left": "l", "right": "r", "center": "c", "decimal": "r"}
|
|
207
|
+
tabular_columns_fmt = "".join([alignment.get(a, "l") for a in colaligns])
|
|
208
|
+
return "\n".join(
|
|
209
|
+
[
|
|
210
|
+
("\\begin{longtable}{" if longtable else "\\begin{tabular}{") + tabular_columns_fmt + "}",
|
|
211
|
+
"\\toprule" if booktabs else "\\hline",
|
|
212
|
+
]
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def _asciidoc_row(is_header, *args):
|
|
217
|
+
"""Handle header and data rows for asciidoc format."""
|
|
218
|
+
|
|
219
|
+
def make_header_line(is_header, colwidths, colaligns):
|
|
220
|
+
# generate the column specifiers
|
|
221
|
+
|
|
222
|
+
alignment = {"left": "<", "right": ">", "center": "^", "decimal": ">"}
|
|
223
|
+
# use the column widths generated by tabulate for the asciidoc column width specifiers
|
|
224
|
+
asciidoc_alignments = zip(colwidths, [alignment[colalign] for colalign in colaligns])
|
|
225
|
+
asciidoc_column_specifiers = [f"{width:d}{align}" for width, align in asciidoc_alignments]
|
|
226
|
+
header_list = ['cols="' + (",".join(asciidoc_column_specifiers)) + '"']
|
|
227
|
+
|
|
228
|
+
# generate the list of options (currently only "header")
|
|
229
|
+
options_list = []
|
|
230
|
+
|
|
231
|
+
if is_header:
|
|
232
|
+
options_list.append("header")
|
|
233
|
+
|
|
234
|
+
if options_list:
|
|
235
|
+
header_list += ['options="' + ",".join(options_list) + '"']
|
|
236
|
+
|
|
237
|
+
# generate the list of entries in the table header field
|
|
238
|
+
|
|
239
|
+
return "[{}]\n|====".format(",".join(header_list))
|
|
240
|
+
|
|
241
|
+
if len(args) == 2:
|
|
242
|
+
# two arguments are passed if called in the context of aboveline
|
|
243
|
+
# print the table header with column widths and optional header tag
|
|
244
|
+
return make_header_line(False, *args)
|
|
245
|
+
|
|
246
|
+
elif len(args) == 3:
|
|
247
|
+
# three arguments are passed if called in the context of dataline or headerline
|
|
248
|
+
# print the table line and make the aboveline if it is a header
|
|
249
|
+
|
|
250
|
+
cell_values, colwidths, colaligns = args
|
|
251
|
+
data_line = "|" + "|".join(cell_values)
|
|
252
|
+
|
|
253
|
+
if is_header:
|
|
254
|
+
return make_header_line(True, colwidths, colaligns) + "\n" + data_line
|
|
255
|
+
else:
|
|
256
|
+
return data_line
|
|
257
|
+
|
|
258
|
+
else:
|
|
259
|
+
raise ValueError(
|
|
260
|
+
" _asciidoc_row() requires two (colwidths, colaligns) "
|
|
261
|
+
+ "or three (cell_values, colwidths, colaligns) arguments) "
|
|
262
|
+
)
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
LATEX_ESCAPE_RULES = {
|
|
266
|
+
r"&": r"\&",
|
|
267
|
+
r"%": r"\%",
|
|
268
|
+
r"$": r"\$",
|
|
269
|
+
r"#": r"\#",
|
|
270
|
+
r"_": r"\_",
|
|
271
|
+
r"^": r"\^{}",
|
|
272
|
+
r"{": r"\{",
|
|
273
|
+
r"}": r"\}",
|
|
274
|
+
r"~": r"\textasciitilde{}",
|
|
275
|
+
"\\": r"\textbackslash{}",
|
|
276
|
+
r"<": r"\ensuremath{<}",
|
|
277
|
+
r">": r"\ensuremath{>}",
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
def _latex_row(cell_values, colwidths, colaligns, escrules=LATEX_ESCAPE_RULES):
|
|
282
|
+
"""Generates a LaTeX table row with escaped special characters based on provided cell values, column widths, and
|
|
283
|
+
alignments.
|
|
284
|
+
"""
|
|
285
|
+
|
|
286
|
+
def escape_char(c):
|
|
287
|
+
return escrules.get(c, c)
|
|
288
|
+
|
|
289
|
+
escaped_values = ["".join(map(escape_char, cell)) for cell in cell_values]
|
|
290
|
+
rowfmt = DataRow("", "&", "\\\\")
|
|
291
|
+
return _build_simple_row(escaped_values, rowfmt)
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
def _rst_escape_first_column(rows, headers):
|
|
295
|
+
"""Escapes empty values in the first column of rows and headers for reStructuredText (RST) formatting compliance."""
|
|
296
|
+
|
|
297
|
+
def escape_empty(val):
|
|
298
|
+
return ".." if isinstance(val, (str, bytes)) and not val.strip() else val
|
|
299
|
+
|
|
300
|
+
new_headers = list(headers)
|
|
301
|
+
new_rows = []
|
|
302
|
+
if headers:
|
|
303
|
+
new_headers[0] = escape_empty(headers[0])
|
|
304
|
+
for row in rows:
|
|
305
|
+
new_row = list(row)
|
|
306
|
+
if new_row:
|
|
307
|
+
new_row[0] = escape_empty(row[0])
|
|
308
|
+
new_rows.append(new_row)
|
|
309
|
+
return new_rows, new_headers
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
_table_formats = {
|
|
313
|
+
"simple": TableFormat(
|
|
314
|
+
lineabove=Line("", "-", " ", ""),
|
|
315
|
+
linebelowheader=Line("", "-", " ", ""),
|
|
316
|
+
linebetweenrows=None,
|
|
317
|
+
linebelow=Line("", "-", " ", ""),
|
|
318
|
+
headerrow=DataRow("", " ", ""),
|
|
319
|
+
datarow=DataRow("", " ", ""),
|
|
320
|
+
padding=0,
|
|
321
|
+
with_header_hide=["lineabove", "linebelow"],
|
|
322
|
+
),
|
|
323
|
+
"plain": TableFormat(
|
|
324
|
+
lineabove=None,
|
|
325
|
+
linebelowheader=None,
|
|
326
|
+
linebetweenrows=None,
|
|
327
|
+
linebelow=None,
|
|
328
|
+
headerrow=DataRow("", " ", ""),
|
|
329
|
+
datarow=DataRow("", " ", ""),
|
|
330
|
+
padding=0,
|
|
331
|
+
with_header_hide=None,
|
|
332
|
+
),
|
|
333
|
+
"grid": TableFormat(
|
|
334
|
+
lineabove=Line("+", "-", "+", "+"),
|
|
335
|
+
linebelowheader=Line("+", "=", "+", "+"),
|
|
336
|
+
linebetweenrows=Line("+", "-", "+", "+"),
|
|
337
|
+
linebelow=Line("+", "-", "+", "+"),
|
|
338
|
+
headerrow=DataRow("|", "|", "|"),
|
|
339
|
+
datarow=DataRow("|", "|", "|"),
|
|
340
|
+
padding=1,
|
|
341
|
+
with_header_hide=None,
|
|
342
|
+
),
|
|
343
|
+
"simple_grid": TableFormat(
|
|
344
|
+
lineabove=Line("┌", "─", "┬", "┐"),
|
|
345
|
+
linebelowheader=Line("├", "─", "┼", "┤"),
|
|
346
|
+
linebetweenrows=Line("├", "─", "┼", "┤"),
|
|
347
|
+
linebelow=Line("└", "─", "┴", "┘"),
|
|
348
|
+
headerrow=DataRow("│", "│", "│"),
|
|
349
|
+
datarow=DataRow("│", "│", "│"),
|
|
350
|
+
padding=1,
|
|
351
|
+
with_header_hide=None,
|
|
352
|
+
),
|
|
353
|
+
"rounded_grid": TableFormat(
|
|
354
|
+
lineabove=Line("╭", "─", "┬", "╮"),
|
|
355
|
+
linebelowheader=Line("├", "─", "┼", "┤"),
|
|
356
|
+
linebetweenrows=Line("├", "─", "┼", "┤"),
|
|
357
|
+
linebelow=Line("╰", "─", "┴", "╯"),
|
|
358
|
+
headerrow=DataRow("│", "│", "│"),
|
|
359
|
+
datarow=DataRow("│", "│", "│"),
|
|
360
|
+
padding=1,
|
|
361
|
+
with_header_hide=None,
|
|
362
|
+
),
|
|
363
|
+
"heavy_grid": TableFormat(
|
|
364
|
+
lineabove=Line("┏", "━", "┳", "┓"),
|
|
365
|
+
linebelowheader=Line("┣", "━", "╋", "┫"),
|
|
366
|
+
linebetweenrows=Line("┣", "━", "╋", "┫"),
|
|
367
|
+
linebelow=Line("┗", "━", "┻", "┛"),
|
|
368
|
+
headerrow=DataRow("┃", "┃", "┃"),
|
|
369
|
+
datarow=DataRow("┃", "┃", "┃"),
|
|
370
|
+
padding=1,
|
|
371
|
+
with_header_hide=None,
|
|
372
|
+
),
|
|
373
|
+
"mixed_grid": TableFormat(
|
|
374
|
+
lineabove=Line("┍", "━", "┯", "┑"),
|
|
375
|
+
linebelowheader=Line("┝", "━", "┿", "┥"),
|
|
376
|
+
linebetweenrows=Line("├", "─", "┼", "┤"),
|
|
377
|
+
linebelow=Line("┕", "━", "┷", "┙"),
|
|
378
|
+
headerrow=DataRow("│", "│", "│"),
|
|
379
|
+
datarow=DataRow("│", "│", "│"),
|
|
380
|
+
padding=1,
|
|
381
|
+
with_header_hide=None,
|
|
382
|
+
),
|
|
383
|
+
"double_grid": TableFormat(
|
|
384
|
+
lineabove=Line("╔", "═", "╦", "╗"),
|
|
385
|
+
linebelowheader=Line("╠", "═", "╬", "╣"),
|
|
386
|
+
linebetweenrows=Line("╠", "═", "╬", "╣"),
|
|
387
|
+
linebelow=Line("╚", "═", "╩", "╝"),
|
|
388
|
+
headerrow=DataRow("║", "║", "║"),
|
|
389
|
+
datarow=DataRow("║", "║", "║"),
|
|
390
|
+
padding=1,
|
|
391
|
+
with_header_hide=None,
|
|
392
|
+
),
|
|
393
|
+
"fancy_grid": TableFormat(
|
|
394
|
+
lineabove=Line("╒", "═", "╤", "╕"),
|
|
395
|
+
linebelowheader=Line("╞", "═", "╪", "╡"),
|
|
396
|
+
linebetweenrows=Line("├", "─", "┼", "┤"),
|
|
397
|
+
linebelow=Line("╘", "═", "╧", "╛"),
|
|
398
|
+
headerrow=DataRow("│", "│", "│"),
|
|
399
|
+
datarow=DataRow("│", "│", "│"),
|
|
400
|
+
padding=1,
|
|
401
|
+
with_header_hide=None,
|
|
402
|
+
),
|
|
403
|
+
"outline": TableFormat(
|
|
404
|
+
lineabove=Line("+", "-", "+", "+"),
|
|
405
|
+
linebelowheader=Line("+", "=", "+", "+"),
|
|
406
|
+
linebetweenrows=None,
|
|
407
|
+
linebelow=Line("+", "-", "+", "+"),
|
|
408
|
+
headerrow=DataRow("|", "|", "|"),
|
|
409
|
+
datarow=DataRow("|", "|", "|"),
|
|
410
|
+
padding=1,
|
|
411
|
+
with_header_hide=None,
|
|
412
|
+
),
|
|
413
|
+
"simple_outline": TableFormat(
|
|
414
|
+
lineabove=Line("┌", "─", "┬", "┐"),
|
|
415
|
+
linebelowheader=Line("├", "─", "┼", "┤"),
|
|
416
|
+
linebetweenrows=None,
|
|
417
|
+
linebelow=Line("└", "─", "┴", "┘"),
|
|
418
|
+
headerrow=DataRow("│", "│", "│"),
|
|
419
|
+
datarow=DataRow("│", "│", "│"),
|
|
420
|
+
padding=1,
|
|
421
|
+
with_header_hide=None,
|
|
422
|
+
),
|
|
423
|
+
"rounded_outline": TableFormat(
|
|
424
|
+
lineabove=Line("╭", "─", "┬", "╮"),
|
|
425
|
+
linebelowheader=Line("├", "─", "┼", "┤"),
|
|
426
|
+
linebetweenrows=None,
|
|
427
|
+
linebelow=Line("╰", "─", "┴", "╯"),
|
|
428
|
+
headerrow=DataRow("│", "│", "│"),
|
|
429
|
+
datarow=DataRow("│", "│", "│"),
|
|
430
|
+
padding=1,
|
|
431
|
+
with_header_hide=None,
|
|
432
|
+
),
|
|
433
|
+
"heavy_outline": TableFormat(
|
|
434
|
+
lineabove=Line("┏", "━", "┳", "┓"),
|
|
435
|
+
linebelowheader=Line("┣", "━", "╋", "┫"),
|
|
436
|
+
linebetweenrows=None,
|
|
437
|
+
linebelow=Line("┗", "━", "┻", "┛"),
|
|
438
|
+
headerrow=DataRow("┃", "┃", "┃"),
|
|
439
|
+
datarow=DataRow("┃", "┃", "┃"),
|
|
440
|
+
padding=1,
|
|
441
|
+
with_header_hide=None,
|
|
442
|
+
),
|
|
443
|
+
"mixed_outline": TableFormat(
|
|
444
|
+
lineabove=Line("┍", "━", "┯", "┑"),
|
|
445
|
+
linebelowheader=Line("┝", "━", "┿", "┥"),
|
|
446
|
+
linebetweenrows=None,
|
|
447
|
+
linebelow=Line("┕", "━", "┷", "┙"),
|
|
448
|
+
headerrow=DataRow("│", "│", "│"),
|
|
449
|
+
datarow=DataRow("│", "│", "│"),
|
|
450
|
+
padding=1,
|
|
451
|
+
with_header_hide=None,
|
|
452
|
+
),
|
|
453
|
+
"double_outline": TableFormat(
|
|
454
|
+
lineabove=Line("╔", "═", "╦", "╗"),
|
|
455
|
+
linebelowheader=Line("╠", "═", "╬", "╣"),
|
|
456
|
+
linebetweenrows=None,
|
|
457
|
+
linebelow=Line("╚", "═", "╩", "╝"),
|
|
458
|
+
headerrow=DataRow("║", "║", "║"),
|
|
459
|
+
datarow=DataRow("║", "║", "║"),
|
|
460
|
+
padding=1,
|
|
461
|
+
with_header_hide=None,
|
|
462
|
+
),
|
|
463
|
+
"fancy_outline": TableFormat(
|
|
464
|
+
lineabove=Line("╒", "═", "╤", "╕"),
|
|
465
|
+
linebelowheader=Line("╞", "═", "╪", "╡"),
|
|
466
|
+
linebetweenrows=None,
|
|
467
|
+
linebelow=Line("╘", "═", "╧", "╛"),
|
|
468
|
+
headerrow=DataRow("│", "│", "│"),
|
|
469
|
+
datarow=DataRow("│", "│", "│"),
|
|
470
|
+
padding=1,
|
|
471
|
+
with_header_hide=None,
|
|
472
|
+
),
|
|
473
|
+
"github": TableFormat(
|
|
474
|
+
lineabove=Line("|", "-", "|", "|"),
|
|
475
|
+
linebelowheader=Line("|", "-", "|", "|"),
|
|
476
|
+
linebetweenrows=None,
|
|
477
|
+
linebelow=None,
|
|
478
|
+
headerrow=DataRow("|", "|", "|"),
|
|
479
|
+
datarow=DataRow("|", "|", "|"),
|
|
480
|
+
padding=1,
|
|
481
|
+
with_header_hide=["lineabove"],
|
|
482
|
+
),
|
|
483
|
+
"pipe": TableFormat(
|
|
484
|
+
lineabove=_pipe_line_with_colons,
|
|
485
|
+
linebelowheader=_pipe_line_with_colons,
|
|
486
|
+
linebetweenrows=None,
|
|
487
|
+
linebelow=None,
|
|
488
|
+
headerrow=DataRow("|", "|", "|"),
|
|
489
|
+
datarow=DataRow("|", "|", "|"),
|
|
490
|
+
padding=1,
|
|
491
|
+
with_header_hide=["lineabove"],
|
|
492
|
+
),
|
|
493
|
+
"orgtbl": TableFormat(
|
|
494
|
+
lineabove=None,
|
|
495
|
+
linebelowheader=Line("|", "-", "+", "|"),
|
|
496
|
+
linebetweenrows=None,
|
|
497
|
+
linebelow=None,
|
|
498
|
+
headerrow=DataRow("|", "|", "|"),
|
|
499
|
+
datarow=DataRow("|", "|", "|"),
|
|
500
|
+
padding=1,
|
|
501
|
+
with_header_hide=None,
|
|
502
|
+
),
|
|
503
|
+
"jira": TableFormat(
|
|
504
|
+
lineabove=None,
|
|
505
|
+
linebelowheader=None,
|
|
506
|
+
linebetweenrows=None,
|
|
507
|
+
linebelow=None,
|
|
508
|
+
headerrow=DataRow("||", "||", "||"),
|
|
509
|
+
datarow=DataRow("|", "|", "|"),
|
|
510
|
+
padding=1,
|
|
511
|
+
with_header_hide=None,
|
|
512
|
+
),
|
|
513
|
+
"presto": TableFormat(
|
|
514
|
+
lineabove=None,
|
|
515
|
+
linebelowheader=Line("", "-", "+", ""),
|
|
516
|
+
linebetweenrows=None,
|
|
517
|
+
linebelow=None,
|
|
518
|
+
headerrow=DataRow("", "|", ""),
|
|
519
|
+
datarow=DataRow("", "|", ""),
|
|
520
|
+
padding=1,
|
|
521
|
+
with_header_hide=None,
|
|
522
|
+
),
|
|
523
|
+
"pretty": TableFormat(
|
|
524
|
+
lineabove=Line("+", "-", "+", "+"),
|
|
525
|
+
linebelowheader=Line("+", "-", "+", "+"),
|
|
526
|
+
linebetweenrows=None,
|
|
527
|
+
linebelow=Line("+", "-", "+", "+"),
|
|
528
|
+
headerrow=DataRow("|", "|", "|"),
|
|
529
|
+
datarow=DataRow("|", "|", "|"),
|
|
530
|
+
padding=1,
|
|
531
|
+
with_header_hide=None,
|
|
532
|
+
),
|
|
533
|
+
"psql": TableFormat(
|
|
534
|
+
lineabove=Line("+", "-", "+", "+"),
|
|
535
|
+
linebelowheader=Line("|", "-", "+", "|"),
|
|
536
|
+
linebetweenrows=None,
|
|
537
|
+
linebelow=Line("+", "-", "+", "+"),
|
|
538
|
+
headerrow=DataRow("|", "|", "|"),
|
|
539
|
+
datarow=DataRow("|", "|", "|"),
|
|
540
|
+
padding=1,
|
|
541
|
+
with_header_hide=None,
|
|
542
|
+
),
|
|
543
|
+
"rst": TableFormat(
|
|
544
|
+
lineabove=Line("", "=", " ", ""),
|
|
545
|
+
linebelowheader=Line("", "=", " ", ""),
|
|
546
|
+
linebetweenrows=None,
|
|
547
|
+
linebelow=Line("", "=", " ", ""),
|
|
548
|
+
headerrow=DataRow("", " ", ""),
|
|
549
|
+
datarow=DataRow("", " ", ""),
|
|
550
|
+
padding=0,
|
|
551
|
+
with_header_hide=None,
|
|
552
|
+
),
|
|
553
|
+
"mediawiki": TableFormat(
|
|
554
|
+
lineabove=Line(
|
|
555
|
+
'{| class="wikitable" style="text-align: left;"',
|
|
556
|
+
"",
|
|
557
|
+
"",
|
|
558
|
+
"\n|+ <!-- caption -->\n|-",
|
|
559
|
+
),
|
|
560
|
+
linebelowheader=Line("|-", "", "", ""),
|
|
561
|
+
linebetweenrows=Line("|-", "", "", ""),
|
|
562
|
+
linebelow=Line("|}", "", "", ""),
|
|
563
|
+
headerrow=partial(_mediawiki_row_with_attrs, "!"),
|
|
564
|
+
datarow=partial(_mediawiki_row_with_attrs, "|"),
|
|
565
|
+
padding=0,
|
|
566
|
+
with_header_hide=None,
|
|
567
|
+
),
|
|
568
|
+
"moinmoin": TableFormat(
|
|
569
|
+
lineabove=None,
|
|
570
|
+
linebelowheader=None,
|
|
571
|
+
linebetweenrows=None,
|
|
572
|
+
linebelow=None,
|
|
573
|
+
headerrow=partial(_moin_row_with_attrs, "||", header="'''"),
|
|
574
|
+
datarow=partial(_moin_row_with_attrs, "||"),
|
|
575
|
+
padding=1,
|
|
576
|
+
with_header_hide=None,
|
|
577
|
+
),
|
|
578
|
+
"youtrack": TableFormat(
|
|
579
|
+
lineabove=None,
|
|
580
|
+
linebelowheader=None,
|
|
581
|
+
linebetweenrows=None,
|
|
582
|
+
linebelow=None,
|
|
583
|
+
headerrow=DataRow("|| ", " || ", " || "),
|
|
584
|
+
datarow=DataRow("| ", " | ", " |"),
|
|
585
|
+
padding=1,
|
|
586
|
+
with_header_hide=None,
|
|
587
|
+
),
|
|
588
|
+
"html": TableFormat(
|
|
589
|
+
lineabove=_html_begin_table_without_header,
|
|
590
|
+
linebelowheader="",
|
|
591
|
+
linebetweenrows=None,
|
|
592
|
+
linebelow=Line("</tbody>\n</table>", "", "", ""),
|
|
593
|
+
headerrow=partial(_html_row_with_attrs, "th", False),
|
|
594
|
+
datarow=partial(_html_row_with_attrs, "td", False),
|
|
595
|
+
padding=0,
|
|
596
|
+
with_header_hide=["lineabove"],
|
|
597
|
+
),
|
|
598
|
+
"unsafehtml": TableFormat(
|
|
599
|
+
lineabove=_html_begin_table_without_header,
|
|
600
|
+
linebelowheader="",
|
|
601
|
+
linebetweenrows=None,
|
|
602
|
+
linebelow=Line("</tbody>\n</table>", "", "", ""),
|
|
603
|
+
headerrow=partial(_html_row_with_attrs, "th", True),
|
|
604
|
+
datarow=partial(_html_row_with_attrs, "td", True),
|
|
605
|
+
padding=0,
|
|
606
|
+
with_header_hide=["lineabove"],
|
|
607
|
+
),
|
|
608
|
+
"latex": TableFormat(
|
|
609
|
+
lineabove=_latex_line_begin_tabular,
|
|
610
|
+
linebelowheader=Line("\\hline", "", "", ""),
|
|
611
|
+
linebetweenrows=None,
|
|
612
|
+
linebelow=Line("\\hline\n\\end{tabular}", "", "", ""),
|
|
613
|
+
headerrow=_latex_row,
|
|
614
|
+
datarow=_latex_row,
|
|
615
|
+
padding=1,
|
|
616
|
+
with_header_hide=None,
|
|
617
|
+
),
|
|
618
|
+
"latex_raw": TableFormat(
|
|
619
|
+
lineabove=_latex_line_begin_tabular,
|
|
620
|
+
linebelowheader=Line("\\hline", "", "", ""),
|
|
621
|
+
linebetweenrows=None,
|
|
622
|
+
linebelow=Line("\\hline\n\\end{tabular}", "", "", ""),
|
|
623
|
+
headerrow=partial(_latex_row, escrules={}),
|
|
624
|
+
datarow=partial(_latex_row, escrules={}),
|
|
625
|
+
padding=1,
|
|
626
|
+
with_header_hide=None,
|
|
627
|
+
),
|
|
628
|
+
"latex_booktabs": TableFormat(
|
|
629
|
+
lineabove=partial(_latex_line_begin_tabular, booktabs=True),
|
|
630
|
+
linebelowheader=Line("\\midrule", "", "", ""),
|
|
631
|
+
linebetweenrows=None,
|
|
632
|
+
linebelow=Line("\\bottomrule\n\\end{tabular}", "", "", ""),
|
|
633
|
+
headerrow=_latex_row,
|
|
634
|
+
datarow=_latex_row,
|
|
635
|
+
padding=1,
|
|
636
|
+
with_header_hide=None,
|
|
637
|
+
),
|
|
638
|
+
"latex_longtable": TableFormat(
|
|
639
|
+
lineabove=partial(_latex_line_begin_tabular, longtable=True),
|
|
640
|
+
linebelowheader=Line("\\hline\n\\endhead", "", "", ""),
|
|
641
|
+
linebetweenrows=None,
|
|
642
|
+
linebelow=Line("\\hline\n\\end{longtable}", "", "", ""),
|
|
643
|
+
headerrow=_latex_row,
|
|
644
|
+
datarow=_latex_row,
|
|
645
|
+
padding=1,
|
|
646
|
+
with_header_hide=None,
|
|
647
|
+
),
|
|
648
|
+
"tsv": TableFormat(
|
|
649
|
+
lineabove=None,
|
|
650
|
+
linebelowheader=None,
|
|
651
|
+
linebetweenrows=None,
|
|
652
|
+
linebelow=None,
|
|
653
|
+
headerrow=DataRow("", "\t", ""),
|
|
654
|
+
datarow=DataRow("", "\t", ""),
|
|
655
|
+
padding=0,
|
|
656
|
+
with_header_hide=None,
|
|
657
|
+
),
|
|
658
|
+
"textile": TableFormat(
|
|
659
|
+
lineabove=None,
|
|
660
|
+
linebelowheader=None,
|
|
661
|
+
linebetweenrows=None,
|
|
662
|
+
linebelow=None,
|
|
663
|
+
headerrow=DataRow("|_. ", "|_.", "|"),
|
|
664
|
+
datarow=_textile_row_with_attrs,
|
|
665
|
+
padding=1,
|
|
666
|
+
with_header_hide=None,
|
|
667
|
+
),
|
|
668
|
+
"asciidoc": TableFormat(
|
|
669
|
+
lineabove=partial(_asciidoc_row, False),
|
|
670
|
+
linebelowheader=None,
|
|
671
|
+
linebetweenrows=None,
|
|
672
|
+
linebelow=Line("|====", "", "", ""),
|
|
673
|
+
headerrow=partial(_asciidoc_row, True),
|
|
674
|
+
datarow=partial(_asciidoc_row, False),
|
|
675
|
+
padding=1,
|
|
676
|
+
with_header_hide=["lineabove"],
|
|
677
|
+
),
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
tabulate_formats = list(sorted(_table_formats.keys()))
|
|
682
|
+
|
|
683
|
+
# The table formats for which multiline cells will be folded into subsequent
|
|
684
|
+
# table rows. The key is the original format specified at the API. The value is
|
|
685
|
+
# the format that will be used to represent the original format.
|
|
686
|
+
multiline_formats = {
|
|
687
|
+
"plain": "plain",
|
|
688
|
+
"simple": "simple",
|
|
689
|
+
"grid": "grid",
|
|
690
|
+
"simple_grid": "simple_grid",
|
|
691
|
+
"rounded_grid": "rounded_grid",
|
|
692
|
+
"heavy_grid": "heavy_grid",
|
|
693
|
+
"mixed_grid": "mixed_grid",
|
|
694
|
+
"double_grid": "double_grid",
|
|
695
|
+
"fancy_grid": "fancy_grid",
|
|
696
|
+
"pipe": "pipe",
|
|
697
|
+
"orgtbl": "orgtbl",
|
|
698
|
+
"jira": "jira",
|
|
699
|
+
"presto": "presto",
|
|
700
|
+
"pretty": "pretty",
|
|
701
|
+
"psql": "psql",
|
|
702
|
+
"rst": "rst",
|
|
703
|
+
"outline": "outline",
|
|
704
|
+
"simple_outline": "simple_outline",
|
|
705
|
+
"rounded_outline": "rounded_outline",
|
|
706
|
+
"heavy_outline": "heavy_outline",
|
|
707
|
+
"mixed_outline": "mixed_outline",
|
|
708
|
+
"double_outline": "double_outline",
|
|
709
|
+
"fancy_outline": "fancy_outline",
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
# TODO: Add multiline support for the remaining table formats:
|
|
713
|
+
# - mediawiki: Replace \n with <br>
|
|
714
|
+
# - moinmoin: TBD
|
|
715
|
+
# - youtrack: TBD
|
|
716
|
+
# - html: Replace \n with <br>
|
|
717
|
+
# - latex*: Use "makecell" package: In header, replace X\nY with
|
|
718
|
+
# \thead{X\\Y} and in data row, replace X\nY with \makecell{X\\Y}
|
|
719
|
+
# - tsv: TBD
|
|
720
|
+
# - textile: Replace \n with <br/> (must be well-formed XML)
|
|
721
|
+
|
|
722
|
+
_multiline_codes = re.compile(r"\r|\n|\r\n")
|
|
723
|
+
_multiline_codes_bytes = re.compile(b"\r|\n|\r\n")
|
|
724
|
+
|
|
725
|
+
# Handle ANSI escape sequences for both control sequence introducer (CSI) and
|
|
726
|
+
# operating system command (OSC). Both of these begin with 0x1b (or octal 033),
|
|
727
|
+
# which will be shown below as ESC.
|
|
728
|
+
#
|
|
729
|
+
# CSI ANSI escape codes have the following format, defined in section 5.4 of ECMA-48:
|
|
730
|
+
#
|
|
731
|
+
# CSI: ESC followed by the '[' character (0x5b)
|
|
732
|
+
# Parameter Bytes: 0..n bytes in the range 0x30-0x3f
|
|
733
|
+
# Intermediate Bytes: 0..n bytes in the range 0x20-0x2f
|
|
734
|
+
# Final Byte: a single byte in the range 0x40-0x7e
|
|
735
|
+
#
|
|
736
|
+
# Also include the terminal hyperlink sequences as described here:
|
|
737
|
+
# https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda
|
|
738
|
+
#
|
|
739
|
+
# OSC 8 ; params ; uri ST display_text OSC 8 ;; ST
|
|
740
|
+
#
|
|
741
|
+
# Example: \x1b]8;;https://example.com\x5ctext to show\x1b]8;;\x5c
|
|
742
|
+
#
|
|
743
|
+
# Where:
|
|
744
|
+
# OSC: ESC followed by the ']' character (0x5d)
|
|
745
|
+
# params: 0..n optional key value pairs separated by ':' (e.g. foo=bar:baz=qux:abc=123)
|
|
746
|
+
# URI: the actual URI with protocol scheme (e.g. https://, file://, ftp://)
|
|
747
|
+
# ST: ESC followed by the '\' character (0x5c)
|
|
748
|
+
_esc = r"\x1b"
|
|
749
|
+
_csi = rf"{_esc}\["
|
|
750
|
+
_osc = rf"{_esc}\]"
|
|
751
|
+
_st = rf"{_esc}\\"
|
|
752
|
+
|
|
753
|
+
_ansi_escape_pat = rf"""
|
|
754
|
+
(
|
|
755
|
+
# terminal colors, etc
|
|
756
|
+
{_csi} # CSI
|
|
757
|
+
[\x30-\x3f]* # parameter bytes
|
|
758
|
+
[\x20-\x2f]* # intermediate bytes
|
|
759
|
+
[\x40-\x7e] # final byte
|
|
760
|
+
|
|
|
761
|
+
# terminal hyperlinks
|
|
762
|
+
{_osc}8; # OSC opening
|
|
763
|
+
(\w+=\w+:?)* # key=value params list (submatch 2)
|
|
764
|
+
; # delimiter
|
|
765
|
+
([^{_esc}]+) # URI - anything but ESC (submatch 3)
|
|
766
|
+
{_st} # ST
|
|
767
|
+
([^{_esc}]+) # link text - anything but ESC (submatch 4)
|
|
768
|
+
{_osc}8;;{_st} # "closing" OSC sequence
|
|
769
|
+
)
|
|
770
|
+
"""
|
|
771
|
+
_ansi_codes = re.compile(_ansi_escape_pat, re.VERBOSE)
|
|
772
|
+
_ansi_codes_bytes = re.compile(_ansi_escape_pat.encode("utf8"), re.VERBOSE)
|
|
773
|
+
_ansi_color_reset_code = "\033[0m"
|
|
774
|
+
|
|
775
|
+
_float_with_thousands_separators = re.compile(r"^(([+-]?[0-9]{1,3})(?:,([0-9]{3}))*)?(?(1)\.[0-9]*|\.[0-9]+)?$")
|
|
776
|
+
|
|
777
|
+
|
|
778
|
+
def simple_separated_format(separator):
|
|
779
|
+
r"""
|
|
780
|
+
Construct a simple TableFormat with columns separated by a separator.
|
|
781
|
+
|
|
782
|
+
>>> tsv = simple_separated_format("\\t") ; \
|
|
783
|
+
tabulate([["foo", 1], ["spam", 23]], tablefmt=tsv) == 'foo \\t 1\\nspam\\t23'
|
|
784
|
+
True
|
|
785
|
+
"""
|
|
786
|
+
return TableFormat(
|
|
787
|
+
None,
|
|
788
|
+
None,
|
|
789
|
+
None,
|
|
790
|
+
None,
|
|
791
|
+
headerrow=DataRow("", separator, ""),
|
|
792
|
+
datarow=DataRow("", separator, ""),
|
|
793
|
+
padding=0,
|
|
794
|
+
with_header_hide=None,
|
|
795
|
+
)
|
|
796
|
+
|
|
797
|
+
|
|
798
|
+
def _isnumber_with_thousands_separator(string):
|
|
799
|
+
"""
|
|
800
|
+
>>> _isnumber_with_thousands_separator(".")
|
|
801
|
+
False
|
|
802
|
+
>>> _isnumber_with_thousands_separator("1")
|
|
803
|
+
True
|
|
804
|
+
>>> _isnumber_with_thousands_separator("1.")
|
|
805
|
+
True
|
|
806
|
+
>>> _isnumber_with_thousands_separator(".1")
|
|
807
|
+
True
|
|
808
|
+
>>> _isnumber_with_thousands_separator("1000")
|
|
809
|
+
False
|
|
810
|
+
>>> _isnumber_with_thousands_separator("1,000")
|
|
811
|
+
True
|
|
812
|
+
>>> _isnumber_with_thousands_separator("1,0000")
|
|
813
|
+
False
|
|
814
|
+
>>> _isnumber_with_thousands_separator("1,000.1234")
|
|
815
|
+
True
|
|
816
|
+
>>> _isnumber_with_thousands_separator(b"1,000.1234")
|
|
817
|
+
True
|
|
818
|
+
>>> _isnumber_with_thousands_separator("+1,000.1234")
|
|
819
|
+
True
|
|
820
|
+
>>> _isnumber_with_thousands_separator("-1,000.1234")
|
|
821
|
+
True.
|
|
822
|
+
"""
|
|
823
|
+
try:
|
|
824
|
+
string = string.decode()
|
|
825
|
+
except (UnicodeDecodeError, AttributeError):
|
|
826
|
+
pass
|
|
827
|
+
|
|
828
|
+
return bool(re.match(_float_with_thousands_separators, string))
|
|
829
|
+
|
|
830
|
+
|
|
831
|
+
def _isconvertible(conv, string):
|
|
832
|
+
"""Check if a string can be converted to a specified type without raising a ValueError or TypeError."""
|
|
833
|
+
try:
|
|
834
|
+
conv(string)
|
|
835
|
+
return True
|
|
836
|
+
except (ValueError, TypeError):
|
|
837
|
+
return False
|
|
838
|
+
|
|
839
|
+
|
|
840
|
+
def _isnumber(string):
|
|
841
|
+
"""
|
|
842
|
+
>>> _isnumber("123.45")
|
|
843
|
+
True
|
|
844
|
+
>>> _isnumber("123")
|
|
845
|
+
True
|
|
846
|
+
>>> _isnumber("spam")
|
|
847
|
+
False
|
|
848
|
+
>>> _isnumber("123e45678")
|
|
849
|
+
False
|
|
850
|
+
>>> _isnumber("inf")
|
|
851
|
+
True.
|
|
852
|
+
"""
|
|
853
|
+
if not _isconvertible(float, string):
|
|
854
|
+
return False
|
|
855
|
+
elif isinstance(string, (str, bytes)) and (math.isinf(float(string)) or math.isnan(float(string))):
|
|
856
|
+
return string.lower() in {"inf", "-inf", "nan"}
|
|
857
|
+
return True
|
|
858
|
+
|
|
859
|
+
|
|
860
|
+
def _isint(string, inttype=int):
|
|
861
|
+
"""
|
|
862
|
+
>>> _isint("123")
|
|
863
|
+
True
|
|
864
|
+
>>> _isint("123.45")
|
|
865
|
+
False.
|
|
866
|
+
"""
|
|
867
|
+
return (
|
|
868
|
+
type(string) is inttype
|
|
869
|
+
or (
|
|
870
|
+
(hasattr(string, "is_integer") or hasattr(string, "__array__"))
|
|
871
|
+
and str(type(string)).startswith("<class 'numpy.int")
|
|
872
|
+
) # numpy.int64 and similar
|
|
873
|
+
or (isinstance(string, (bytes, str)) and _isconvertible(inttype, string)) # integer as string
|
|
874
|
+
)
|
|
875
|
+
|
|
876
|
+
|
|
877
|
+
def _isbool(string):
|
|
878
|
+
"""
|
|
879
|
+
>>> _isbool(True)
|
|
880
|
+
True
|
|
881
|
+
>>> _isbool("False")
|
|
882
|
+
True
|
|
883
|
+
>>> _isbool(1)
|
|
884
|
+
False.
|
|
885
|
+
"""
|
|
886
|
+
return type(string) is bool or (isinstance(string, (bytes, str)) and string in {"True", "False"})
|
|
887
|
+
|
|
888
|
+
|
|
889
|
+
def _type(string, has_invisible=True, numparse=True):
|
|
890
|
+
r"""
|
|
891
|
+
The least generic type (type(None), int, float, str, unicode).
|
|
892
|
+
|
|
893
|
+
>>> _type(None) is type(None)
|
|
894
|
+
True
|
|
895
|
+
>>> _type("foo") is type("")
|
|
896
|
+
True
|
|
897
|
+
>>> _type("1") is type(1)
|
|
898
|
+
True
|
|
899
|
+
>>> _type('\x1b[31m42\x1b[0m') is type(42)
|
|
900
|
+
True
|
|
901
|
+
>>> _type('\x1b[31m42\x1b[0m') is type(42)
|
|
902
|
+
True
|
|
903
|
+
"""
|
|
904
|
+
if has_invisible and isinstance(string, (str, bytes)):
|
|
905
|
+
string = _strip_ansi(string)
|
|
906
|
+
|
|
907
|
+
if string is None:
|
|
908
|
+
return type(None)
|
|
909
|
+
elif hasattr(string, "isoformat"): # datetime.datetime, date, and time
|
|
910
|
+
return str
|
|
911
|
+
elif _isbool(string):
|
|
912
|
+
return bool
|
|
913
|
+
elif _isint(string) and numparse:
|
|
914
|
+
return int
|
|
915
|
+
elif _isnumber(string) and numparse:
|
|
916
|
+
return float
|
|
917
|
+
elif isinstance(string, bytes):
|
|
918
|
+
return bytes
|
|
919
|
+
else:
|
|
920
|
+
return str
|
|
921
|
+
|
|
922
|
+
|
|
923
|
+
def _afterpoint(string):
|
|
924
|
+
"""
|
|
925
|
+
Symbols after a decimal point, -1 if the string lacks the decimal point.
|
|
926
|
+
|
|
927
|
+
>>> _afterpoint("123.45")
|
|
928
|
+
2
|
|
929
|
+
>>> _afterpoint("1001")
|
|
930
|
+
-1
|
|
931
|
+
>>> _afterpoint("eggs")
|
|
932
|
+
-1
|
|
933
|
+
>>> _afterpoint("123e45")
|
|
934
|
+
2
|
|
935
|
+
>>> _afterpoint("123,456.78")
|
|
936
|
+
2
|
|
937
|
+
"""
|
|
938
|
+
if not _isnumber(string) and not _isnumber_with_thousands_separator(string):
|
|
939
|
+
return -1 # not a number
|
|
940
|
+
if _isint(string):
|
|
941
|
+
return -1
|
|
942
|
+
pos = string.rfind(".")
|
|
943
|
+
pos = string.lower().rfind("e") if pos < 0 else pos
|
|
944
|
+
return len(string) - pos - 1 if pos >= 0 else -1
|
|
945
|
+
|
|
946
|
+
|
|
947
|
+
def _padleft(width, s):
|
|
948
|
+
"""
|
|
949
|
+
Flush right.
|
|
950
|
+
|
|
951
|
+
>>> _padleft(6, '\u044f\u0439\u0446\u0430') == ' \u044f\u0439\u0446\u0430'
|
|
952
|
+
True
|
|
953
|
+
"""
|
|
954
|
+
fmt = "{0:>%ds}" % width
|
|
955
|
+
return fmt.format(s)
|
|
956
|
+
|
|
957
|
+
|
|
958
|
+
def _padright(width, s):
|
|
959
|
+
"""
|
|
960
|
+
Flush left.
|
|
961
|
+
|
|
962
|
+
>>> _padright(6, '\u044f\u0439\u0446\u0430') == '\u044f\u0439\u0446\u0430 '
|
|
963
|
+
True
|
|
964
|
+
"""
|
|
965
|
+
fmt = "{0:<%ds}" % width
|
|
966
|
+
return fmt.format(s)
|
|
967
|
+
|
|
968
|
+
|
|
969
|
+
def _padboth(width, s):
|
|
970
|
+
"""
|
|
971
|
+
Center string.
|
|
972
|
+
|
|
973
|
+
>>> _padboth(6, '\u044f\u0439\u0446\u0430') == ' \u044f\u0439\u0446\u0430 '
|
|
974
|
+
True
|
|
975
|
+
"""
|
|
976
|
+
fmt = "{0:^%ds}" % width
|
|
977
|
+
return fmt.format(s)
|
|
978
|
+
|
|
979
|
+
|
|
980
|
+
def _padnone(ignore_width, s):
|
|
981
|
+
"""Returns the input string without padding."""
|
|
982
|
+
return s
|
|
983
|
+
|
|
984
|
+
|
|
985
|
+
def _strip_ansi(s):
|
|
986
|
+
r"""
|
|
987
|
+
Remove ANSI escape sequences, both CSI (color codes, etc) and OSC hyperlinks.
|
|
988
|
+
|
|
989
|
+
CSI sequences are simply removed from the output, while OSC hyperlinks are replaced
|
|
990
|
+
with the link text. Note: it may be desirable to show the URI instead but this is not
|
|
991
|
+
supported.
|
|
992
|
+
|
|
993
|
+
>>> repr(_strip_ansi('\x1B]8;;https://example.com\x1B\\This is a link\x1B]8;;\x1B\\'))
|
|
994
|
+
"'This is a link'"
|
|
995
|
+
|
|
996
|
+
>>> repr(_strip_ansi('\x1b[31mred\x1b[0m text'))
|
|
997
|
+
"'red text'"
|
|
998
|
+
"""
|
|
999
|
+
if isinstance(s, str):
|
|
1000
|
+
return _ansi_codes.sub(r"\4", s)
|
|
1001
|
+
else: # a bytestring
|
|
1002
|
+
return _ansi_codes_bytes.sub(r"\4", s)
|
|
1003
|
+
|
|
1004
|
+
|
|
1005
|
+
def _visible_width(s):
|
|
1006
|
+
r"""
|
|
1007
|
+
Visible width of a printed string. ANSI color codes are removed.
|
|
1008
|
+
|
|
1009
|
+
>>> _visible_width('\x1b[31mhello\x1b[0m'), _visible_width("world")
|
|
1010
|
+
(5, 5)
|
|
1011
|
+
"""
|
|
1012
|
+
# optional wide-character support
|
|
1013
|
+
len_fn = wcwidth.wcswidth if wcwidth is not None and WIDE_CHARS_MODE else len
|
|
1014
|
+
if isinstance(s, (str, bytes)):
|
|
1015
|
+
return len_fn(_strip_ansi(s))
|
|
1016
|
+
else:
|
|
1017
|
+
return len_fn(str(s))
|
|
1018
|
+
|
|
1019
|
+
|
|
1020
|
+
def _is_multiline(s):
|
|
1021
|
+
"""Check if the input string or bytestring contains multiline ANSI codes."""
|
|
1022
|
+
if isinstance(s, str):
|
|
1023
|
+
return bool(re.search(_multiline_codes, s))
|
|
1024
|
+
else: # a bytestring
|
|
1025
|
+
return bool(re.search(_multiline_codes_bytes, s))
|
|
1026
|
+
|
|
1027
|
+
|
|
1028
|
+
def _multiline_width(multiline_s, line_width_fn=len):
|
|
1029
|
+
"""Visible width of a potentially multiline content."""
|
|
1030
|
+
return max(map(line_width_fn, re.split("[\r\n]", multiline_s)))
|
|
1031
|
+
|
|
1032
|
+
|
|
1033
|
+
def _choose_width_fn(has_invisible, enable_widechars, is_multiline):
|
|
1034
|
+
"""Return a function to calculate visible cell width."""
|
|
1035
|
+
if has_invisible:
|
|
1036
|
+
line_width_fn = _visible_width
|
|
1037
|
+
elif enable_widechars: # optional wide-character support if available
|
|
1038
|
+
line_width_fn = wcwidth.wcswidth
|
|
1039
|
+
else:
|
|
1040
|
+
line_width_fn = len
|
|
1041
|
+
return (lambda s: _multiline_width(s, line_width_fn)) if is_multiline else line_width_fn
|
|
1042
|
+
|
|
1043
|
+
|
|
1044
|
+
def _align_column_choose_padfn(strings, alignment, has_invisible):
|
|
1045
|
+
"""Selects the appropriate padding function based on alignment and visibility of invisible characters for given
|
|
1046
|
+
strings.
|
|
1047
|
+
"""
|
|
1048
|
+
if alignment == "right":
|
|
1049
|
+
if not PRESERVE_WHITESPACE:
|
|
1050
|
+
strings = [s.strip() for s in strings]
|
|
1051
|
+
padfn = _padleft
|
|
1052
|
+
elif alignment == "center":
|
|
1053
|
+
if not PRESERVE_WHITESPACE:
|
|
1054
|
+
strings = [s.strip() for s in strings]
|
|
1055
|
+
padfn = _padboth
|
|
1056
|
+
elif alignment == "decimal":
|
|
1057
|
+
if has_invisible:
|
|
1058
|
+
decimals = [_afterpoint(_strip_ansi(s)) for s in strings]
|
|
1059
|
+
else:
|
|
1060
|
+
decimals = [_afterpoint(s) for s in strings]
|
|
1061
|
+
maxdecimals = max(decimals)
|
|
1062
|
+
strings = [s + (maxdecimals - decs) * " " for s, decs in zip(strings, decimals)]
|
|
1063
|
+
padfn = _padleft
|
|
1064
|
+
elif not alignment:
|
|
1065
|
+
padfn = _padnone
|
|
1066
|
+
else:
|
|
1067
|
+
if not PRESERVE_WHITESPACE:
|
|
1068
|
+
strings = [s.strip() for s in strings]
|
|
1069
|
+
padfn = _padright
|
|
1070
|
+
return strings, padfn
|
|
1071
|
+
|
|
1072
|
+
|
|
1073
|
+
def _align_column_choose_width_fn(has_invisible, enable_widechars, is_multiline):
|
|
1074
|
+
"""Choose the appropriate width function for aligning text columns based on visibility, wide characters support, and
|
|
1075
|
+
multiline status.
|
|
1076
|
+
"""
|
|
1077
|
+
if has_invisible:
|
|
1078
|
+
line_width_fn = _visible_width
|
|
1079
|
+
elif enable_widechars: # optional wide-character support if available
|
|
1080
|
+
line_width_fn = wcwidth.wcswidth
|
|
1081
|
+
else:
|
|
1082
|
+
line_width_fn = len
|
|
1083
|
+
return (lambda s: _align_column_multiline_width(s, line_width_fn)) if is_multiline else line_width_fn
|
|
1084
|
+
|
|
1085
|
+
|
|
1086
|
+
def _align_column_multiline_width(multiline_s, line_width_fn=len):
|
|
1087
|
+
"""Visible width of a potentially multiline content."""
|
|
1088
|
+
return list(map(line_width_fn, re.split("[\r\n]", multiline_s)))
|
|
1089
|
+
|
|
1090
|
+
|
|
1091
|
+
def _flat_list(nested_list):
|
|
1092
|
+
"""Flatten a nested list into a single list containing all the elements."""
|
|
1093
|
+
ret = []
|
|
1094
|
+
for item in nested_list:
|
|
1095
|
+
if isinstance(item, list):
|
|
1096
|
+
ret.extend(iter(item))
|
|
1097
|
+
else:
|
|
1098
|
+
ret.append(item)
|
|
1099
|
+
return ret
|
|
1100
|
+
|
|
1101
|
+
|
|
1102
|
+
def _align_column(
|
|
1103
|
+
strings,
|
|
1104
|
+
alignment,
|
|
1105
|
+
minwidth=0,
|
|
1106
|
+
has_invisible=True,
|
|
1107
|
+
enable_widechars=False,
|
|
1108
|
+
is_multiline=False,
|
|
1109
|
+
):
|
|
1110
|
+
"""[string] -> [padded_string]."""
|
|
1111
|
+
strings, padfn = _align_column_choose_padfn(strings, alignment, has_invisible)
|
|
1112
|
+
width_fn = _align_column_choose_width_fn(has_invisible, enable_widechars, is_multiline)
|
|
1113
|
+
|
|
1114
|
+
s_widths = list(map(width_fn, strings))
|
|
1115
|
+
maxwidth = max(max(_flat_list(s_widths)), minwidth)
|
|
1116
|
+
# TODO: refactor column alignment in single-line and multiline modes
|
|
1117
|
+
if is_multiline:
|
|
1118
|
+
if enable_widechars or has_invisible:
|
|
1119
|
+
# enable wide-character width corrections
|
|
1120
|
+
s_lens = [[len(s) for s in re.split("[\r\n]", ms)] for ms in strings]
|
|
1121
|
+
visible_widths = [[maxwidth - (w - l) for w, l in zip(mw, ml)] for mw, ml in zip(s_widths, s_lens)]
|
|
1122
|
+
# wcswidth and _visible_width don't count invisible characters;
|
|
1123
|
+
# padfn doesn't need to apply another correction
|
|
1124
|
+
padded_strings = [
|
|
1125
|
+
"\n".join([padfn(w, s) for s, w in zip((ms.splitlines() or ms), mw)])
|
|
1126
|
+
for ms, mw in zip(strings, visible_widths)
|
|
1127
|
+
]
|
|
1128
|
+
else:
|
|
1129
|
+
padded_strings = ["\n".join([padfn(maxwidth, s) for s in ms.splitlines()]) for ms in strings]
|
|
1130
|
+
elif not enable_widechars and not has_invisible:
|
|
1131
|
+
padded_strings = [padfn(maxwidth, s) for s in strings]
|
|
1132
|
+
else:
|
|
1133
|
+
# enable wide-character width corrections
|
|
1134
|
+
s_lens = list(map(len, strings))
|
|
1135
|
+
visible_widths = [maxwidth - (w - l) for w, l in zip(s_widths, s_lens)]
|
|
1136
|
+
# wcswidth and _visible_width don't count invisible characters;
|
|
1137
|
+
# padfn doesn't need to apply another correction
|
|
1138
|
+
padded_strings = [padfn(w, s) for s, w in zip(strings, visible_widths)]
|
|
1139
|
+
return padded_strings
|
|
1140
|
+
|
|
1141
|
+
|
|
1142
|
+
def _more_generic(type1, type2):
|
|
1143
|
+
"""Return the more generic type between type1 and type2 based on predefined type hierarchy."""
|
|
1144
|
+
types = {
|
|
1145
|
+
type(None): 0,
|
|
1146
|
+
bool: 1,
|
|
1147
|
+
int: 2,
|
|
1148
|
+
float: 3,
|
|
1149
|
+
bytes: 4,
|
|
1150
|
+
str: 5,
|
|
1151
|
+
}
|
|
1152
|
+
invtypes = {
|
|
1153
|
+
5: str,
|
|
1154
|
+
4: bytes,
|
|
1155
|
+
3: float,
|
|
1156
|
+
2: int,
|
|
1157
|
+
1: bool,
|
|
1158
|
+
0: type(None),
|
|
1159
|
+
}
|
|
1160
|
+
moregeneric = max(types.get(type1, 5), types.get(type2, 5))
|
|
1161
|
+
return invtypes[moregeneric]
|
|
1162
|
+
|
|
1163
|
+
|
|
1164
|
+
def _column_type(strings, has_invisible=True, numparse=True):
|
|
1165
|
+
"""
|
|
1166
|
+
The least generic type all column values are convertible to.
|
|
1167
|
+
|
|
1168
|
+
>>> _column_type([True, False]) is bool
|
|
1169
|
+
True
|
|
1170
|
+
>>> _column_type(["1", "2"]) is int
|
|
1171
|
+
True
|
|
1172
|
+
>>> _column_type(["1", "2.3"]) is float
|
|
1173
|
+
True
|
|
1174
|
+
>>> _column_type(["1", "2.3", "four"]) is str
|
|
1175
|
+
True
|
|
1176
|
+
>>> _column_type(["four", '\u043f\u044f\u0442\u044c']) is str
|
|
1177
|
+
True
|
|
1178
|
+
>>> _column_type([None, "brux"]) is str
|
|
1179
|
+
True
|
|
1180
|
+
>>> _column_type([1, 2, None]) is int
|
|
1181
|
+
True
|
|
1182
|
+
>>> import datetime as dt
|
|
1183
|
+
>>> _column_type([dt.datetime(1991,2,19), dt.time(17,35)]) is str
|
|
1184
|
+
True
|
|
1185
|
+
"""
|
|
1186
|
+
types = [_type(s, has_invisible, numparse) for s in strings]
|
|
1187
|
+
return reduce(_more_generic, types, bool)
|
|
1188
|
+
|
|
1189
|
+
|
|
1190
|
+
def _format(val, valtype, floatfmt, intfmt, missingval="", has_invisible=True):
|
|
1191
|
+
"""
|
|
1192
|
+
Format a value according to its type.
|
|
1193
|
+
|
|
1194
|
+
Unicode is supported:
|
|
1195
|
+
|
|
1196
|
+
>>> hrow = ['\u0431\u0443\u043a\u0432\u0430', '\u0446\u0438\u0444\u0440\u0430'] ; \
|
|
1197
|
+
tbl = [['\u0430\u0437', 2], ['\u0431\u0443\u043a\u0438', 4]] ; \
|
|
1198
|
+
good_result = '\\u0431\\u0443\\u043a\\u0432\\u0430 \\u0446\\u0438\\u0444\\u0440\\u0430\\n------- -------\\n\\u0430\\u0437 2\\n\\u0431\\u0443\\u043a\\u0438 4' ; \
|
|
1199
|
+
tabulate(tbl, headers=hrow) == good_result
|
|
1200
|
+
True
|
|
1201
|
+
""" # noqa
|
|
1202
|
+
if val is None:
|
|
1203
|
+
return missingval
|
|
1204
|
+
|
|
1205
|
+
if valtype is str or (valtype is not int and valtype is not bytes and valtype is not float):
|
|
1206
|
+
return f"{val}"
|
|
1207
|
+
elif valtype is int:
|
|
1208
|
+
return format(val, intfmt)
|
|
1209
|
+
elif valtype is bytes:
|
|
1210
|
+
try:
|
|
1211
|
+
return str(val, "ascii")
|
|
1212
|
+
except (TypeError, UnicodeDecodeError):
|
|
1213
|
+
return str(val)
|
|
1214
|
+
else:
|
|
1215
|
+
is_a_colored_number = has_invisible and isinstance(val, (str, bytes))
|
|
1216
|
+
if not is_a_colored_number:
|
|
1217
|
+
return format(float(val), floatfmt)
|
|
1218
|
+
raw_val = _strip_ansi(val)
|
|
1219
|
+
formatted_val = format(float(raw_val), floatfmt)
|
|
1220
|
+
return val.replace(raw_val, formatted_val)
|
|
1221
|
+
|
|
1222
|
+
|
|
1223
|
+
def _align_header(header, alignment, width, visible_width, is_multiline=False, width_fn=None):
|
|
1224
|
+
"""Pad string header to the specified width given its visible width and alignment."""
|
|
1225
|
+
"Pad string header to width chars given known visible_width of the header."
|
|
1226
|
+
if is_multiline:
|
|
1227
|
+
header_lines = re.split(_multiline_codes, header)
|
|
1228
|
+
padded_lines = [_align_header(h, alignment, width, width_fn(h)) for h in header_lines]
|
|
1229
|
+
return "\n".join(padded_lines)
|
|
1230
|
+
# else: not multiline
|
|
1231
|
+
ninvisible = len(header) - visible_width
|
|
1232
|
+
width += ninvisible
|
|
1233
|
+
if alignment == "left":
|
|
1234
|
+
return _padright(width, header)
|
|
1235
|
+
elif alignment == "center":
|
|
1236
|
+
return _padboth(width, header)
|
|
1237
|
+
elif not alignment:
|
|
1238
|
+
return f"{header}"
|
|
1239
|
+
else:
|
|
1240
|
+
return _padleft(width, header)
|
|
1241
|
+
|
|
1242
|
+
|
|
1243
|
+
def _remove_separating_lines(rows):
|
|
1244
|
+
"""Removes separating lines from a list of rows, returning the filtered rows and the indexes of the removed
|
|
1245
|
+
lines.
|
|
1246
|
+
"""
|
|
1247
|
+
if type(rows) != list:
|
|
1248
|
+
return rows, None
|
|
1249
|
+
separating_lines = []
|
|
1250
|
+
sans_rows = []
|
|
1251
|
+
for index, row in enumerate(rows):
|
|
1252
|
+
if _is_separating_line(row):
|
|
1253
|
+
separating_lines.append(index)
|
|
1254
|
+
else:
|
|
1255
|
+
sans_rows.append(row)
|
|
1256
|
+
return sans_rows, separating_lines
|
|
1257
|
+
|
|
1258
|
+
|
|
1259
|
+
def _reinsert_separating_lines(rows, separating_lines):
|
|
1260
|
+
"""Reinserts separating lines back into their original positions in the list of rows."""
|
|
1261
|
+
if separating_lines:
|
|
1262
|
+
for index in separating_lines:
|
|
1263
|
+
rows.insert(index, SEPARATING_LINE)
|
|
1264
|
+
|
|
1265
|
+
|
|
1266
|
+
def _prepend_row_index(rows, index):
|
|
1267
|
+
"""Add a left-most index column."""
|
|
1268
|
+
if index is None or index is False:
|
|
1269
|
+
return rows
|
|
1270
|
+
if isinstance(index, Sized) and len(index) != len(rows):
|
|
1271
|
+
raise ValueError(
|
|
1272
|
+
f"index must be as long as the number of data rows: len(index)={len(index)} len(rows)={len(rows)}"
|
|
1273
|
+
)
|
|
1274
|
+
sans_rows, separating_lines = _remove_separating_lines(rows)
|
|
1275
|
+
new_rows = []
|
|
1276
|
+
index_iter = iter(index)
|
|
1277
|
+
for row in sans_rows:
|
|
1278
|
+
index_v = next(index_iter)
|
|
1279
|
+
new_rows.append([index_v, *list(row)])
|
|
1280
|
+
rows = new_rows
|
|
1281
|
+
_reinsert_separating_lines(rows, separating_lines)
|
|
1282
|
+
return rows
|
|
1283
|
+
|
|
1284
|
+
|
|
1285
|
+
def _bool(val):
|
|
1286
|
+
"""Convert a value to a boolean without throwing an exception on NumPy arrays."""
|
|
1287
|
+
"A wrapper around standard bool() which doesn't throw on NumPy arrays"
|
|
1288
|
+
try:
|
|
1289
|
+
return bool(val)
|
|
1290
|
+
except ValueError: # val is likely to be a numpy array with many elements
|
|
1291
|
+
return False
|
|
1292
|
+
|
|
1293
|
+
|
|
1294
|
+
def _normalize_tabular_data(tabular_data, headers, showindex="default"):
|
|
1295
|
+
"""
|
|
1296
|
+
Transform a supported data type to a list of lists, and a list of headers, with headers padding.
|
|
1297
|
+
|
|
1298
|
+
Supported tabular data types:
|
|
1299
|
+
|
|
1300
|
+
* list-of-lists or another iterable of iterables
|
|
1301
|
+
|
|
1302
|
+
* list of named tuples (usually used with headers="keys")
|
|
1303
|
+
|
|
1304
|
+
* list of dicts (usually used with headers="keys")
|
|
1305
|
+
|
|
1306
|
+
* list of OrderedDicts (usually used with headers="keys")
|
|
1307
|
+
|
|
1308
|
+
* list of dataclasses (Python 3.7+ only, usually used with headers="keys")
|
|
1309
|
+
|
|
1310
|
+
* 2D NumPy arrays
|
|
1311
|
+
|
|
1312
|
+
* NumPy record arrays (usually used with headers="keys")
|
|
1313
|
+
|
|
1314
|
+
* dict of iterables (usually used with headers="keys")
|
|
1315
|
+
|
|
1316
|
+
* pandas.DataFrame (usually used with headers="keys")
|
|
1317
|
+
|
|
1318
|
+
The first row can be used as headers if headers="firstrow",
|
|
1319
|
+
column indices can be used as headers if headers="keys".
|
|
1320
|
+
|
|
1321
|
+
If showindex="default", show row indices of the pandas.DataFrame.
|
|
1322
|
+
If showindex="always", show row indices for all types of data.
|
|
1323
|
+
If showindex="never", don't show row indices for all types of data.
|
|
1324
|
+
If showindex is an iterable, show its values as row indices.
|
|
1325
|
+
"""
|
|
1326
|
+
try:
|
|
1327
|
+
bool(headers)
|
|
1328
|
+
is_headers2bool_broken = False
|
|
1329
|
+
except ValueError: # numpy.ndarray, pandas.core.index.Index, ...
|
|
1330
|
+
is_headers2bool_broken = True # noqa
|
|
1331
|
+
headers = list(headers)
|
|
1332
|
+
|
|
1333
|
+
index = None
|
|
1334
|
+
if hasattr(tabular_data, "keys") and hasattr(tabular_data, "values"):
|
|
1335
|
+
# dict-like and pandas.DataFrame?
|
|
1336
|
+
if hasattr(tabular_data.values, "__call__"):
|
|
1337
|
+
# likely a conventional dict
|
|
1338
|
+
keys = tabular_data.keys()
|
|
1339
|
+
rows = list(izip_longest(*tabular_data.values())) # columns have to be transposed
|
|
1340
|
+
elif hasattr(tabular_data, "index"):
|
|
1341
|
+
# values is a property, has .index => it's likely a pandas.DataFrame (pandas 0.11.0)
|
|
1342
|
+
keys = list(tabular_data)
|
|
1343
|
+
if showindex in {"default", "always", True} and tabular_data.index.name is not None:
|
|
1344
|
+
if isinstance(tabular_data.index.name, list):
|
|
1345
|
+
keys[:0] = tabular_data.index.name
|
|
1346
|
+
else:
|
|
1347
|
+
keys[:0] = [tabular_data.index.name]
|
|
1348
|
+
vals = tabular_data.values # values matrix doesn't need to be transposed
|
|
1349
|
+
# for DataFrames add an index per default
|
|
1350
|
+
index = list(tabular_data.index)
|
|
1351
|
+
rows = [list(row) for row in vals]
|
|
1352
|
+
else:
|
|
1353
|
+
raise ValueError("tabular data doesn't appear to be a dict or a DataFrame")
|
|
1354
|
+
|
|
1355
|
+
if headers == "keys":
|
|
1356
|
+
headers = list(map(str, keys)) # headers should be strings
|
|
1357
|
+
|
|
1358
|
+
else: # it's a usual iterable of iterables, or a NumPy array, or an iterable of dataclasses
|
|
1359
|
+
rows = list(tabular_data)
|
|
1360
|
+
|
|
1361
|
+
if headers == "keys" and not rows:
|
|
1362
|
+
# an empty table (issue #81)
|
|
1363
|
+
headers = []
|
|
1364
|
+
elif headers == "keys" and hasattr(tabular_data, "dtype") and getattr(tabular_data.dtype, "names"):
|
|
1365
|
+
# numpy record array
|
|
1366
|
+
headers = tabular_data.dtype.names
|
|
1367
|
+
elif headers == "keys" and len(rows) > 0 and isinstance(rows[0], tuple) and hasattr(rows[0], "_fields"):
|
|
1368
|
+
# namedtuple
|
|
1369
|
+
headers = list(map(str, rows[0]._fields))
|
|
1370
|
+
elif len(rows) > 0 and hasattr(rows[0], "keys") and hasattr(rows[0], "values"):
|
|
1371
|
+
# dict-like object
|
|
1372
|
+
uniq_keys = set() # implements hashed lookup
|
|
1373
|
+
keys = [] # storage for set
|
|
1374
|
+
if headers == "firstrow":
|
|
1375
|
+
firstdict = rows[0] if rows else {}
|
|
1376
|
+
keys.extend(firstdict.keys())
|
|
1377
|
+
uniq_keys.update(keys)
|
|
1378
|
+
rows = rows[1:]
|
|
1379
|
+
for row in rows:
|
|
1380
|
+
for k in row.keys():
|
|
1381
|
+
# Save unique items in input order
|
|
1382
|
+
if k not in uniq_keys:
|
|
1383
|
+
keys.append(k)
|
|
1384
|
+
uniq_keys.add(k)
|
|
1385
|
+
if headers == "keys":
|
|
1386
|
+
headers = keys
|
|
1387
|
+
elif isinstance(headers, dict):
|
|
1388
|
+
# a dict of headers for a list of dicts
|
|
1389
|
+
headers = [headers.get(k, k) for k in keys]
|
|
1390
|
+
headers = list(map(str, headers))
|
|
1391
|
+
elif headers == "firstrow":
|
|
1392
|
+
if len(rows) > 0:
|
|
1393
|
+
headers = [firstdict.get(k, k) for k in keys]
|
|
1394
|
+
headers = list(map(str, headers))
|
|
1395
|
+
else:
|
|
1396
|
+
headers = []
|
|
1397
|
+
elif headers:
|
|
1398
|
+
raise ValueError("headers for a list of dicts is not a dict or a keyword")
|
|
1399
|
+
rows = [[row.get(k) for k in keys] for row in rows]
|
|
1400
|
+
|
|
1401
|
+
elif (
|
|
1402
|
+
headers == "keys"
|
|
1403
|
+
and hasattr(tabular_data, "description")
|
|
1404
|
+
and hasattr(tabular_data, "fetchone")
|
|
1405
|
+
and hasattr(tabular_data, "rowcount")
|
|
1406
|
+
):
|
|
1407
|
+
# Python Database API cursor object (PEP 0249)
|
|
1408
|
+
# print tabulate(cursor, headers='keys')
|
|
1409
|
+
headers = [column[0] for column in tabular_data.description]
|
|
1410
|
+
|
|
1411
|
+
elif dataclasses is not None and len(rows) > 0 and dataclasses.is_dataclass(rows[0]):
|
|
1412
|
+
# Python 3.7+'s dataclass
|
|
1413
|
+
field_names = [field.name for field in dataclasses.fields(rows[0])]
|
|
1414
|
+
if headers == "keys":
|
|
1415
|
+
headers = field_names
|
|
1416
|
+
rows = [[getattr(row, f) for f in field_names] for row in rows]
|
|
1417
|
+
|
|
1418
|
+
elif headers == "keys" and len(rows) > 0:
|
|
1419
|
+
# keys are column indices
|
|
1420
|
+
headers = list(map(str, range(len(rows[0]))))
|
|
1421
|
+
|
|
1422
|
+
# take headers from the first row if necessary
|
|
1423
|
+
if headers == "firstrow":
|
|
1424
|
+
if len(rows) > 0:
|
|
1425
|
+
if index is not None:
|
|
1426
|
+
headers = [index[0], *list(rows[0])]
|
|
1427
|
+
index = index[1:]
|
|
1428
|
+
else:
|
|
1429
|
+
headers = rows[0]
|
|
1430
|
+
headers = list(map(str, headers)) # headers should be strings
|
|
1431
|
+
rows = rows[1:]
|
|
1432
|
+
else:
|
|
1433
|
+
headers = []
|
|
1434
|
+
|
|
1435
|
+
headers = list(map(str, headers))
|
|
1436
|
+
# rows = list(map(list, rows))
|
|
1437
|
+
rows = list(map(lambda r: r if _is_separating_line(r) else list(r), rows))
|
|
1438
|
+
|
|
1439
|
+
# add or remove an index column
|
|
1440
|
+
showindex_is_a_str = type(showindex) in {str, bytes}
|
|
1441
|
+
if showindex == "default" and index is not None:
|
|
1442
|
+
rows = _prepend_row_index(rows, index)
|
|
1443
|
+
elif isinstance(showindex, Sized) and not showindex_is_a_str:
|
|
1444
|
+
rows = _prepend_row_index(rows, list(showindex))
|
|
1445
|
+
elif isinstance(showindex, Iterable) and not showindex_is_a_str:
|
|
1446
|
+
rows = _prepend_row_index(rows, showindex)
|
|
1447
|
+
elif showindex == "always" or (_bool(showindex) and not showindex_is_a_str):
|
|
1448
|
+
if index is None:
|
|
1449
|
+
index = list(range(len(rows)))
|
|
1450
|
+
rows = _prepend_row_index(rows, index)
|
|
1451
|
+
# pad with empty headers for initial columns if necessary
|
|
1452
|
+
headers_pad = 0
|
|
1453
|
+
if headers and len(rows) > 0:
|
|
1454
|
+
headers_pad = max(0, len(rows[0]) - len(headers))
|
|
1455
|
+
headers = [""] * headers_pad + headers
|
|
1456
|
+
|
|
1457
|
+
return rows, headers, headers_pad
|
|
1458
|
+
|
|
1459
|
+
|
|
1460
|
+
def _wrap_text_to_colwidths(list_of_lists, colwidths, numparses=True):
|
|
1461
|
+
"""Wrap text in each cell of a list of lists to fit specified column widths, optionally parsing numbers."""
|
|
1462
|
+
num_cols = len(list_of_lists[0]) if len(list_of_lists) else 0
|
|
1463
|
+
numparses = _expand_iterable(numparses, num_cols, True)
|
|
1464
|
+
|
|
1465
|
+
result = []
|
|
1466
|
+
|
|
1467
|
+
for row in list_of_lists:
|
|
1468
|
+
new_row = []
|
|
1469
|
+
for cell, width, numparse in zip(row, colwidths, numparses):
|
|
1470
|
+
if _isnumber(cell) and numparse:
|
|
1471
|
+
new_row.append(cell)
|
|
1472
|
+
continue
|
|
1473
|
+
|
|
1474
|
+
if width is not None:
|
|
1475
|
+
wrapper = _CustomTextWrap(width=width)
|
|
1476
|
+
# Cast based on our internal type handling
|
|
1477
|
+
# Any future custom formatting of types (such as datetimes)
|
|
1478
|
+
# may need to be more explicit than just `str` of the object
|
|
1479
|
+
casted_cell = str(cell) if _isnumber(cell) else _type(cell, False, numparse)(cell)
|
|
1480
|
+
wrapped = ["\n".join(wrapper.wrap(line)) for line in casted_cell.splitlines() if line.strip() != ""]
|
|
1481
|
+
new_row.append("\n".join(wrapped))
|
|
1482
|
+
else:
|
|
1483
|
+
new_row.append(cell)
|
|
1484
|
+
result.append(new_row)
|
|
1485
|
+
|
|
1486
|
+
return result
|
|
1487
|
+
|
|
1488
|
+
|
|
1489
|
+
def _to_str(s, encoding="utf8", errors="ignore"):
|
|
1490
|
+
"""
|
|
1491
|
+
A type safe wrapper for converting a bytestring to str. This is essentially just
|
|
1492
|
+
a wrapper around .decode() intended for use with things like map(), but with some
|
|
1493
|
+
specific behavior:
|
|
1494
|
+
|
|
1495
|
+
1. if the given parameter is not a bytestring, it is returned unmodified
|
|
1496
|
+
2. decode() is called for the given parameter and assumes utf8 encoding, but the
|
|
1497
|
+
default error behavior is changed from 'strict' to 'ignore'
|
|
1498
|
+
|
|
1499
|
+
>>> repr(_to_str(b'foo'))
|
|
1500
|
+
"'foo'"
|
|
1501
|
+
|
|
1502
|
+
>>> repr(_to_str('foo'))
|
|
1503
|
+
"'foo'"
|
|
1504
|
+
|
|
1505
|
+
>>> repr(_to_str(42))
|
|
1506
|
+
"'42'"
|
|
1507
|
+
|
|
1508
|
+
"""
|
|
1509
|
+
if isinstance(s, bytes):
|
|
1510
|
+
return s.decode(encoding=encoding, errors=errors)
|
|
1511
|
+
return str(s)
|
|
1512
|
+
|
|
1513
|
+
|
|
1514
|
+
def tabulate(
|
|
1515
|
+
tabular_data,
|
|
1516
|
+
headers=(),
|
|
1517
|
+
tablefmt="simple",
|
|
1518
|
+
floatfmt=_DEFAULT_FLOATFMT,
|
|
1519
|
+
intfmt=_DEFAULT_INTFMT,
|
|
1520
|
+
numalign=_DEFAULT_ALIGN,
|
|
1521
|
+
stralign=_DEFAULT_ALIGN,
|
|
1522
|
+
missingval=_DEFAULT_MISSINGVAL,
|
|
1523
|
+
showindex="default",
|
|
1524
|
+
disable_numparse=False,
|
|
1525
|
+
colglobalalign=None,
|
|
1526
|
+
colalign=None,
|
|
1527
|
+
maxcolwidths=None,
|
|
1528
|
+
headersglobalalign=None,
|
|
1529
|
+
headersalign=None,
|
|
1530
|
+
rowalign=None,
|
|
1531
|
+
maxheadercolwidths=None,
|
|
1532
|
+
):
|
|
1533
|
+
r"""
|
|
1534
|
+
Format a fixed width table for pretty printing.
|
|
1535
|
+
|
|
1536
|
+
>>> print(tabulate([[1, 2.34], [-56, "8.999"], ["2", "10001"]]))
|
|
1537
|
+
--- ---------
|
|
1538
|
+
1 2.34
|
|
1539
|
+
-56 8.999
|
|
1540
|
+
2 10001
|
|
1541
|
+
--- ---------
|
|
1542
|
+
|
|
1543
|
+
The first required argument (`tabular_data`) can be a
|
|
1544
|
+
list-of-lists (or another iterable of iterables), a list of named
|
|
1545
|
+
tuples, a dictionary of iterables, an iterable of dictionaries,
|
|
1546
|
+
an iterable of dataclasses (Python 3.7+), a two-dimensional NumPy array,
|
|
1547
|
+
NumPy record array, or a Pandas' dataframe.
|
|
1548
|
+
|
|
1549
|
+
|
|
1550
|
+
Table headers
|
|
1551
|
+
-------------
|
|
1552
|
+
|
|
1553
|
+
To print nice column headers, supply the second argument (`headers`):
|
|
1554
|
+
|
|
1555
|
+
- `headers` can be an explicit list of column headers
|
|
1556
|
+
- if `headers="firstrow"`, then the first row of data is used
|
|
1557
|
+
- if `headers="keys"`, then dictionary keys or column indices are used
|
|
1558
|
+
|
|
1559
|
+
Otherwise a headerless table is produced.
|
|
1560
|
+
|
|
1561
|
+
If the number of headers is less than the number of columns, they
|
|
1562
|
+
are supposed to be names of the last columns. This is consistent
|
|
1563
|
+
with the plain-text format of R and Pandas' dataframes.
|
|
1564
|
+
|
|
1565
|
+
>>> print(tabulate([["sex","age"],["Alice","F",24],["Bob","M",19]],
|
|
1566
|
+
... headers="firstrow"))
|
|
1567
|
+
sex age
|
|
1568
|
+
----- ----- -----
|
|
1569
|
+
Alice F 24
|
|
1570
|
+
Bob M 19
|
|
1571
|
+
|
|
1572
|
+
By default, pandas.DataFrame data have an additional column called
|
|
1573
|
+
row index. To add a similar column to all other types of data,
|
|
1574
|
+
use `showindex="always"` or `showindex=True`. To suppress row indices
|
|
1575
|
+
for all types of data, pass `showindex="never" or `showindex=False`.
|
|
1576
|
+
To add a custom row index column, pass `showindex=some_iterable`.
|
|
1577
|
+
|
|
1578
|
+
>>> print(tabulate([["F",24],["M",19]], showindex="always"))
|
|
1579
|
+
- - --
|
|
1580
|
+
0 F 24
|
|
1581
|
+
1 M 19
|
|
1582
|
+
- - --
|
|
1583
|
+
|
|
1584
|
+
|
|
1585
|
+
Column and Headers alignment
|
|
1586
|
+
----------------------------
|
|
1587
|
+
|
|
1588
|
+
`tabulate` tries to detect column types automatically, and aligns
|
|
1589
|
+
the values properly. By default it aligns decimal points of the
|
|
1590
|
+
numbers (or flushes integer numbers to the right), and flushes
|
|
1591
|
+
everything else to the left. Possible column alignments
|
|
1592
|
+
(`numalign`, `stralign`) are: "right", "center", "left", "decimal"
|
|
1593
|
+
(only for `numalign`), and None (to disable alignment).
|
|
1594
|
+
|
|
1595
|
+
`colglobalalign` allows for global alignment of columns, before any
|
|
1596
|
+
specific override from `colalign`. Possible values are: None
|
|
1597
|
+
(defaults according to coltype), "right", "center", "decimal",
|
|
1598
|
+
"left".
|
|
1599
|
+
`colalign` allows for column-wise override starting from left-most
|
|
1600
|
+
column. Possible values are: "global" (no override), "right",
|
|
1601
|
+
"center", "decimal", "left".
|
|
1602
|
+
`headersglobalalign` allows for global headers alignment, before any
|
|
1603
|
+
specific override from `headersalign`. Possible values are: None
|
|
1604
|
+
(follow columns alignment), "right", "center", "left".
|
|
1605
|
+
`headersalign` allows for header-wise override starting from left-most
|
|
1606
|
+
given header. Possible values are: "global" (no override), "same"
|
|
1607
|
+
(follow column alignment), "right", "center", "left".
|
|
1608
|
+
|
|
1609
|
+
Note on intended behavior: If there is no `tabular_data`, any column
|
|
1610
|
+
alignment argument is ignored. Hence, in this case, header
|
|
1611
|
+
alignment cannot be inferred from column alignment.
|
|
1612
|
+
|
|
1613
|
+
Table formats
|
|
1614
|
+
-------------
|
|
1615
|
+
|
|
1616
|
+
`intfmt` is a format specification used for columns which
|
|
1617
|
+
contain numeric data without a decimal point. This can also be
|
|
1618
|
+
a list or tuple of format strings, one per column.
|
|
1619
|
+
|
|
1620
|
+
`floatfmt` is a format specification used for columns which
|
|
1621
|
+
contain numeric data with a decimal point. This can also be
|
|
1622
|
+
a list or tuple of format strings, one per column.
|
|
1623
|
+
|
|
1624
|
+
`None` values are replaced with a `missingval` string (like
|
|
1625
|
+
`floatfmt`, this can also be a list of values for different
|
|
1626
|
+
columns):
|
|
1627
|
+
|
|
1628
|
+
>>> print(tabulate([["spam", 1, None],
|
|
1629
|
+
... ["eggs", 42, 3.14],
|
|
1630
|
+
... ["other", None, 2.7]], missingval="?"))
|
|
1631
|
+
----- -- ----
|
|
1632
|
+
spam 1 ?
|
|
1633
|
+
eggs 42 3.14
|
|
1634
|
+
other ? 2.7
|
|
1635
|
+
----- -- ----
|
|
1636
|
+
|
|
1637
|
+
Various plain-text table formats (`tablefmt`) are supported:
|
|
1638
|
+
'plain', 'simple', 'grid', 'pipe', 'orgtbl', 'rst', 'mediawiki',
|
|
1639
|
+
'latex', 'latex_raw', 'latex_booktabs', 'latex_longtable' and tsv.
|
|
1640
|
+
Variable `tabulate_formats`contains the list of currently supported formats.
|
|
1641
|
+
|
|
1642
|
+
"plain" format doesn't use any pseudographics to draw tables,
|
|
1643
|
+
it separates columns with a double space:
|
|
1644
|
+
|
|
1645
|
+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]],
|
|
1646
|
+
... ["strings", "numbers"], "plain"))
|
|
1647
|
+
strings numbers
|
|
1648
|
+
spam 41.9999
|
|
1649
|
+
eggs 451
|
|
1650
|
+
|
|
1651
|
+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], tablefmt="plain"))
|
|
1652
|
+
spam 41.9999
|
|
1653
|
+
eggs 451
|
|
1654
|
+
|
|
1655
|
+
"simple" format is like Pandoc simple_tables:
|
|
1656
|
+
|
|
1657
|
+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]],
|
|
1658
|
+
... ["strings", "numbers"], "simple"))
|
|
1659
|
+
strings numbers
|
|
1660
|
+
--------- ---------
|
|
1661
|
+
spam 41.9999
|
|
1662
|
+
eggs 451
|
|
1663
|
+
|
|
1664
|
+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], tablefmt="simple"))
|
|
1665
|
+
---- --------
|
|
1666
|
+
spam 41.9999
|
|
1667
|
+
eggs 451
|
|
1668
|
+
---- --------
|
|
1669
|
+
|
|
1670
|
+
"grid" is similar to tables produced by Emacs table.el package or
|
|
1671
|
+
Pandoc grid_tables:
|
|
1672
|
+
|
|
1673
|
+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]],
|
|
1674
|
+
... ["strings", "numbers"], "grid"))
|
|
1675
|
+
+-----------+-----------+
|
|
1676
|
+
| strings | numbers |
|
|
1677
|
+
+===========+===========+
|
|
1678
|
+
| spam | 41.9999 |
|
|
1679
|
+
+-----------+-----------+
|
|
1680
|
+
| eggs | 451 |
|
|
1681
|
+
+-----------+-----------+
|
|
1682
|
+
|
|
1683
|
+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], tablefmt="grid"))
|
|
1684
|
+
+------+----------+
|
|
1685
|
+
| spam | 41.9999 |
|
|
1686
|
+
+------+----------+
|
|
1687
|
+
| eggs | 451 |
|
|
1688
|
+
+------+----------+
|
|
1689
|
+
|
|
1690
|
+
"simple_grid" draws a grid using single-line box-drawing
|
|
1691
|
+
characters:
|
|
1692
|
+
|
|
1693
|
+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]],
|
|
1694
|
+
... ["strings", "numbers"], "simple_grid"))
|
|
1695
|
+
┌───────────┬───────────┐
|
|
1696
|
+
│ strings │ numbers │
|
|
1697
|
+
├───────────┼───────────┤
|
|
1698
|
+
│ spam │ 41.9999 │
|
|
1699
|
+
├───────────┼───────────┤
|
|
1700
|
+
│ eggs │ 451 │
|
|
1701
|
+
└───────────┴───────────┘
|
|
1702
|
+
|
|
1703
|
+
"rounded_grid" draws a grid using single-line box-drawing
|
|
1704
|
+
characters with rounded corners:
|
|
1705
|
+
|
|
1706
|
+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]],
|
|
1707
|
+
... ["strings", "numbers"], "rounded_grid"))
|
|
1708
|
+
╭───────────┬───────────╮
|
|
1709
|
+
│ strings │ numbers │
|
|
1710
|
+
├───────────┼───────────┤
|
|
1711
|
+
│ spam │ 41.9999 │
|
|
1712
|
+
├───────────┼───────────┤
|
|
1713
|
+
│ eggs │ 451 │
|
|
1714
|
+
╰───────────┴───────────╯
|
|
1715
|
+
|
|
1716
|
+
"heavy_grid" draws a grid using bold (thick) single-line box-drawing
|
|
1717
|
+
characters:
|
|
1718
|
+
|
|
1719
|
+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]],
|
|
1720
|
+
... ["strings", "numbers"], "heavy_grid"))
|
|
1721
|
+
┏━━━━━━━━━━━┳━━━━━━━━━━━┓
|
|
1722
|
+
┃ strings ┃ numbers ┃
|
|
1723
|
+
┣━━━━━━━━━━━╋━━━━━━━━━━━┫
|
|
1724
|
+
┃ spam ┃ 41.9999 ┃
|
|
1725
|
+
┣━━━━━━━━━━━╋━━━━━━━━━━━┫
|
|
1726
|
+
┃ eggs ┃ 451 ┃
|
|
1727
|
+
┗━━━━━━━━━━━┻━━━━━━━━━━━┛
|
|
1728
|
+
|
|
1729
|
+
"mixed_grid" draws a grid using a mix of light (thin) and heavy (thick) lines
|
|
1730
|
+
box-drawing characters:
|
|
1731
|
+
|
|
1732
|
+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]],
|
|
1733
|
+
... ["strings", "numbers"], "mixed_grid"))
|
|
1734
|
+
┍━━━━━━━━━━━┯━━━━━━━━━━━┑
|
|
1735
|
+
│ strings │ numbers │
|
|
1736
|
+
┝━━━━━━━━━━━┿━━━━━━━━━━━┥
|
|
1737
|
+
│ spam │ 41.9999 │
|
|
1738
|
+
├───────────┼───────────┤
|
|
1739
|
+
│ eggs │ 451 │
|
|
1740
|
+
┕━━━━━━━━━━━┷━━━━━━━━━━━┙
|
|
1741
|
+
|
|
1742
|
+
"double_grid" draws a grid using double-line box-drawing
|
|
1743
|
+
characters:
|
|
1744
|
+
|
|
1745
|
+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]],
|
|
1746
|
+
... ["strings", "numbers"], "double_grid"))
|
|
1747
|
+
╔═══════════╦═══════════╗
|
|
1748
|
+
║ strings ║ numbers ║
|
|
1749
|
+
╠═══════════╬═══════════╣
|
|
1750
|
+
║ spam ║ 41.9999 ║
|
|
1751
|
+
╠═══════════╬═══════════╣
|
|
1752
|
+
║ eggs ║ 451 ║
|
|
1753
|
+
╚═══════════╩═══════════╝
|
|
1754
|
+
|
|
1755
|
+
"fancy_grid" draws a grid using a mix of single and
|
|
1756
|
+
double-line box-drawing characters:
|
|
1757
|
+
|
|
1758
|
+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]],
|
|
1759
|
+
... ["strings", "numbers"], "fancy_grid"))
|
|
1760
|
+
╒═══════════╤═══════════╕
|
|
1761
|
+
│ strings │ numbers │
|
|
1762
|
+
╞═══════════╪═══════════╡
|
|
1763
|
+
│ spam │ 41.9999 │
|
|
1764
|
+
├───────────┼───────────┤
|
|
1765
|
+
│ eggs │ 451 │
|
|
1766
|
+
╘═══════════╧═══════════╛
|
|
1767
|
+
|
|
1768
|
+
"outline" is the same as the "grid" format but doesn't draw lines between rows:
|
|
1769
|
+
|
|
1770
|
+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]],
|
|
1771
|
+
... ["strings", "numbers"], "outline"))
|
|
1772
|
+
+-----------+-----------+
|
|
1773
|
+
| strings | numbers |
|
|
1774
|
+
+===========+===========+
|
|
1775
|
+
| spam | 41.9999 |
|
|
1776
|
+
| eggs | 451 |
|
|
1777
|
+
+-----------+-----------+
|
|
1778
|
+
|
|
1779
|
+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], tablefmt="outline"))
|
|
1780
|
+
+------+----------+
|
|
1781
|
+
| spam | 41.9999 |
|
|
1782
|
+
| eggs | 451 |
|
|
1783
|
+
+------+----------+
|
|
1784
|
+
|
|
1785
|
+
"simple_outline" is the same as the "simple_grid" format but doesn't draw lines between rows:
|
|
1786
|
+
|
|
1787
|
+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]],
|
|
1788
|
+
... ["strings", "numbers"], "simple_outline"))
|
|
1789
|
+
┌───────────┬───────────┐
|
|
1790
|
+
│ strings │ numbers │
|
|
1791
|
+
├───────────┼───────────┤
|
|
1792
|
+
│ spam │ 41.9999 │
|
|
1793
|
+
│ eggs │ 451 │
|
|
1794
|
+
└───────────┴───────────┘
|
|
1795
|
+
|
|
1796
|
+
"rounded_outline" is the same as the "rounded_grid" format but doesn't draw lines between rows:
|
|
1797
|
+
|
|
1798
|
+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]],
|
|
1799
|
+
... ["strings", "numbers"], "rounded_outline"))
|
|
1800
|
+
╭───────────┬───────────╮
|
|
1801
|
+
│ strings │ numbers │
|
|
1802
|
+
├───────────┼───────────┤
|
|
1803
|
+
│ spam │ 41.9999 │
|
|
1804
|
+
│ eggs │ 451 │
|
|
1805
|
+
╰───────────┴───────────╯
|
|
1806
|
+
|
|
1807
|
+
"heavy_outline" is the same as the "heavy_grid" format but doesn't draw lines between rows:
|
|
1808
|
+
|
|
1809
|
+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]],
|
|
1810
|
+
... ["strings", "numbers"], "heavy_outline"))
|
|
1811
|
+
┏━━━━━━━━━━━┳━━━━━━━━━━━┓
|
|
1812
|
+
┃ strings ┃ numbers ┃
|
|
1813
|
+
┣━━━━━━━━━━━╋━━━━━━━━━━━┫
|
|
1814
|
+
┃ spam ┃ 41.9999 ┃
|
|
1815
|
+
┃ eggs ┃ 451 ┃
|
|
1816
|
+
┗━━━━━━━━━━━┻━━━━━━━━━━━┛
|
|
1817
|
+
|
|
1818
|
+
"mixed_outline" is the same as the "mixed_grid" format but doesn't draw lines between rows:
|
|
1819
|
+
|
|
1820
|
+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]],
|
|
1821
|
+
... ["strings", "numbers"], "mixed_outline"))
|
|
1822
|
+
┍━━━━━━━━━━━┯━━━━━━━━━━━┑
|
|
1823
|
+
│ strings │ numbers │
|
|
1824
|
+
┝━━━━━━━━━━━┿━━━━━━━━━━━┥
|
|
1825
|
+
│ spam │ 41.9999 │
|
|
1826
|
+
│ eggs │ 451 │
|
|
1827
|
+
┕━━━━━━━━━━━┷━━━━━━━━━━━┙
|
|
1828
|
+
|
|
1829
|
+
"double_outline" is the same as the "double_grid" format but doesn't draw lines between rows:
|
|
1830
|
+
|
|
1831
|
+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]],
|
|
1832
|
+
... ["strings", "numbers"], "double_outline"))
|
|
1833
|
+
╔═══════════╦═══════════╗
|
|
1834
|
+
║ strings ║ numbers ║
|
|
1835
|
+
╠═══════════╬═══════════╣
|
|
1836
|
+
║ spam ║ 41.9999 ║
|
|
1837
|
+
║ eggs ║ 451 ║
|
|
1838
|
+
╚═══════════╩═══════════╝
|
|
1839
|
+
|
|
1840
|
+
"fancy_outline" is the same as the "fancy_grid" format but doesn't draw lines between rows:
|
|
1841
|
+
|
|
1842
|
+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]],
|
|
1843
|
+
... ["strings", "numbers"], "fancy_outline"))
|
|
1844
|
+
╒═══════════╤═══════════╕
|
|
1845
|
+
│ strings │ numbers │
|
|
1846
|
+
╞═══════════╪═══════════╡
|
|
1847
|
+
│ spam │ 41.9999 │
|
|
1848
|
+
│ eggs │ 451 │
|
|
1849
|
+
╘═══════════╧═══════════╛
|
|
1850
|
+
|
|
1851
|
+
"pipe" is like tables in PHP Markdown Extra extension or Pandoc
|
|
1852
|
+
pipe_tables:
|
|
1853
|
+
|
|
1854
|
+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]],
|
|
1855
|
+
... ["strings", "numbers"], "pipe"))
|
|
1856
|
+
| strings | numbers |
|
|
1857
|
+
|:----------|----------:|
|
|
1858
|
+
| spam | 41.9999 |
|
|
1859
|
+
| eggs | 451 |
|
|
1860
|
+
|
|
1861
|
+
"presto" is like tables produce by the Presto CLI:
|
|
1862
|
+
|
|
1863
|
+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]],
|
|
1864
|
+
... ["strings", "numbers"], "presto"))
|
|
1865
|
+
strings | numbers
|
|
1866
|
+
-----------+-----------
|
|
1867
|
+
spam | 41.9999
|
|
1868
|
+
eggs | 451
|
|
1869
|
+
|
|
1870
|
+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], tablefmt="pipe"))
|
|
1871
|
+
|:-----|---------:|
|
|
1872
|
+
| spam | 41.9999 |
|
|
1873
|
+
| eggs | 451 |
|
|
1874
|
+
|
|
1875
|
+
"orgtbl" is like tables in Emacs org-mode and orgtbl-mode. They
|
|
1876
|
+
are slightly different from "pipe" format by not using colons to
|
|
1877
|
+
define column alignment, and using a "+" sign to indicate line
|
|
1878
|
+
intersections:
|
|
1879
|
+
|
|
1880
|
+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]],
|
|
1881
|
+
... ["strings", "numbers"], "orgtbl"))
|
|
1882
|
+
| strings | numbers |
|
|
1883
|
+
|-----------+-----------|
|
|
1884
|
+
| spam | 41.9999 |
|
|
1885
|
+
| eggs | 451 |
|
|
1886
|
+
|
|
1887
|
+
|
|
1888
|
+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], tablefmt="orgtbl"))
|
|
1889
|
+
| spam | 41.9999 |
|
|
1890
|
+
| eggs | 451 |
|
|
1891
|
+
|
|
1892
|
+
"rst" is like a simple table format from reStructuredText; please
|
|
1893
|
+
note that reStructuredText accepts also "grid" tables:
|
|
1894
|
+
|
|
1895
|
+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]],
|
|
1896
|
+
... ["strings", "numbers"], "rst"))
|
|
1897
|
+
========= =========
|
|
1898
|
+
strings numbers
|
|
1899
|
+
========= =========
|
|
1900
|
+
spam 41.9999
|
|
1901
|
+
eggs 451
|
|
1902
|
+
========= =========
|
|
1903
|
+
|
|
1904
|
+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], tablefmt="rst"))
|
|
1905
|
+
==== ========
|
|
1906
|
+
spam 41.9999
|
|
1907
|
+
eggs 451
|
|
1908
|
+
==== ========
|
|
1909
|
+
|
|
1910
|
+
"mediawiki" produces a table markup used in Wikipedia and on other
|
|
1911
|
+
MediaWiki-based sites:
|
|
1912
|
+
|
|
1913
|
+
>>> print(tabulate([["strings", "numbers"], ["spam", 41.9999], ["eggs", "451.0"]],
|
|
1914
|
+
... headers="firstrow", tablefmt="mediawiki"))
|
|
1915
|
+
{| class="wikitable" style="text-align: left;"
|
|
1916
|
+
|+ <!-- caption -->
|
|
1917
|
+
|-
|
|
1918
|
+
! strings !! style="text-align: right;"| numbers
|
|
1919
|
+
|-
|
|
1920
|
+
| spam || style="text-align: right;"| 41.9999
|
|
1921
|
+
|-
|
|
1922
|
+
| eggs || style="text-align: right;"| 451
|
|
1923
|
+
|}
|
|
1924
|
+
|
|
1925
|
+
"html" produces HTML markup as an html.escape'd str
|
|
1926
|
+
with a ._repr_html_ method so that Jupyter Lab and Notebook display the HTML
|
|
1927
|
+
and a .str property so that the raw HTML remains accessible
|
|
1928
|
+
the unsafehtml table format can be used if an unescaped HTML format is required:
|
|
1929
|
+
|
|
1930
|
+
>>> print(tabulate([["strings", "numbers"], ["spam", 41.9999], ["eggs", "451.0"]],
|
|
1931
|
+
... headers="firstrow", tablefmt="html"))
|
|
1932
|
+
<table>
|
|
1933
|
+
<thead>
|
|
1934
|
+
<tr><th>strings </th><th style="text-align: right;"> numbers</th></tr>
|
|
1935
|
+
</thead>
|
|
1936
|
+
<tbody>
|
|
1937
|
+
<tr><td>spam </td><td style="text-align: right;"> 41.9999</td></tr>
|
|
1938
|
+
<tr><td>eggs </td><td style="text-align: right;"> 451 </td></tr>
|
|
1939
|
+
</tbody>
|
|
1940
|
+
</table>
|
|
1941
|
+
|
|
1942
|
+
"latex" produces a tabular environment of LaTeX document markup:
|
|
1943
|
+
|
|
1944
|
+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], tablefmt="latex"))
|
|
1945
|
+
\\begin{tabular}{lr}
|
|
1946
|
+
\\hline
|
|
1947
|
+
spam & 41.9999 \\\\
|
|
1948
|
+
eggs & 451 \\\\
|
|
1949
|
+
\\hline
|
|
1950
|
+
\\end{tabular}
|
|
1951
|
+
|
|
1952
|
+
"latex_raw" is similar to "latex", but doesn't escape special characters,
|
|
1953
|
+
such as backslash and underscore, so LaTeX commands may embedded into
|
|
1954
|
+
cells' values:
|
|
1955
|
+
|
|
1956
|
+
>>> print(tabulate([["spam$_9$", 41.9999], ["\\\\emph{eggs}", "451.0"]], tablefmt="latex_raw"))
|
|
1957
|
+
\\begin{tabular}{lr}
|
|
1958
|
+
\\hline
|
|
1959
|
+
spam$_9$ & 41.9999 \\\\
|
|
1960
|
+
\\emph{eggs} & 451 \\\\
|
|
1961
|
+
\\hline
|
|
1962
|
+
\\end{tabular}
|
|
1963
|
+
|
|
1964
|
+
"latex_booktabs" produces a tabular environment of LaTeX document markup
|
|
1965
|
+
using the booktabs.sty package:
|
|
1966
|
+
|
|
1967
|
+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], tablefmt="latex_booktabs"))
|
|
1968
|
+
\\begin{tabular}{lr}
|
|
1969
|
+
\\toprule
|
|
1970
|
+
spam & 41.9999 \\\\
|
|
1971
|
+
eggs & 451 \\\\
|
|
1972
|
+
\\bottomrule
|
|
1973
|
+
\\end{tabular}
|
|
1974
|
+
|
|
1975
|
+
"latex_longtable" produces a tabular environment that can stretch along
|
|
1976
|
+
multiple pages, using the longtable package for LaTeX.
|
|
1977
|
+
|
|
1978
|
+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], tablefmt="latex_longtable"))
|
|
1979
|
+
\\begin{longtable}{lr}
|
|
1980
|
+
\\hline
|
|
1981
|
+
spam & 41.9999 \\\\
|
|
1982
|
+
eggs & 451 \\\\
|
|
1983
|
+
\\hline
|
|
1984
|
+
\\end{longtable}
|
|
1985
|
+
|
|
1986
|
+
|
|
1987
|
+
Number parsing
|
|
1988
|
+
--------------
|
|
1989
|
+
By default, anything which can be parsed as a number is a number.
|
|
1990
|
+
This ensures numbers represented as strings are aligned properly.
|
|
1991
|
+
This can lead to weird results for particular strings such as
|
|
1992
|
+
specific git SHAs e.g. "42992e1" will be parsed into the number
|
|
1993
|
+
429920 and aligned as such.
|
|
1994
|
+
|
|
1995
|
+
To completely disable number parsing (and alignment), use
|
|
1996
|
+
`disable_numparse=True`. For more fine grained control, a list column
|
|
1997
|
+
indices is used to disable number parsing only on those columns
|
|
1998
|
+
e.g. `disable_numparse=[0, 2]` would disable number parsing only on the
|
|
1999
|
+
first and third columns.
|
|
2000
|
+
|
|
2001
|
+
Column Widths and Auto Line Wrapping
|
|
2002
|
+
------------------------------------
|
|
2003
|
+
Tabulate will, by default, set the width of each column to the length of the
|
|
2004
|
+
longest element in that column. However, in situations where fields are expected
|
|
2005
|
+
to reasonably be too long to look good as a single line, tabulate can help automate
|
|
2006
|
+
word wrapping long fields for you. Use the parameter `maxcolwidth` to provide a
|
|
2007
|
+
list of maximal column widths
|
|
2008
|
+
|
|
2009
|
+
>>> print(tabulate( \
|
|
2010
|
+
[('1', 'John Smith', \
|
|
2011
|
+
'This is a rather long description that might look better if it is wrapped a bit')], \
|
|
2012
|
+
headers=("Issue Id", "Author", "Description"), \
|
|
2013
|
+
maxcolwidths=[None, None, 30], \
|
|
2014
|
+
tablefmt="grid" \
|
|
2015
|
+
))
|
|
2016
|
+
+------------+------------+-------------------------------+
|
|
2017
|
+
| Issue Id | Author | Description |
|
|
2018
|
+
+============+============+===============================+
|
|
2019
|
+
| 1 | John Smith | This is a rather long |
|
|
2020
|
+
| | | description that might look |
|
|
2021
|
+
| | | better if it is wrapped a bit |
|
|
2022
|
+
+------------+------------+-------------------------------+
|
|
2023
|
+
|
|
2024
|
+
Header column width can be specified in a similar way using `maxheadercolwidth`
|
|
2025
|
+
"""
|
|
2026
|
+
if tabular_data is None:
|
|
2027
|
+
tabular_data = []
|
|
2028
|
+
|
|
2029
|
+
list_of_lists, headers, headers_pad = _normalize_tabular_data(tabular_data, headers, showindex=showindex)
|
|
2030
|
+
list_of_lists, separating_lines = _remove_separating_lines(list_of_lists)
|
|
2031
|
+
|
|
2032
|
+
if maxcolwidths is not None:
|
|
2033
|
+
num_cols = len(list_of_lists[0]) if len(list_of_lists) else 0
|
|
2034
|
+
if isinstance(maxcolwidths, int): # Expand scalar for all columns
|
|
2035
|
+
maxcolwidths = _expand_iterable(maxcolwidths, num_cols, maxcolwidths)
|
|
2036
|
+
else: # Ignore col width for any 'trailing' columns
|
|
2037
|
+
maxcolwidths = _expand_iterable(maxcolwidths, num_cols, None)
|
|
2038
|
+
|
|
2039
|
+
numparses = _expand_numparse(disable_numparse, num_cols)
|
|
2040
|
+
list_of_lists = _wrap_text_to_colwidths(list_of_lists, maxcolwidths, numparses=numparses)
|
|
2041
|
+
|
|
2042
|
+
if maxheadercolwidths is not None:
|
|
2043
|
+
num_cols = len(list_of_lists[0])
|
|
2044
|
+
if isinstance(maxheadercolwidths, int): # Expand scalar for all columns
|
|
2045
|
+
maxheadercolwidths = _expand_iterable(maxheadercolwidths, num_cols, maxheadercolwidths)
|
|
2046
|
+
else: # Ignore col width for any 'trailing' columns
|
|
2047
|
+
maxheadercolwidths = _expand_iterable(maxheadercolwidths, num_cols, None)
|
|
2048
|
+
|
|
2049
|
+
numparses = _expand_numparse(disable_numparse, num_cols)
|
|
2050
|
+
headers = _wrap_text_to_colwidths([headers], maxheadercolwidths, numparses=numparses)[0]
|
|
2051
|
+
|
|
2052
|
+
# empty values in the first column of RST tables should be escaped (issue #82)
|
|
2053
|
+
# "" should be escaped as "\\ " or ".."
|
|
2054
|
+
if tablefmt == "rst":
|
|
2055
|
+
list_of_lists, headers = _rst_escape_first_column(list_of_lists, headers)
|
|
2056
|
+
|
|
2057
|
+
# PrettyTable formatting does not use any extra padding.
|
|
2058
|
+
# Numbers are not parsed and are treated the same as strings for alignment.
|
|
2059
|
+
# Check if pretty is the format being used and override the defaults so it
|
|
2060
|
+
# does not impact other formats.
|
|
2061
|
+
min_padding = MIN_PADDING
|
|
2062
|
+
if tablefmt == "pretty":
|
|
2063
|
+
min_padding = 0
|
|
2064
|
+
disable_numparse = True
|
|
2065
|
+
numalign = "center" if numalign == _DEFAULT_ALIGN else numalign
|
|
2066
|
+
stralign = "center" if stralign == _DEFAULT_ALIGN else stralign
|
|
2067
|
+
else:
|
|
2068
|
+
numalign = "decimal" if numalign == _DEFAULT_ALIGN else numalign
|
|
2069
|
+
stralign = "left" if stralign == _DEFAULT_ALIGN else stralign
|
|
2070
|
+
|
|
2071
|
+
# optimization: look for ANSI control codes once,
|
|
2072
|
+
# enable smart width functions only if a control code is found
|
|
2073
|
+
#
|
|
2074
|
+
# convert the headers and rows into a single, tab-delimited string ensuring
|
|
2075
|
+
# that any bytestrings are decoded safely (i.e. errors ignored)
|
|
2076
|
+
plain_text = "\t".join(
|
|
2077
|
+
chain(
|
|
2078
|
+
# headers
|
|
2079
|
+
map(_to_str, headers),
|
|
2080
|
+
# rows: chain the rows together into a single iterable after mapping
|
|
2081
|
+
# the bytestring conversino to each cell value
|
|
2082
|
+
chain.from_iterable(map(_to_str, row) for row in list_of_lists),
|
|
2083
|
+
)
|
|
2084
|
+
)
|
|
2085
|
+
|
|
2086
|
+
has_invisible = _ansi_codes.search(plain_text) is not None
|
|
2087
|
+
|
|
2088
|
+
enable_widechars = wcwidth is not None and WIDE_CHARS_MODE
|
|
2089
|
+
if not isinstance(tablefmt, TableFormat) and tablefmt in multiline_formats and _is_multiline(plain_text):
|
|
2090
|
+
tablefmt = multiline_formats.get(tablefmt, tablefmt)
|
|
2091
|
+
is_multiline = True
|
|
2092
|
+
else:
|
|
2093
|
+
is_multiline = False
|
|
2094
|
+
width_fn = _choose_width_fn(has_invisible, enable_widechars, is_multiline)
|
|
2095
|
+
|
|
2096
|
+
# format rows and columns, convert numeric values to strings
|
|
2097
|
+
cols = list(izip_longest(*list_of_lists))
|
|
2098
|
+
numparses = _expand_numparse(disable_numparse, len(cols))
|
|
2099
|
+
coltypes = [_column_type(col, numparse=np) for col, np in zip(cols, numparses)]
|
|
2100
|
+
if isinstance(floatfmt, str): # old version
|
|
2101
|
+
float_formats = len(cols) * [floatfmt] # just duplicate the string to use in each column
|
|
2102
|
+
else: # if floatfmt is list, tuple etc we have one per column
|
|
2103
|
+
float_formats = list(floatfmt)
|
|
2104
|
+
if len(float_formats) < len(cols):
|
|
2105
|
+
float_formats.extend((len(cols) - len(float_formats)) * [_DEFAULT_FLOATFMT])
|
|
2106
|
+
if isinstance(intfmt, str): # old version
|
|
2107
|
+
int_formats = len(cols) * [intfmt] # just duplicate the string to use in each column
|
|
2108
|
+
else: # if intfmt is list, tuple etc we have one per column
|
|
2109
|
+
int_formats = list(intfmt)
|
|
2110
|
+
if len(int_formats) < len(cols):
|
|
2111
|
+
int_formats.extend((len(cols) - len(int_formats)) * [_DEFAULT_INTFMT])
|
|
2112
|
+
if isinstance(missingval, str):
|
|
2113
|
+
missing_vals = len(cols) * [missingval]
|
|
2114
|
+
else:
|
|
2115
|
+
missing_vals = list(missingval)
|
|
2116
|
+
if len(missing_vals) < len(cols):
|
|
2117
|
+
missing_vals.extend((len(cols) - len(missing_vals)) * [_DEFAULT_MISSINGVAL])
|
|
2118
|
+
cols = [
|
|
2119
|
+
[_format(v, ct, fl_fmt, int_fmt, miss_v, has_invisible) for v in c]
|
|
2120
|
+
for c, ct, fl_fmt, int_fmt, miss_v in zip(cols, coltypes, float_formats, int_formats, missing_vals)
|
|
2121
|
+
]
|
|
2122
|
+
|
|
2123
|
+
# align columns
|
|
2124
|
+
# first set global alignment
|
|
2125
|
+
if colglobalalign is not None: # if global alignment provided
|
|
2126
|
+
aligns = [colglobalalign] * len(cols)
|
|
2127
|
+
else: # default
|
|
2128
|
+
aligns = [numalign if ct in {int, float} else stralign for ct in coltypes]
|
|
2129
|
+
# then specific alignments
|
|
2130
|
+
if colalign is not None:
|
|
2131
|
+
assert isinstance(colalign, Iterable)
|
|
2132
|
+
if isinstance(colalign, str):
|
|
2133
|
+
warnings.warn(
|
|
2134
|
+
f'As a string, `colalign` is interpreted as {list(colalign)}. Did you mean `colglobalalign = "{colalign}"` or `colalign = ("{colalign}",)`?',
|
|
2135
|
+
stacklevel=2,
|
|
2136
|
+
)
|
|
2137
|
+
for idx, align in enumerate(colalign):
|
|
2138
|
+
if idx >= len(aligns):
|
|
2139
|
+
break
|
|
2140
|
+
elif align != "global":
|
|
2141
|
+
aligns[idx] = align
|
|
2142
|
+
minwidths = [width_fn(h) + min_padding for h in headers] if headers else [0] * len(cols)
|
|
2143
|
+
cols = [
|
|
2144
|
+
_align_column(c, a, minw, has_invisible, enable_widechars, is_multiline)
|
|
2145
|
+
for c, a, minw in zip(cols, aligns, minwidths)
|
|
2146
|
+
]
|
|
2147
|
+
|
|
2148
|
+
aligns_headers = None
|
|
2149
|
+
if headers:
|
|
2150
|
+
# align headers and add headers
|
|
2151
|
+
t_cols = cols or [[""]] * len(headers)
|
|
2152
|
+
# first set global alignment
|
|
2153
|
+
if headersglobalalign is not None: # if global alignment provided
|
|
2154
|
+
aligns_headers = [headersglobalalign] * len(t_cols)
|
|
2155
|
+
else: # default
|
|
2156
|
+
aligns_headers = aligns or [stralign] * len(headers)
|
|
2157
|
+
# then specific header alignments
|
|
2158
|
+
if headersalign is not None:
|
|
2159
|
+
assert isinstance(headersalign, Iterable)
|
|
2160
|
+
if isinstance(headersalign, str):
|
|
2161
|
+
warnings.warn(
|
|
2162
|
+
f'As a string, `headersalign` is interpreted as {list(headersalign)}. Did you mean `headersglobalalign = "{headersalign}"` or `headersalign = ("{headersalign}",)`?',
|
|
2163
|
+
stacklevel=2,
|
|
2164
|
+
)
|
|
2165
|
+
for idx, align in enumerate(headersalign):
|
|
2166
|
+
hidx = headers_pad + idx
|
|
2167
|
+
if hidx >= len(aligns_headers):
|
|
2168
|
+
break
|
|
2169
|
+
elif align == "same" and hidx < len(aligns): # same as column align
|
|
2170
|
+
aligns_headers[hidx] = aligns[hidx]
|
|
2171
|
+
elif align != "global":
|
|
2172
|
+
aligns_headers[hidx] = align
|
|
2173
|
+
minwidths = [max(minw, max(width_fn(cl) for cl in c)) for minw, c in zip(minwidths, t_cols)]
|
|
2174
|
+
headers = [
|
|
2175
|
+
_align_header(h, a, minw, width_fn(h), is_multiline, width_fn)
|
|
2176
|
+
for h, a, minw in zip(headers, aligns_headers, minwidths)
|
|
2177
|
+
]
|
|
2178
|
+
else:
|
|
2179
|
+
minwidths = [max(width_fn(cl) for cl in c) for c in cols]
|
|
2180
|
+
rows = list(zip(*cols))
|
|
2181
|
+
if not isinstance(tablefmt, TableFormat):
|
|
2182
|
+
tablefmt = _table_formats.get(tablefmt, _table_formats["simple"])
|
|
2183
|
+
|
|
2184
|
+
ra_default = rowalign if isinstance(rowalign, str) else None
|
|
2185
|
+
rowaligns = _expand_iterable(rowalign, len(rows), ra_default)
|
|
2186
|
+
_reinsert_separating_lines(rows, separating_lines)
|
|
2187
|
+
|
|
2188
|
+
return _format_table(
|
|
2189
|
+
tablefmt,
|
|
2190
|
+
headers,
|
|
2191
|
+
aligns_headers,
|
|
2192
|
+
rows,
|
|
2193
|
+
minwidths,
|
|
2194
|
+
aligns,
|
|
2195
|
+
is_multiline,
|
|
2196
|
+
rowaligns=rowaligns,
|
|
2197
|
+
)
|
|
2198
|
+
|
|
2199
|
+
|
|
2200
|
+
def _expand_numparse(disable_numparse, column_count):
|
|
2201
|
+
"""
|
|
2202
|
+
Return a list of bools of length `column_count` which indicates whether number parsing should be used on each
|
|
2203
|
+
column.
|
|
2204
|
+
|
|
2205
|
+
If `disable_numparse` is a list of indices, each of those indices are False,
|
|
2206
|
+
and everything else is True.
|
|
2207
|
+
If `disable_numparse` is a bool, then the returned list is all the same.
|
|
2208
|
+
"""
|
|
2209
|
+
if not isinstance(disable_numparse, Iterable):
|
|
2210
|
+
return [not disable_numparse] * column_count
|
|
2211
|
+
numparses = [True] * column_count
|
|
2212
|
+
for index in disable_numparse:
|
|
2213
|
+
numparses[index] = False
|
|
2214
|
+
return numparses
|
|
2215
|
+
|
|
2216
|
+
|
|
2217
|
+
def _expand_iterable(original, num_desired, default):
|
|
2218
|
+
"""
|
|
2219
|
+
Expands the `original` argument to return a return a list of length `num_desired`.
|
|
2220
|
+
|
|
2221
|
+
If `original` is shorter than `num_desired`, it will
|
|
2222
|
+
be padded with the value in `default`.
|
|
2223
|
+
If `original` is not a list to begin with (i.e. scalar value) a list of
|
|
2224
|
+
length `num_desired` completely populated with `default will be returned
|
|
2225
|
+
"""
|
|
2226
|
+
if isinstance(original, Iterable) and not isinstance(original, str):
|
|
2227
|
+
return original + [default] * (num_desired - len(original))
|
|
2228
|
+
else:
|
|
2229
|
+
return [default] * num_desired
|
|
2230
|
+
|
|
2231
|
+
|
|
2232
|
+
def _pad_row(cells, padding):
|
|
2233
|
+
"""Pads the strings in a list `cells` with spaces on both sides as specified by `padding`."""
|
|
2234
|
+
if cells:
|
|
2235
|
+
pad = " " * padding
|
|
2236
|
+
return [pad + cell + pad for cell in cells]
|
|
2237
|
+
else:
|
|
2238
|
+
return cells
|
|
2239
|
+
|
|
2240
|
+
|
|
2241
|
+
def _build_simple_row(padded_cells, rowfmt):
|
|
2242
|
+
"""Format a row from padded cells using the specified row format."""
|
|
2243
|
+
"Format row according to DataRow format without padding."
|
|
2244
|
+
begin, sep, end = rowfmt
|
|
2245
|
+
return (begin + sep.join(padded_cells) + end).rstrip()
|
|
2246
|
+
|
|
2247
|
+
|
|
2248
|
+
def _build_row(padded_cells, colwidths, colaligns, rowfmt):
|
|
2249
|
+
"""Return a formatted string representing a row of data cells using the specified row format and alignment."""
|
|
2250
|
+
"Return a string which represents a row of data cells."
|
|
2251
|
+
if not rowfmt:
|
|
2252
|
+
return None
|
|
2253
|
+
if hasattr(rowfmt, "__call__"):
|
|
2254
|
+
return rowfmt(padded_cells, colwidths, colaligns)
|
|
2255
|
+
else:
|
|
2256
|
+
return _build_simple_row(padded_cells, rowfmt)
|
|
2257
|
+
|
|
2258
|
+
|
|
2259
|
+
def _append_basic_row(lines, padded_cells, colwidths, colaligns, rowfmt, rowalign=None):
|
|
2260
|
+
"""Append a formatted row to the lines list using specified padding, column widths, alignments, and row format."""
|
|
2261
|
+
lines.append(_build_row(padded_cells, colwidths, colaligns, rowfmt))
|
|
2262
|
+
return lines
|
|
2263
|
+
|
|
2264
|
+
|
|
2265
|
+
def _align_cell_veritically(text_lines, num_lines, column_width, row_alignment):
|
|
2266
|
+
"""Align cell text vertically within a table column based on the specified number of lines, width, and alignment."""
|
|
2267
|
+
delta_lines = num_lines - len(text_lines)
|
|
2268
|
+
blank = [" " * column_width]
|
|
2269
|
+
if row_alignment == "bottom":
|
|
2270
|
+
return blank * delta_lines + text_lines
|
|
2271
|
+
elif row_alignment == "center":
|
|
2272
|
+
top_delta = delta_lines // 2
|
|
2273
|
+
bottom_delta = delta_lines - top_delta
|
|
2274
|
+
return top_delta * blank + text_lines + bottom_delta * blank
|
|
2275
|
+
else:
|
|
2276
|
+
return text_lines + blank * delta_lines
|
|
2277
|
+
|
|
2278
|
+
|
|
2279
|
+
def _append_multiline_row(lines, padded_multiline_cells, padded_widths, colaligns, rowfmt, pad, rowalign=None):
|
|
2280
|
+
"""Append a multiline row to the table, vertically aligning and padding cells based on the provided widths and
|
|
2281
|
+
alignments.
|
|
2282
|
+
"""
|
|
2283
|
+
colwidths = [w - 2 * pad for w in padded_widths]
|
|
2284
|
+
cells_lines = [c.splitlines() for c in padded_multiline_cells]
|
|
2285
|
+
nlines = max(map(len, cells_lines)) # number of lines in the row
|
|
2286
|
+
# vertically pad cells where some lines are missing
|
|
2287
|
+
# cells_lines = [
|
|
2288
|
+
# (cl + [" " * w] * (nlines - len(cl))) for cl, w in zip(cells_lines, colwidths)
|
|
2289
|
+
# ]
|
|
2290
|
+
|
|
2291
|
+
cells_lines = [_align_cell_veritically(cl, nlines, w, rowalign) for cl, w in zip(cells_lines, colwidths)]
|
|
2292
|
+
lines_cells = [[cl[i] for cl in cells_lines] for i in range(nlines)]
|
|
2293
|
+
for ln in lines_cells:
|
|
2294
|
+
padded_ln = _pad_row(ln, pad)
|
|
2295
|
+
_append_basic_row(lines, padded_ln, colwidths, colaligns, rowfmt)
|
|
2296
|
+
return lines
|
|
2297
|
+
|
|
2298
|
+
|
|
2299
|
+
def _build_line(colwidths, colaligns, linefmt):
|
|
2300
|
+
"""Return a string representing a horizontal line formatted with column widths and alignments using the specified
|
|
2301
|
+
format.
|
|
2302
|
+
"""
|
|
2303
|
+
"Return a string which represents a horizontal line."
|
|
2304
|
+
if not linefmt:
|
|
2305
|
+
return None
|
|
2306
|
+
if hasattr(linefmt, "__call__"):
|
|
2307
|
+
return linefmt(colwidths, colaligns)
|
|
2308
|
+
begin, fill, sep, end = linefmt
|
|
2309
|
+
cells = [fill * w for w in colwidths]
|
|
2310
|
+
return _build_simple_row(cells, (begin, sep, end))
|
|
2311
|
+
|
|
2312
|
+
|
|
2313
|
+
def _append_line(lines, colwidths, colaligns, linefmt):
|
|
2314
|
+
"""Append a formatted line to the list of lines based on column widths, alignments, and line format."""
|
|
2315
|
+
lines.append(_build_line(colwidths, colaligns, linefmt))
|
|
2316
|
+
return lines
|
|
2317
|
+
|
|
2318
|
+
|
|
2319
|
+
class JupyterHTMLStr(str):
|
|
2320
|
+
"""Wrap the string with a _repr_html_ method so that Jupyter displays the HTML table."""
|
|
2321
|
+
|
|
2322
|
+
def _repr_html_(self):
|
|
2323
|
+
"""Return the HTML representation of the JupyterHTMLStr object for proper display in Jupyter Notebooks."""
|
|
2324
|
+
return self
|
|
2325
|
+
|
|
2326
|
+
@property
|
|
2327
|
+
def str(self):
|
|
2328
|
+
"""Add a .str property so that the raw string is still accessible."""
|
|
2329
|
+
return self
|
|
2330
|
+
|
|
2331
|
+
|
|
2332
|
+
def _format_table(fmt, headers, headersaligns, rows, colwidths, colaligns, is_multiline, rowaligns):
|
|
2333
|
+
"""Produce a plain-text representation of the table."""
|
|
2334
|
+
lines = []
|
|
2335
|
+
hidden = fmt.with_header_hide if (headers and fmt.with_header_hide) else []
|
|
2336
|
+
pad = fmt.padding
|
|
2337
|
+
headerrow = fmt.headerrow
|
|
2338
|
+
|
|
2339
|
+
padded_widths = [(w + 2 * pad) for w in colwidths]
|
|
2340
|
+
if is_multiline:
|
|
2341
|
+
pad_row = lambda row, _: row # noqa do it later, in _append_multiline_row
|
|
2342
|
+
append_row = partial(_append_multiline_row, pad=pad)
|
|
2343
|
+
else:
|
|
2344
|
+
pad_row = _pad_row
|
|
2345
|
+
append_row = _append_basic_row
|
|
2346
|
+
|
|
2347
|
+
padded_headers = pad_row(headers, pad)
|
|
2348
|
+
padded_rows = [pad_row(row, pad) for row in rows]
|
|
2349
|
+
|
|
2350
|
+
if fmt.lineabove and "lineabove" not in hidden:
|
|
2351
|
+
_append_line(lines, padded_widths, colaligns, fmt.lineabove)
|
|
2352
|
+
|
|
2353
|
+
if padded_headers:
|
|
2354
|
+
append_row(lines, padded_headers, padded_widths, headersaligns, headerrow)
|
|
2355
|
+
if fmt.linebelowheader and "linebelowheader" not in hidden:
|
|
2356
|
+
_append_line(lines, padded_widths, colaligns, fmt.linebelowheader)
|
|
2357
|
+
|
|
2358
|
+
if padded_rows and fmt.linebetweenrows and "linebetweenrows" not in hidden:
|
|
2359
|
+
# initial rows with a line below
|
|
2360
|
+
for row, ralign in zip(padded_rows[:-1], rowaligns):
|
|
2361
|
+
append_row(lines, row, padded_widths, colaligns, fmt.datarow, rowalign=ralign)
|
|
2362
|
+
_append_line(lines, padded_widths, colaligns, fmt.linebetweenrows)
|
|
2363
|
+
# the last row without a line below
|
|
2364
|
+
append_row(
|
|
2365
|
+
lines,
|
|
2366
|
+
padded_rows[-1],
|
|
2367
|
+
padded_widths,
|
|
2368
|
+
colaligns,
|
|
2369
|
+
fmt.datarow,
|
|
2370
|
+
rowalign=rowaligns[-1],
|
|
2371
|
+
)
|
|
2372
|
+
else:
|
|
2373
|
+
separating_line = (
|
|
2374
|
+
fmt.linebetweenrows or fmt.linebelowheader or fmt.linebelow or fmt.lineabove or Line("", "", "", "")
|
|
2375
|
+
)
|
|
2376
|
+
for row in padded_rows:
|
|
2377
|
+
# test to see if either the 1st column or the 2nd column (account for showindex) has
|
|
2378
|
+
# the SEPARATING_LINE flag
|
|
2379
|
+
if _is_separating_line(row):
|
|
2380
|
+
_append_line(lines, padded_widths, colaligns, separating_line)
|
|
2381
|
+
else:
|
|
2382
|
+
append_row(lines, row, padded_widths, colaligns, fmt.datarow)
|
|
2383
|
+
|
|
2384
|
+
if fmt.linebelow and "linebelow" not in hidden:
|
|
2385
|
+
_append_line(lines, padded_widths, colaligns, fmt.linebelow)
|
|
2386
|
+
|
|
2387
|
+
if not headers and not rows:
|
|
2388
|
+
return ""
|
|
2389
|
+
output = "\n".join(lines)
|
|
2390
|
+
return JupyterHTMLStr(output) if fmt.lineabove == _html_begin_table_without_header else output
|
|
2391
|
+
|
|
2392
|
+
|
|
2393
|
+
class _CustomTextWrap(textwrap.TextWrapper):
|
|
2394
|
+
"""
|
|
2395
|
+
A custom implementation of CPython's textwrap.TextWrapper.
|
|
2396
|
+
|
|
2397
|
+
This supports
|
|
2398
|
+
both wide characters (Korea, Japanese, Chinese) - including mixed string.
|
|
2399
|
+
For the most part, the `_handle_long_word` and `_wrap_chunks` functions were
|
|
2400
|
+
copy pasted out of the CPython baseline, and updated with our custom length
|
|
2401
|
+
and line appending logic.
|
|
2402
|
+
"""
|
|
2403
|
+
|
|
2404
|
+
def __init__(self, *args, **kwargs):
|
|
2405
|
+
"""Initialize the wrapper with support for wide characters and custom length logic."""
|
|
2406
|
+
self._active_codes = []
|
|
2407
|
+
self.max_lines = None # For python2 compatibility
|
|
2408
|
+
textwrap.TextWrapper.__init__(self, *args, **kwargs)
|
|
2409
|
+
|
|
2410
|
+
@staticmethod
|
|
2411
|
+
def _len(item):
|
|
2412
|
+
"""Custom len that gets console column width for wide and non-wide characters as well as ignores color codes."""
|
|
2413
|
+
stripped = _strip_ansi(item)
|
|
2414
|
+
return wcwidth.wcswidth(stripped) if wcwidth else len(stripped)
|
|
2415
|
+
|
|
2416
|
+
def _update_lines(self, lines, new_line):
|
|
2417
|
+
"""Adds a new line to the list of lines the text is being wrapped into This function will also track any ANSI
|
|
2418
|
+
color codes in this string as well as add any colors from previous lines order to preserve the same formatting
|
|
2419
|
+
as a single unwrapped string.
|
|
2420
|
+
"""
|
|
2421
|
+
code_matches = list(_ansi_codes.finditer(new_line))
|
|
2422
|
+
color_codes = [code.string[code.span()[0] : code.span()[1]] for code in code_matches]
|
|
2423
|
+
|
|
2424
|
+
# Add color codes from earlier in the unwrapped line, and then track any new ones we add.
|
|
2425
|
+
new_line = "".join(self._active_codes) + new_line
|
|
2426
|
+
|
|
2427
|
+
for code in color_codes:
|
|
2428
|
+
if code != _ansi_color_reset_code:
|
|
2429
|
+
self._active_codes.append(code)
|
|
2430
|
+
else: # A single reset code resets everything
|
|
2431
|
+
self._active_codes = []
|
|
2432
|
+
|
|
2433
|
+
# Always ensure each line is color terminated if any colors are
|
|
2434
|
+
# still active, otherwise colors will bleed into other cells on the console
|
|
2435
|
+
if len(self._active_codes) > 0:
|
|
2436
|
+
new_line = new_line + _ansi_color_reset_code
|
|
2437
|
+
|
|
2438
|
+
lines.append(new_line)
|
|
2439
|
+
|
|
2440
|
+
def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width):
|
|
2441
|
+
"""_handle_long_word(chunks : [string],
|
|
2442
|
+
cur_line : [string],
|
|
2443
|
+
cur_len : int, width : int)
|
|
2444
|
+
Handle a chunk of text (most likely a word, not whitespace) that
|
|
2445
|
+
is too long to fit in any line.
|
|
2446
|
+
"""
|
|
2447
|
+
# Figure out when indent is larger than the specified width, and make
|
|
2448
|
+
# sure at least one character is stripped off on every pass
|
|
2449
|
+
space_left = 1 if width < 1 else width - cur_len
|
|
2450
|
+
# If we're allowed to break long words, then do so: put as much
|
|
2451
|
+
# of the next chunk onto the current line as will fit.
|
|
2452
|
+
if self.break_long_words:
|
|
2453
|
+
# Tabulate Custom: Build the string up piece-by-piece in order to
|
|
2454
|
+
# take each charcter's width into account
|
|
2455
|
+
chunk = reversed_chunks[-1]
|
|
2456
|
+
i = 1
|
|
2457
|
+
while self._len(chunk[:i]) <= space_left:
|
|
2458
|
+
i += 1
|
|
2459
|
+
cur_line.append(chunk[: i - 1])
|
|
2460
|
+
reversed_chunks[-1] = chunk[i - 1 :]
|
|
2461
|
+
|
|
2462
|
+
elif not cur_line:
|
|
2463
|
+
cur_line.append(reversed_chunks.pop())
|
|
2464
|
+
|
|
2465
|
+
# If we're not allowed to break long words, and there's already
|
|
2466
|
+
# text on the current line, do nothing. Next time through the
|
|
2467
|
+
# main loop of _wrap_chunks(), we'll wind up here again, but
|
|
2468
|
+
# cur_len will be zero, so the next line will be entirely
|
|
2469
|
+
# devoted to the long word that we can't handle right now.
|
|
2470
|
+
|
|
2471
|
+
def _wrap_chunks(self, chunks):
|
|
2472
|
+
"""
|
|
2473
|
+
_wrap_chunks(chunks : [string]) -> [string] Wrap a sequence of text chunks and return a list of lines of length
|
|
2474
|
+
'self.width' or less.
|
|
2475
|
+
|
|
2476
|
+
(If 'break_long_words' is false,
|
|
2477
|
+
some lines may be longer than this.) Chunks correspond roughly
|
|
2478
|
+
to words and the whitespace between them: each chunk is
|
|
2479
|
+
indivisible (modulo 'break_long_words'), but a line break can
|
|
2480
|
+
come between any two chunks. Chunks should not have internal
|
|
2481
|
+
whitespace; ie. a chunk is either all whitespace or a "word".
|
|
2482
|
+
Whitespace chunks will be removed from the beginning and end of
|
|
2483
|
+
lines, but apart from that whitespace is preserved.
|
|
2484
|
+
"""
|
|
2485
|
+
lines = []
|
|
2486
|
+
if self.width <= 0:
|
|
2487
|
+
raise ValueError(f"invalid width {self.width!r} (must be > 0)")
|
|
2488
|
+
if self.max_lines is not None:
|
|
2489
|
+
indent = self.subsequent_indent if self.max_lines > 1 else self.initial_indent
|
|
2490
|
+
if self._len(indent) + self._len(self.placeholder.lstrip()) > self.width:
|
|
2491
|
+
raise ValueError("placeholder too large for max width")
|
|
2492
|
+
|
|
2493
|
+
# Arrange in reverse order so items can be efficiently popped
|
|
2494
|
+
# from a stack of chucks.
|
|
2495
|
+
chunks.reverse()
|
|
2496
|
+
|
|
2497
|
+
while chunks:
|
|
2498
|
+
# Start the list of chunks that will make up the current line.
|
|
2499
|
+
# cur_len is just the length of all the chunks in cur_line.
|
|
2500
|
+
cur_line = []
|
|
2501
|
+
cur_len = 0
|
|
2502
|
+
|
|
2503
|
+
# Figure out which static string will prefix this line.
|
|
2504
|
+
indent = self.subsequent_indent if lines else self.initial_indent
|
|
2505
|
+
# Maximum width for this line.
|
|
2506
|
+
width = self.width - self._len(indent)
|
|
2507
|
+
|
|
2508
|
+
# First chunk on line is whitespace -- drop it, unless this
|
|
2509
|
+
# is the very beginning of the text (ie. no lines started yet).
|
|
2510
|
+
if self.drop_whitespace and chunks[-1].strip() == "" and lines:
|
|
2511
|
+
del chunks[-1]
|
|
2512
|
+
|
|
2513
|
+
while chunks:
|
|
2514
|
+
chunk_len = self._len(chunks[-1])
|
|
2515
|
+
|
|
2516
|
+
if cur_len + chunk_len > width:
|
|
2517
|
+
break
|
|
2518
|
+
|
|
2519
|
+
cur_line.append(chunks.pop())
|
|
2520
|
+
cur_len += chunk_len
|
|
2521
|
+
|
|
2522
|
+
# The current line is full, and the next chunk is too big to
|
|
2523
|
+
# fit on *any* line (not just this one).
|
|
2524
|
+
if chunks and self._len(chunks[-1]) > width:
|
|
2525
|
+
self._handle_long_word(chunks, cur_line, cur_len, width)
|
|
2526
|
+
cur_len = sum(map(self._len, cur_line))
|
|
2527
|
+
|
|
2528
|
+
# If the last chunk on this line is all whitespace, drop it.
|
|
2529
|
+
if self.drop_whitespace and cur_line and cur_line[-1].strip() == "":
|
|
2530
|
+
cur_len -= self._len(cur_line[-1])
|
|
2531
|
+
del cur_line[-1]
|
|
2532
|
+
|
|
2533
|
+
if cur_line:
|
|
2534
|
+
if (
|
|
2535
|
+
self.max_lines is None
|
|
2536
|
+
or len(lines) + 1 < self.max_lines
|
|
2537
|
+
or (
|
|
2538
|
+
(not chunks or (self.drop_whitespace and len(chunks) == 1 and not chunks[0].strip()))
|
|
2539
|
+
and cur_len <= width
|
|
2540
|
+
)
|
|
2541
|
+
):
|
|
2542
|
+
# Convert current line back to a string and store it in
|
|
2543
|
+
# list of all lines (return value).
|
|
2544
|
+
self._update_lines(lines, indent + "".join(cur_line))
|
|
2545
|
+
else:
|
|
2546
|
+
while cur_line:
|
|
2547
|
+
if cur_line[-1].strip() and cur_len + self._len(self.placeholder) <= width:
|
|
2548
|
+
cur_line.append(self.placeholder)
|
|
2549
|
+
self._update_lines(lines, indent + "".join(cur_line))
|
|
2550
|
+
break
|
|
2551
|
+
cur_len -= self._len(cur_line[-1])
|
|
2552
|
+
del cur_line[-1]
|
|
2553
|
+
else:
|
|
2554
|
+
if lines:
|
|
2555
|
+
prev_line = lines[-1].rstrip()
|
|
2556
|
+
if self._len(prev_line) + self._len(self.placeholder) <= self.width:
|
|
2557
|
+
lines[-1] = prev_line + self.placeholder
|
|
2558
|
+
break
|
|
2559
|
+
self._update_lines(lines, indent + self.placeholder.lstrip())
|
|
2560
|
+
break
|
|
2561
|
+
|
|
2562
|
+
return lines
|
|
2563
|
+
|
|
2564
|
+
|
|
2565
|
+
def _main():
|
|
2566
|
+
"""\
|
|
2567
|
+
Usage: tabulate [options] [FILE ...].
|
|
2568
|
+
|
|
2569
|
+
Pretty-print tabular data.
|
|
2570
|
+
See also https://github.com/astanin/python-tabulate
|
|
2571
|
+
|
|
2572
|
+
FILE a filename of the file with tabular data;
|
|
2573
|
+
if "-" or missing, read data from stdin.
|
|
2574
|
+
|
|
2575
|
+
Options:
|
|
2576
|
+
|
|
2577
|
+
-h, --help show this message
|
|
2578
|
+
-1, --header use the first row of data as a table header
|
|
2579
|
+
-o FILE, --output FILE print table to FILE (default: stdout)
|
|
2580
|
+
-s REGEXP, --sep REGEXP use a custom column separator (default: whitespace)
|
|
2581
|
+
-F FPFMT, --float FPFMT floating point number format (default: g)
|
|
2582
|
+
-I INTFMT, --int INTFMT integer point number format (default: "")
|
|
2583
|
+
-f FMT, --format FMT set output table format; supported formats:
|
|
2584
|
+
plain, simple, grid, fancy_grid, pipe, orgtbl,
|
|
2585
|
+
rst, mediawiki, html, latex, latex_raw,
|
|
2586
|
+
latex_booktabs, latex_longtable, tsv
|
|
2587
|
+
(default: simple)
|
|
2588
|
+
"""
|
|
2589
|
+
import getopt
|
|
2590
|
+
import sys
|
|
2591
|
+
import textwrap
|
|
2592
|
+
|
|
2593
|
+
usage = textwrap.dedent(_main.__doc__)
|
|
2594
|
+
try:
|
|
2595
|
+
opts, args = getopt.getopt(
|
|
2596
|
+
sys.argv[1:],
|
|
2597
|
+
"h1o:s:F:A:f:",
|
|
2598
|
+
["help", "header", "output", "sep=", "float=", "int=", "align=", "format="],
|
|
2599
|
+
)
|
|
2600
|
+
except getopt.GetoptError as e:
|
|
2601
|
+
print(e)
|
|
2602
|
+
print(usage)
|
|
2603
|
+
sys.exit(2)
|
|
2604
|
+
headers = []
|
|
2605
|
+
floatfmt = _DEFAULT_FLOATFMT
|
|
2606
|
+
intfmt = _DEFAULT_INTFMT
|
|
2607
|
+
colalign = None
|
|
2608
|
+
tablefmt = "simple"
|
|
2609
|
+
sep = r"\s+"
|
|
2610
|
+
outfile = "-"
|
|
2611
|
+
for opt, value in opts:
|
|
2612
|
+
if opt in {"-1", "--header"}:
|
|
2613
|
+
headers = "firstrow"
|
|
2614
|
+
elif opt in {"-o", "--output"}:
|
|
2615
|
+
outfile = value
|
|
2616
|
+
elif opt in {"-F", "--float"}:
|
|
2617
|
+
floatfmt = value
|
|
2618
|
+
elif opt in {"-I", "--int"}:
|
|
2619
|
+
intfmt = value
|
|
2620
|
+
elif opt in {"-C", "--colalign"}:
|
|
2621
|
+
colalign = value.split()
|
|
2622
|
+
elif opt in {"-f", "--format"}:
|
|
2623
|
+
if value not in tabulate_formats:
|
|
2624
|
+
print(f"{value} is not a supported table format")
|
|
2625
|
+
print(usage)
|
|
2626
|
+
sys.exit(3)
|
|
2627
|
+
tablefmt = value
|
|
2628
|
+
elif opt in {"-s", "--sep"}:
|
|
2629
|
+
sep = value
|
|
2630
|
+
elif opt in {"-h", "--help"}:
|
|
2631
|
+
print(usage)
|
|
2632
|
+
sys.exit(0)
|
|
2633
|
+
files = args or [sys.stdin]
|
|
2634
|
+
with sys.stdout if outfile == "-" else open(outfile, "w") as out:
|
|
2635
|
+
for f in files:
|
|
2636
|
+
if f == "-":
|
|
2637
|
+
f = sys.stdin
|
|
2638
|
+
if _is_file(f):
|
|
2639
|
+
_pprint_file(
|
|
2640
|
+
f,
|
|
2641
|
+
headers=headers,
|
|
2642
|
+
tablefmt=tablefmt,
|
|
2643
|
+
sep=sep,
|
|
2644
|
+
floatfmt=floatfmt,
|
|
2645
|
+
intfmt=intfmt,
|
|
2646
|
+
file=out,
|
|
2647
|
+
colalign=colalign,
|
|
2648
|
+
)
|
|
2649
|
+
else:
|
|
2650
|
+
with open(f) as fobj:
|
|
2651
|
+
_pprint_file(
|
|
2652
|
+
fobj,
|
|
2653
|
+
headers=headers,
|
|
2654
|
+
tablefmt=tablefmt,
|
|
2655
|
+
sep=sep,
|
|
2656
|
+
floatfmt=floatfmt,
|
|
2657
|
+
intfmt=intfmt,
|
|
2658
|
+
file=out,
|
|
2659
|
+
colalign=colalign,
|
|
2660
|
+
)
|
|
2661
|
+
|
|
2662
|
+
|
|
2663
|
+
def _pprint_file(fobject, headers, tablefmt, sep, floatfmt, intfmt, file, colalign):
|
|
2664
|
+
"""Pretty prints a tabulated file with specified formatting parameters using the 'tabulate' library."""
|
|
2665
|
+
rows = fobject.readlines()
|
|
2666
|
+
table = [re.split(sep, r.rstrip()) for r in rows if r.strip()]
|
|
2667
|
+
print(
|
|
2668
|
+
tabulate(
|
|
2669
|
+
table,
|
|
2670
|
+
headers,
|
|
2671
|
+
tablefmt,
|
|
2672
|
+
floatfmt=floatfmt,
|
|
2673
|
+
intfmt=intfmt,
|
|
2674
|
+
colalign=colalign,
|
|
2675
|
+
),
|
|
2676
|
+
file=file,
|
|
2677
|
+
)
|
|
2678
|
+
|
|
2679
|
+
|
|
2680
|
+
if __name__ == "__main__":
|
|
2681
|
+
_main()
|