Skip to content

Commit 47b298f

Browse files
committed
refactor(base.py): improve performance for tables with many rows and columns by reducing object creation overhead and using more efficient Python constructs
1 parent 1b5dab3 commit 47b298f

1 file changed

Lines changed: 18 additions & 18 deletions

File tree

base.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"""
1313

1414
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
15-
__version__ = '2025070401'
15+
__version__ = '2025121201'
1616

1717
import collections
1818
import numbers
@@ -277,33 +277,33 @@ def get_table(data, cols, header=None, strip=True, sort_by_key=None, sort_order_
277277
if header:
278278
data.insert(0, dict(zip(cols, header)))
279279

280-
column_widths = collections.OrderedDict()
281-
for idx, row in enumerate(data):
280+
# Process values and calculate column widths in a single pass
281+
processed_rows = []
282+
column_widths = {}
283+
284+
for row in data:
285+
processed_row = {}
282286
for col in cols:
283287
if col not in row:
284288
return f'Unknown column "{col}"'
285289
value = str(row[col])
286290
if strip:
287291
value = value.strip()
288-
data[idx][col] = value
292+
processed_row[col] = value
289293
column_widths[col] = max(column_widths.get(col, 0), len(value))
294+
processed_rows.append(processed_row)
290295

291296
if header:
292297
divider = {col: '-' * width for col, width in column_widths.items()}
293-
data.insert(1, divider)
294-
295-
def generate_lines():
296-
for idx, row in enumerate(data):
297-
parts = []
298-
for col, width in column_widths.items():
299-
fmt = f'{{:<{width}}}'
300-
value = row[col]
301-
if idx == 1:
302-
value = value.replace(' ', '-')
303-
parts.append(fmt.format(value))
304-
yield '-+-'.join(parts) if idx == 1 else ' ! '.join(parts)
305-
306-
return '\n'.join(generate_lines()) + '\n'
298+
processed_rows.insert(1, divider)
299+
300+
# Generate output lines
301+
lines = []
302+
for idx, row in enumerate(processed_rows):
303+
parts = [f'{row[col]:<{column_widths[col]}}' for col in column_widths]
304+
lines.append(('-+-' if idx == 1 else ' ! ').join(parts))
305+
306+
return '\n'.join(lines) + '\n'
307307

308308

309309
def get_worst(state1, state2):

0 commit comments

Comments
 (0)