From d0b0afce94d250efa2f958fda6d3e442321fad5a Mon Sep 17 00:00:00 2001 From: Eike Broda Date: Wed, 12 Apr 2023 16:27:32 +0200 Subject: [PATCH 1/2] Add check if rowIndex and columnIndex for table --- plantsim/table.py | 45 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 11 deletions(-) mode change 100644 => 100755 plantsim/table.py diff --git a/plantsim/table.py b/plantsim/table.py old mode 100644 new mode 100755 index 28630e6..d7c64cb --- a/plantsim/table.py +++ b/plantsim/table.py @@ -22,18 +22,30 @@ def __init__(self, plantsim, table_name): row_count = plantsim.get_value(f'{table_name}.YDim') col_count = plantsim.get_value(f'{table_name}.XDim') + + self.has_col_index = plantsim.get_value(f'{table_name}.ColumnIndex ') + self.has_row_index = plantsim.get_value(f'{table_name}.RowIndex') + + min_col_idx = 0 + if not self.has_col_index: + min_col_idx = 1 + + min_row_idx = 0 + if not self.has_row_index: + min_row_idx = 1 + if row_count > 0 and col_count > 0: - for row_idx in range(row_count + 1): + for row_idx in range(min_row_idx, row_count + 1): row = [] row_coldict = {} - for col_idx in range(col_count + 1): + for col_idx in range(min_col_idx, col_count + 1): cell_value = plantsim.get_value(f'{table_name}[{col_idx}, {row_idx}]') row.append(cell_value) - if row_idx > 0: + if self.has_col_index and row_idx > 0: col_header = self.rows[0][col_idx] row_coldict[col_header] = cell_value self._rows.append(row) - if row_idx > 0: + if self.has_row_index and row_idx > 0: self._rows_coldict.append(row_coldict) @property @@ -47,10 +59,13 @@ def rows(self) -> List[List]: @property def header(self) -> List: """ - Returns header (first table row) + Returns header (first table row if any, else None) :return: list containing header elements """ - return self.rows[0] + if self.has_row_index: + return self.rows[0] + + return None @property def rows_body(self) -> List[List]: @@ -58,20 +73,26 @@ def rows_body(self) -> List[List]: Returns table data row-first indexed without header :return: 2-dim list containing table data row-first indexed without first row """ - return self.rows[1:] + if self.has_row_index: + return self.rows[1:] + + return self.rows @property def rows_coldict(self) -> List[Dict]: """ Returns table data row-first indexed. Each column is a Dict with header as key - :return: list of dictionaries + :return: list of dictionaries or None if no headers """ - return self._rows_coldict + if self.has_col_index: + return self._rows_coldict + + return None @property def row_count(self) -> int: """ - Returns total row count (including header) + Returns total row count (including header if any) :return: row count as integer """ return len(self.rows) @@ -152,7 +173,9 @@ def __str__(self): if self.row_count > 0: texttable = Texttable(200) texttable.add_rows(self.rows) - texttable.set_deco(Texttable.HEADER) + texttable.set_deco(0) + if self.has_row_index: + texttable.set_deco(texttable.HEADER) return texttable.draw() else: return '' From e4e5de956ca045aab9a47e03d342e461edf74a40 Mon Sep 17 00:00:00 2001 From: Eike Broda Date: Wed, 12 Apr 2023 18:04:17 +0200 Subject: [PATCH 2/2] Fix problem with table only with colIndex --- plantsim/table.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/plantsim/table.py b/plantsim/table.py index d7c64cb..cdbe5fc 100755 --- a/plantsim/table.py +++ b/plantsim/table.py @@ -26,13 +26,14 @@ def __init__(self, plantsim, table_name): self.has_col_index = plantsim.get_value(f'{table_name}.ColumnIndex ') self.has_row_index = plantsim.get_value(f'{table_name}.RowIndex') - min_col_idx = 0 - if not self.has_col_index: - min_col_idx = 1 + min_col_idx = 1 + min_row_idx = 1 + + if self.has_col_index: + min_row_idx = 0 - min_row_idx = 0 - if not self.has_row_index: - min_row_idx = 1 + if self.has_row_index: + min_col_idx = 0 if row_count > 0 and col_count > 0: for row_idx in range(min_row_idx, row_count + 1): @@ -42,7 +43,10 @@ def __init__(self, plantsim, table_name): cell_value = plantsim.get_value(f'{table_name}[{col_idx}, {row_idx}]') row.append(cell_value) if self.has_col_index and row_idx > 0: - col_header = self.rows[0][col_idx] + if self.has_row_index: + col_header = self._rows[0][col_idx] + else: + col_header = self._rows[0][col_idx-1] row_coldict[col_header] = cell_value self._rows.append(row) if self.has_row_index and row_idx > 0: