| name | format-statistics-table |
| description | For result reporting: tabular output, aligned columns, statistics summaries, human-readable reports. |
format-statistics-table
When to Use
- Reporting test results
- Showing statistics
- Comparing alternatives
- Human inspection of data
When NOT to Use
- Machine-parseable output (use JSON/CSV)
- Simple single values
- Too many columns
The Pattern
Format data as aligned columns with headers and separators.
def print_table(headers, rows, widths=None):
"""Print formatted table."""
if widths is None:
widths = [max(len(str(row[i])) for row in [headers] + rows)
for i in range(len(headers))]
print(' | '.join(h.ljust(w) for h, w in zip(headers, widths)))
print('-+-'.join('-' * w for w in widths))
for row in rows:
print(' | '.join(str(v).ljust(w) for v, w in zip(row, widths)))
Example (from pytudes)
def show(tallies, label):
"""Print out the counts."""
print()
print('Size | Sets | NoSets | Set:NoSet ratio for', label)
print('-----+--------+--------+----------------')
for size in sorted(tallies):
y, n = tallies[size][True], tallies[size][False]
ratio = ('inft' if n == 0 else int(round(float(y) / n)))
print('{:4d} |{:7,d} |{:7,d} | {:4}:1'
.format(size, y, n, ratio))
def solve_all(grids, name=''):
"""Report solution statistics."""
times, results = zip(*[time_solve(grid) for grid in grids])
N = len(results)
if N > 1:
print("Solved %d of %d %s puzzles "
"(avg %.2f secs (%d Hz), max %.2f secs)." % (
sum(results), N, name,
sum(times)/N, N/sum(times), max(times)))
print('{:.0%} of {} correct ({:.0%} unknown) at {:.0f} words per second'
.format(good/n, n, unknown/n, n/dt))
Key Principles
- Align columns: Consistent widths
- Format numbers: Commas, percentages, decimals
- Headers and separators: Visual structure
- Edge cases: Handle zero, infinity
- Named columns: Clear what each represents