MCPcopy Create free account
hub / github.com/It4innovations/hyperqueue / Table

Class Table

tests/utils/table.py:6–77  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4
5
6class Table:
7 def __init__(self, rows: List[List[str]], header: Optional[List[str]]):
8 self.rows = rows
9 self.header = header
10
11 def __getitem__(self, item):
12 if isinstance(item, slice):
13 return Table(self.rows[item], header=self.header)
14 return self.rows[item]
15
16 def __iter__(self):
17 yield from self.rows
18
19 def as_horizontal(self) -> "Table":
20 if self.header is not None:
21 return self
22 assert self.rows
23 return Table(self.rows[1:], self.rows[0])
24
25 def get_row_value(self, key) -> Optional[str]:
26 """
27 Assumes vertical table (each value has a separate row).
28 """
29 row = [r for r in self.rows if r and r[0] == key]
30 if not row:
31 return None
32 assert len(row) == 1
33 return row[0][1]
34
35 def check_row_value(self, key: str, value: str):
36 row = self.get_row_value(key)
37 if row is None:
38 raise Exception(f"Key `{key!r}` not found in\n{self}")
39 assert row == value
40
41 def get_column_value(self, key: str) -> Optional[List[str]]:
42 """
43 Assumes horizontal table (each value has a separate column).
44 """
45 if self.header is None:
46 raise Exception(f"This table is not horizontal!\n{self}")
47 if key not in self.header:
48 return None
49
50 index = self.header.index(key)
51 return [row[index] for row in self.rows]
52
53 def check_column_value(self, key: str, index: int, value: str):
54 column = self.get_column_value(key)
55 if not column:
56 raise Exception(f"Value for key `{key!r}` not found in\n{self}")
57 row = column[index]
58 assert row == value
59
60 def check_columns_value(self, keys: List[str], index: int, values: List[str]):
61 assert len(keys) == len(values)
62 for key, val in zip(keys, values):
63 self.check_column_value(key, index, val)

Callers 3

__getitem__Method · 0.85
as_horizontalMethod · 0.85
parse_tableFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected