A table class to create table rows, populated by table cells. Example: # Row from list row = SimpleTableRow(['Hello,', 'world!']) # Row from SimpleTableCell cell1 = SimpleTableCell('Hello,') cell2 = SimpleTableCell('world!') row = SimpleTableRow([cell1, cell2])
| 128 | |
| 129 | |
| 130 | class SimpleTableRow: |
| 131 | """A table class to create table rows, populated by table cells. |
| 132 | |
| 133 | Example: |
| 134 | # Row from list |
| 135 | row = SimpleTableRow(['Hello,', 'world!']) |
| 136 | |
| 137 | # Row from SimpleTableCell |
| 138 | cell1 = SimpleTableCell('Hello,') |
| 139 | cell2 = SimpleTableCell('world!') |
| 140 | row = SimpleTableRow([cell1, cell2]) |
| 141 | """ |
| 142 | |
| 143 | def __init__(self, cells=None, header=False): |
| 144 | """Table row constructor. |
| 145 | |
| 146 | Keyword arguments: |
| 147 | cells -- iterable of SimpleTableCell (default None) |
| 148 | header -- flag to indicate this row is a header row. |
| 149 | if the cells are SimpleTableCell, it is the programmer's |
| 150 | responsibility to verify whether it was created with the |
| 151 | header flag set to True. |
| 152 | """ |
| 153 | cells = cells or [] |
| 154 | if isinstance(cells[0], SimpleTableCell): |
| 155 | self.cells = cells |
| 156 | else: |
| 157 | self.cells = [SimpleTableCell(cell, header=header) for cell in cells] |
| 158 | |
| 159 | self.header = header |
| 160 | |
| 161 | def __str__(self): |
| 162 | """Return the HTML code for the table row and its cells as a string.""" |
| 163 | row = [] |
| 164 | |
| 165 | row.append("<tr>") |
| 166 | |
| 167 | for cell in self.cells: |
| 168 | row.append(str(cell)) |
| 169 | |
| 170 | row.append("</tr>") |
| 171 | |
| 172 | return "\n".join(row) |
| 173 | |
| 174 | def __iter__(self): |
| 175 | """Iterate through row cells""" |
| 176 | yield from self.cells |
| 177 | |
| 178 | def add_cell(self, cell): |
| 179 | """Add a SimpleTableCell object to the list of cells.""" |
| 180 | self.cells.append(cell) |
| 181 | |
| 182 | def add_cells(self, cells): |
| 183 | """Add a list of SimpleTableCell objects to the list of cells.""" |
| 184 | for cell in cells: |
| 185 | self.cells.append(cell) |
| 186 | |
| 187 |
no outgoing calls
no test coverage detected