Prints a string where the rows in the datasheet are organized in outlined columns.
(datasheet, truncate=40, padding=" ", fill=".")
| 2180 | _truncate = truncate |
| 2181 | |
| 2182 | def pprint(datasheet, truncate=40, padding=" ", fill="."): |
| 2183 | """ Prints a string where the rows in the datasheet are organized in outlined columns. |
| 2184 | """ |
| 2185 | # Calculate the width of each column, based on the longest field in each column. |
| 2186 | # Long fields can be split across different lines, so we need to check each line. |
| 2187 | w = [0 for column in datasheet.columns] |
| 2188 | R = [] |
| 2189 | for i, row in enumerate(datasheet.rows): |
| 2190 | fields = [] |
| 2191 | for j, v in enumerate(row): |
| 2192 | # Cast each field in the row to a string. |
| 2193 | # Strings that span beyond the maximum column width are wrapped. |
| 2194 | # Thus, each "field" in the row is a list of lines. |
| 2195 | head, tail = _truncate(decode_utf8(v), truncate) |
| 2196 | lines = [] |
| 2197 | lines.append(head) |
| 2198 | w[j] = max(w[j], len(head)) |
| 2199 | while len(tail) > 0: |
| 2200 | head, tail = _truncate(tail, truncate) |
| 2201 | lines.append(head) |
| 2202 | w[j] = max(w[j], len(head)) |
| 2203 | fields.append(lines) |
| 2204 | R.append(fields) |
| 2205 | for i, fields in enumerate(R): |
| 2206 | # Add empty lines to each field so they are of equal height. |
| 2207 | n = max([len(lines) for lines in fields]) |
| 2208 | fields = [lines+[""] * (n-len(lines)) for lines in fields] |
| 2209 | # Print the row line per line, justifying the fields with spaces. |
| 2210 | for k in range(n): |
| 2211 | for j, lines in enumerate(fields): |
| 2212 | s = lines[k] |
| 2213 | s += ((k==0 or len(lines[k]) > 0) and fill or " ") * (w[j] - len(lines[k])) |
| 2214 | s += padding |
| 2215 | print s, |
| 2216 |
no test coverage detected
searching dependent graphs…