From 786b07f54484df7e55595ac6808542e5bd04e289 Mon Sep 17 00:00:00 2001 From: Sven Augustin Date: Mon, 23 Aug 2021 15:17:13 +0200 Subject: [PATCH] added printable_table --- slic/utils/printing.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/slic/utils/printing.py b/slic/utils/printing.py index a2be29b5d..c86da3133 100644 --- a/slic/utils/printing.py +++ b/slic/utils/printing.py @@ -49,3 +49,35 @@ def itemize(iterable, header=None, bullet="-"): +def printable_table(data, labels=None): + res = [] + + if labels: + data = [labels] + data + + cols = zip(*data) + widths = [maxstrlen(c) for c in cols] + + formatted_data = _fmt_table_data(data, widths) + res.extend(formatted_data) + + if labels: + sep = _fmt_label_sep(widths, line="-") + res.insert(1, sep) # insert behind labels + + return "\n".join(res) + + +def _fmt_table_data(data, widths): + return (_fmt_table_line(entries, widths) for entries in data) + +def _fmt_table_line(entries, widths): + res = (str(c).rjust(w) for c, w in zip(entries, widths)) + return " ".join(res) + +def _fmt_label_sep(widths, line="-"): + res = (line * w for w in widths) + return " ".join(res) + + +