From 41539a2769bae9437b7871bd580a152ac04b933d Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Tue, 22 Jun 2021 04:15:55 +0200 Subject: [PATCH] [enh] allow Table/TableDict to have a row_function --- moulinette/__init__.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/moulinette/__init__.py b/moulinette/__init__.py index b73eb7b9..e73853f6 100755 --- a/moulinette/__init__.py +++ b/moulinette/__init__.py @@ -65,10 +65,11 @@ console.format_exception = _format_exception class Table: - def __init__(self, data, columns=None, title=None): + def __init__(self, data, columns=None, title=None, row_function=None): self.data = data self.columns = columns self.title = title + self.row_function = row_function def print(self): if not self.data: @@ -106,7 +107,11 @@ class Table: for column in self.columns: values.append(row.get(column, "")) - table.add_row(*values) + if self.row_function is not None: + # this function is responsible for adding the row + self.row_function(table=table, columns=self.columns, row=row, values=values) + else: + table.add_row(*values) class TableForDict(Table): @@ -139,8 +144,12 @@ class TableForDict(Table): if column != self.key: row_values.append(str(values.get(column, ""))) - table.add_row(*row_values) - + if self.row_function is not None: + # this function is responsible for adding the row + self.row_function(table=table, columns=self.columns, row=(key, values), + values=row_values) + else: + table.add_row(*row_values) # Package functions