colmi_r02_client.pretty_print
Utility class for printing lists of lists, lists of dicts and lists of dataclasses
1""" 2Utility class for printing lists of lists, lists of dicts and lists of dataclasses 3""" 4 5from typing import Any 6import dataclasses 7 8 9def print_lists(rows: list[list[Any]], header: bool = False) -> str: 10 widths = [0] * len(rows[0]) 11 for row in rows: 12 for i, col in enumerate(row): 13 widths[i] = max(len(str(col)), widths[i]) 14 result = [] 15 for row in rows: 16 pretty_row = [] 17 for i, col in enumerate(row): 18 x = str(col).rjust(widths[i]) 19 pretty_row.append(x) 20 result.append(" | ".join(pretty_row)) 21 22 if header: 23 sep = "-" * len(result[0]) 24 result.insert(1, sep) 25 26 return "\n".join(result) 27 28 29def print_dicts(rows: list[dict]) -> str: 30 lists = [list(rows[0].keys())] 31 lists.extend(list(x.values()) for x in rows) 32 return print_lists(lists, header=True) 33 34 35def print_dataclasses(dcs: list[Any]) -> str: 36 dicted = [dataclasses.asdict(d) for d in dcs] 37 return print_dicts(dicted)
def
print_lists(rows: list[list[typing.Any]], header: bool = False) -> str:
10def print_lists(rows: list[list[Any]], header: bool = False) -> str: 11 widths = [0] * len(rows[0]) 12 for row in rows: 13 for i, col in enumerate(row): 14 widths[i] = max(len(str(col)), widths[i]) 15 result = [] 16 for row in rows: 17 pretty_row = [] 18 for i, col in enumerate(row): 19 x = str(col).rjust(widths[i]) 20 pretty_row.append(x) 21 result.append(" | ".join(pretty_row)) 22 23 if header: 24 sep = "-" * len(result[0]) 25 result.insert(1, sep) 26 27 return "\n".join(result)
def
print_dicts(rows: list[dict]) -> str:
def
print_dataclasses(dcs: list[typing.Any]) -> str: