sykora / tablefmt
- Source
- Commits
- Network (0)
- Issues (0)
- Downloads (0)
- Wiki (1)
- Graphs
-
Tree:
54efebb
commit 54efebbda89c207eddbadf2b0f8ce7fea99b26d6
tree 01f66c6b8338c21c0df52e0a0e7d8d4b5a855447
parent 730867b66938574cc2a44a0bd19dd4ff478c9150
tree 01f66c6b8338c21c0df52e0a0e7d8d4b5a855447
parent 730867b66938574cc2a44a0bd19dd4ff478c9150
tablefmt / tablefmt.py
| 29f06ede » | sykora | 2008-10-18 | 1 | # Table formatting filter. | |
| 2 | # P.C. Shyamshankar <sykora@lucentbeing.com> | ||||
| 3 | # http://lucentbeing.com | ||||
| 4 | |||||
| 54efebbd » | sykora | 2008-10-18 | 5 | from optparse import OptionParser | |
| 6 | from sys import argv | ||||
| 29f06ede » | sykora | 2008-10-18 | 7 | ||
| 8 | def formatter(lines, inputDelimiter, outputDelimiter) : | ||||
| 54efebbd » | sykora | 2008-10-18 | 9 | # Split lines based on inputDelimiter. | |
| 10 | |||||
| 11 | lines = [filter(None, line.split(inputDelimiter)) for line in lines] | ||||
| 12 | |||||
| 13 | |||||
| 29f06ede » | sykora | 2008-10-18 | 14 | # Assuming all rows are the same length, | |
| 15 | numColumns = len(lines[0]) | ||||
| 16 | widths = [] | ||||
| 17 | |||||
| 18 | # Size of column = size of the largest entry in that column, + 1 | ||||
| 19 | # TODO: Add delimiter support. | ||||
| 20 | for i in range(numColumns) : | ||||
| 21 | widths.append(max(len(line[i]) for line in lines)) | ||||
| 22 | |||||
| 23 | formatList = ["%%%ds"] * numColumns | ||||
| 24 | |||||
| 25 | # There is probably a more efficient way (Time and Space) of doing this. | ||||
| 26 | output = [] | ||||
| 27 | for i in range(len(lines)) : | ||||
| 28 | outputLine = formatList[:] | ||||
| 29 | outputLine = [((s % widths[j]) % lines[i][j]) | ||||
| 30 | for j, s in enumerate(formatList)] | ||||
| 31 | output.append(outputDelimiter.join(outputLine)) | ||||
| 32 | |||||
| 33 | return output | ||||
| 34 | |||||
| 54efebbd » | sykora | 2008-10-18 | 35 | def parseOptions(argv) : | |
| 36 | parser = OptionParser() | ||||
| 37 | |||||
| 38 | parser.add_option('-i', '--input-delimiter', | ||||
| 39 | action = 'store', | ||||
| 40 | type = 'string', | ||||
| 41 | dest = 'inputDelimiter', | ||||
| 42 | default = ' ') | ||||
| 43 | |||||
| 44 | parser.add_option('-o', '--output-delimiter', | ||||
| 45 | action = 'store', | ||||
| 46 | type = 'string', | ||||
| 47 | dest = 'outputDelimiter', | ||||
| 48 | default = ' ') | ||||
| 49 | |||||
| 50 | options, args = parser.parse_args(argv[1:]) | ||||
| 51 | |||||
| 52 | return options | ||||
| 53 | |||||
| 54 | |||||
| 29f06ede » | sykora | 2008-10-18 | 55 | def main() : | |
| 54efebbd » | sykora | 2008-10-18 | 56 | options = parseOptions(argv) | |
| 29f06ede » | sykora | 2008-10-18 | 57 | # Get input. | |
| 58 | lines = [] | ||||
| 59 | |||||
| 60 | try : | ||||
| 61 | line = raw_input() | ||||
| 62 | except EOFError : | ||||
| 63 | return | ||||
| 64 | |||||
| 65 | while True: | ||||
| 54efebbd » | sykora | 2008-10-18 | 66 | lines.append(line) | |
| 29f06ede » | sykora | 2008-10-18 | 67 | try : | |
| 68 | line = raw_input() | ||||
| 69 | except EOFError : | ||||
| 70 | break | ||||
| 71 | |||||
| 54efebbd » | sykora | 2008-10-18 | 72 | formattedLines = formatter(lines, | |
| 73 | options.inputDelimiter, | ||||
| 74 | options.outputDelimiter) | ||||
| 29f06ede » | sykora | 2008-10-18 | 75 | ||
| 76 | for line in formattedLines : | ||||
| 77 | print line | ||||
| 78 | |||||
| 79 | if __name__ == '__main__' : | ||||
| 80 | main() | ||||
| 81 | |||||
