Display rows & columns in ASCII tables
# /usr/bin/env python
# -*- coding: utf-8 -*-
"""
sq.texttable
~~~~~~~~~~~~
Creates simple ASCII tables
Based on texttables by Gerome Fournier
..'https://pypi.python.org/pypi/texttable/0.8.1'_
TextTable features not yet implemented:
1. Horizontal Alignment within cell (left, center, right).
Currently set to left.
2. Vertical Alignment within cell (top, middle, bottom).
Currently set to top.
3. Set column width.
Currently set to auto.
4. Set precision on floats & exponential values
5. Values must be strings
6. Borders are a place holder for future development, not currently implemented
"""
class Border:
"""
Line 1: +===========+===========+
Line 2: | HEADER1 | HEADER2 |
Line 3: +===========+===========+
Line 4: | ROW1-COL1 | ROW1-COL2 |
Line 5: +-----------+-----------+
Line 6: | ROW2-COL1 | ROW2-COL2 |
Line 7: +===========+===========+
LINE1: CORNER + TOP_BOTTOM_BORDER*11 + TOP_BOTTOM_JOIN
+ TOP_BOTTOM_BORDER*11 + CORNER
LINE2: LEFT_RIGHT_BORDER + ' '*PADDING + 'HEADER1' + ' '*PADDING
+ COLUMN_SEPERATOR + ' '*PADDING + 'HEADER2' + ' '*PADDING
+ LEFT_RIGHT_BORDER
LINE3: RIGHT_LEFT_JOIN + BORDER_HEADER*11 + INNER_JOIN +
HEADER_SEPARATOR*11 + RIGHT_LEFT_JOIN
LINE4: LEFT_RIGHT_BORDER + ' '*PADDING + 'ROW1-COL1' + ' '*PADDING
+ COL_SEPERATOR + ' '*PADDING + 'ROW1-COL2' + ' '*PADDING
+ LEFT_RIGHT_BORDER
LINE5: RIGHT_LEFT_JOIN + ROW_SEPERATOR*11 + INNER_JOIN +
ROW_SEPERATOR*11 + RIGHT_LEFT_JOIN
LINE 6: Similar to line 4
LINE 7: Same as line 1
"""
# Border characters
TOP_BOTTOM_BORDER, LEFT_RIGHT_BORDER = '_', '|'
# Seperator characters
HEADER_SEPARATOR, ROW_SEPERATOR, COLUMN_SEPARATOR = '=', '-', '|'
# Join (intesection) characters
INNER_JOIN, TOP_BOTTOM_JOIN, RIGHT_LEFT_JOIN = '+', '+', '+'
CORNER = '+'
class TextTable:
def __init__(self, table_data=None, has_header=None, has_border=None,
row_separator='_', column_separator='|'):
self.rows = []
self.has_header = has_header
self.borders = Border() if has_border else None
self.row_separator = row_separator
self.column_seperator = column_separator
if table_data:
self.add_rows(table_data)
self.padding = ' '
def draw(self, rows):
cols = list(zip(*rows))
for colnum, col in enumerate(cols):
width = max(len(i) for i in col)
cols[colnum] = [i.ljust(width) for i in col]
for row in zip(*cols):
print(self.padding.join(row))
def texttable(matrix):
t = TextTable()
t.draw(matrix)