Skip to content

Commit 08e1e2f

Browse files
Improve string concatenation
Avoid temporary array.
1 parent 74885be commit 08e1e2f

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

tabulate/__init__.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,10 @@ def _pipe_line_with_colons(colwidths, colaligns):
142142
alignment (as in `pipe` output format)."""
143143
if not colaligns: # e.g. printing an empty data frame (github issue #15)
144144
colaligns = [""] * len(colwidths)
145-
segments = [_pipe_segment_with_colons(a, w) for a, w in zip(colaligns, colwidths)]
146-
return "|" + "|".join(segments) + "|"
145+
segments = "|".join(
146+
_pipe_segment_with_colons(a, w) for a, w in zip(colaligns, colwidths)
147+
)
148+
return f"|{segments}|"
147149

148150

149151
def _grid_segment_with_colons(colwidth, align):
@@ -165,8 +167,10 @@ def _grid_line_with_colons(colwidths, colaligns):
165167
in a grid table."""
166168
if not colaligns:
167169
colaligns = [""] * len(colwidths)
168-
segments = [_grid_segment_with_colons(w, a) for a, w in zip(colaligns, colwidths)]
169-
return "+" + "+".join(segments) + "+"
170+
segments = "+".join(
171+
_grid_segment_with_colons(w, a) for a, w in zip(colaligns, colwidths)
172+
)
173+
return f"+{segments}+"
170174

171175

172176
def _mediawiki_row_with_attrs(separator, cell_values, colwidths, colaligns):
@@ -188,8 +192,10 @@ def _mediawiki_row_with_attrs(separator, cell_values, colwidths, colaligns):
188192
def _textile_row_with_attrs(cell_values, colwidths, colaligns):
189193
cell_values[0] += " "
190194
alignment = {"left": "<.", "right": ">.", "center": "=.", "decimal": ">."}
191-
values = (alignment.get(a, "") + v for a, v in zip(colaligns, cell_values))
192-
return "|" + "|".join(values) + "|"
195+
values = "|".join(
196+
alignment.get(a, "") + v for a, v in zip(colaligns, cell_values)
197+
)
198+
return f"|{values}|"
193199

194200

195201
def _html_begin_table_without_header(colwidths_ignore, colaligns_ignore):
@@ -258,10 +264,10 @@ def make_header_line(is_header, colwidths, colaligns):
258264
asciidoc_alignments = zip(
259265
colwidths, [alignment[colalign] for colalign in colaligns]
260266
)
261-
asciidoc_column_specifiers = [
267+
asciidoc_column_specifiers = ",".join(
262268
f"{width:d}{align}" for width, align in asciidoc_alignments
263-
]
264-
header_list = ['cols="' + (",".join(asciidoc_column_specifiers)) + '"']
269+
)
270+
header_list = [f'cols="{asciidoc_column_specifiers}"']
265271

266272
# generate the list of options (currently only "header")
267273
options_list = []
@@ -270,7 +276,8 @@ def make_header_line(is_header, colwidths, colaligns):
270276
options_list.append("header")
271277

272278
if options_list:
273-
header_list += ['options="' + ",".join(options_list) + '"']
279+
options_list = ",".join(options_list)
280+
header_list.append(f'options="{options_list}"')
274281

275282
# generate the list of entries in the table header field
276283

0 commit comments

Comments
 (0)