| 46 | self.own_file = False |
| 47 | |
| 48 | def writekvs(self, kvs): |
| 49 | # Create strings for printing |
| 50 | key2str = {} |
| 51 | for (key, val) in sorted(kvs.items()): |
| 52 | if hasattr(val, "__float__"): |
| 53 | valstr = "%-8.3g" % val |
| 54 | else: |
| 55 | valstr = str(val) |
| 56 | key2str[self._truncate(key)] = self._truncate(valstr) |
| 57 | |
| 58 | # Find max widths |
| 59 | if len(key2str) == 0: |
| 60 | print("WARNING: tried to write empty key-value dict") |
| 61 | return |
| 62 | else: |
| 63 | keywidth = max(map(len, key2str.keys())) |
| 64 | valwidth = max(map(len, key2str.values())) |
| 65 | |
| 66 | # Write out the data |
| 67 | dashes = "-" * (keywidth + valwidth + 7) |
| 68 | lines = [dashes] |
| 69 | for (key, val) in sorted(key2str.items(), key=lambda kv: kv[0].lower()): |
| 70 | lines.append( |
| 71 | "| %s%s | %s%s |" |
| 72 | % (key, " " * (keywidth - len(key)), val, " " * (valwidth - len(val))) |
| 73 | ) |
| 74 | lines.append(dashes) |
| 75 | self.file.write("\n".join(lines) + "\n") |
| 76 | |
| 77 | # Flush the output to the file |
| 78 | self.file.flush() |
| 79 | |
| 80 | def _truncate(self, s): |
| 81 | maxlen = 30 |