|
12 | 12 | """ |
13 | 13 |
|
14 | 14 | __author__ = 'Linuxfabrik GmbH, Zurich/Switzerland' |
15 | | -__version__ = '2025070401' |
| 15 | +__version__ = '2025121201' |
16 | 16 |
|
17 | 17 | import collections |
18 | 18 | import numbers |
@@ -277,33 +277,33 @@ def get_table(data, cols, header=None, strip=True, sort_by_key=None, sort_order_ |
277 | 277 | if header: |
278 | 278 | data.insert(0, dict(zip(cols, header))) |
279 | 279 |
|
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 = {} |
282 | 286 | for col in cols: |
283 | 287 | if col not in row: |
284 | 288 | return f'Unknown column "{col}"' |
285 | 289 | value = str(row[col]) |
286 | 290 | if strip: |
287 | 291 | value = value.strip() |
288 | | - data[idx][col] = value |
| 292 | + processed_row[col] = value |
289 | 293 | column_widths[col] = max(column_widths.get(col, 0), len(value)) |
| 294 | + processed_rows.append(processed_row) |
290 | 295 |
|
291 | 296 | if header: |
292 | 297 | 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' |
307 | 307 |
|
308 | 308 |
|
309 | 309 | def get_worst(state1, state2): |
|
0 commit comments