@param fmt: list of tuple(heading, key, width) heading: str, column label key: dictionary key to value to print width: int, column width in chars @param sep: string, separation between columns @param ul:
(self, fmt, sep="", ul=None, tl=None, bl=None)
| 53 | "Print a list of dicts as a table" |
| 54 | |
| 55 | def __init__(self, fmt, sep="", ul=None, tl=None, bl=None): |
| 56 | """ |
| 57 | @param fmt: list of tuple(heading, key, width) |
| 58 | heading: str, column label |
| 59 | key: dictionary key to value to print |
| 60 | width: int, column width in chars |
| 61 | @param sep: string, separation between columns |
| 62 | @param ul: string, character to underline column label, or None for no underlining |
| 63 | @param tl: string, character to draw as top line over table, or None |
| 64 | @param bl: string, character to draw as bottom line under table, or None |
| 65 | """ |
| 66 | super(TablePrinter, self).__init__() |
| 67 | fmt = [x + ("left",) if len(x) < 4 else x for x in fmt] |
| 68 | self.fmt = str(sep).join( |
| 69 | "{lb}{0}:{align}{1}{rb}".format( |
| 70 | key, width, lb="{", rb="}", align="<" if alignment == "left" else ">" |
| 71 | ) |
| 72 | for heading, key, width, alignment in fmt |
| 73 | ) |
| 74 | self.head = {key: heading for heading, key, width, alignment in fmt} |
| 75 | self.ul = ( |
| 76 | {key: str(ul) * width for heading, key, width, alignment in fmt} |
| 77 | if ul |
| 78 | else None |
| 79 | ) |
| 80 | self.width = {key: width for heading, key, width, alignment in fmt} |
| 81 | self.tl = ( |
| 82 | {key: str(tl) * width for heading, key, width, alignment in fmt} |
| 83 | if tl |
| 84 | else None |
| 85 | ) |
| 86 | self.bl = ( |
| 87 | {key: str(bl) * width for heading, key, width, alignment in fmt} |
| 88 | if bl |
| 89 | else None |
| 90 | ) |
| 91 | |
| 92 | def row(self, data, separation_character=False): |
| 93 | if separation_character: |