| 211 | |
| 212 | |
| 213 | class Table: |
| 214 | def __init__(self, table_heads): |
| 215 | self.table_heads = table_heads |
| 216 | self.table_len = [] |
| 217 | self.data = [] |
| 218 | self.col_num = len(table_heads) |
| 219 | for head in table_heads: |
| 220 | self.table_len.append(len(head)) |
| 221 | |
| 222 | def add_row(self, row_str): |
| 223 | if not isinstance(row_str, list): |
| 224 | print('The row_str should be a list') |
| 225 | if len(row_str) != self.col_num: |
| 226 | print( |
| 227 | f'The length of row data should be equal the length of table heads, but the data: {len(row_str)} is not equal table heads {self.col_num}' |
| 228 | ) |
| 229 | for i in range(self.col_num): |
| 230 | if len(str(row_str[i])) > self.table_len[i]: |
| 231 | self.table_len[i] = len(str(row_str[i])) |
| 232 | self.data.append(row_str) |
| 233 | |
| 234 | def print_row(self, row): |
| 235 | string = '' |
| 236 | for i in range(self.col_num): |
| 237 | string += '|' + str(row[i]).center(self.table_len[i] + 2) |
| 238 | string += '|' |
| 239 | print(string) |
| 240 | |
| 241 | def print_shelf(self): |
| 242 | string = '' |
| 243 | for length in self.table_len: |
| 244 | string += '+' |
| 245 | string += '-' * (length + 2) |
| 246 | string += '+' |
| 247 | print(string) |
| 248 | |
| 249 | def print_table(self): |
| 250 | self.print_shelf() |
| 251 | self.print_row(self.table_heads) |
| 252 | self.print_shelf() |
| 253 | for data in self.data: |
| 254 | self.print_row(data) |
| 255 | self.print_shelf() |
no outgoing calls
no test coverage detected