def test_height(): """Test height of multi-line cells.""" assert 'test\n ' == align_and_pad_cell('test', 'left', 4, 2, 0, 0) assert 'test\n ' == align_and_pad_cell('test\n', 'left', 4, 1, 0, 0) assert 'test\n ' == align_and_pad_cell('test\n', 'left', 4, 2, 0, 0) assert 'test\n \n ' == align_and_pad_cell('test\n', 'left', 4, 3, 0, 0) assert ' test \n \n ' == align_and_pad_cell('test\n', 'left', 4, 3, 1, 1)
def test_multi_line(): """Test multi-line support.""" assert align_and_pad_cell('test\n', 'left', (8, 2, 1, 1)) == ' test \n ' assert align_and_pad_cell('\ntest', 'left', (8, 2, 1, 1)) == ' \n test ' assert align_and_pad_cell( '\ntest\n', 'left', (8, 3, 1, 1)) == ' \n test \n '
def test_height(): """Test height of multi-line cells.""" assert align_and_pad_cell('test', 'left', (4, 2, 0, 0)) == 'test\n ' assert align_and_pad_cell('test\n', 'left', (4, 1, 0, 0)) == 'test\n ' assert align_and_pad_cell('test\n', 'left', (4, 2, 0, 0)) == 'test\n ' assert align_and_pad_cell('test\n', 'left', (4, 3, 0, 0)) == 'test\n \n ' assert align_and_pad_cell('test\n', 'left', (4, 3, 1, 1)) == ' test \n \n '
def padded_table_data(self): """Return a list of lists of strings. It's self.table_data but with the cells padded with spaces and newlines. Most of the work in this class is done here. """ if not self.table_data: return list() # Set all rows to the same number of columns. max_columns = max(len(r) for r in self.table_data) new_table_data = [ r + [''] * (max_columns - len(r)) for r in self.table_data ] # Pad strings in each cell, and apply text-align/justification. widths = self.column_widths for row in new_table_data: height = max([c.count('\n') for c in row] or [0]) + 1 for i in range(len(row)): align = self.justify_columns.get(i, 'left') cell = align_and_pad_cell( row[i], align, (widths[i], height, self.padding_left, self.padding_right)) row[i] = cell return new_table_data
def gen_row_lines(self, row, style, inner_widths, height): r"""Combine cells in row and group them into lines with vertical borders. Caller is expected to pass yielded lines to ''.join() to combine them into a printable line. Caller must append newline character to the end of joined line. In: ['Row One Column One', 'Two', 'Three'] Out: [ ('|', ' Row One Column One ', '|', ' Two ', '|', ' Three ', '|'), ] In: ['Row One\nColumn One', 'Two', 'Three'], Out: [ ('|', ' Row One ', '|', ' Two ', '|', ' Three ', '|'), ('|', ' Column One ', '|', ' ', '|', ' ', '|'), ] :param iter row: One row in the table. List of cells. :param str style: Type of border characters to use. :param iter inner_widths: List of widths (no padding) for each column. :param int height: Inner height (no padding) (number of lines) to expand row to. :return: Yields lines split into components in a list. Caller must ''.join() line. """ cells_in_row = list() # Resize row if it doesn't have enough cells. if len(row) != len(inner_widths): row = row + [''] * (len(inner_widths) - len(row)) # Pad and align each cell. Split each cell into lines to support multi-line cells. for i, cell in enumerate(row): align = (self.justify_columns.get(i),) inner_dimensions = (inner_widths[i], height) padding = (self.padding_left, self.padding_right, 0, 0) cells_in_row.append( align_and_pad_cell(cell, align, inner_dimensions, padding, horizontal_align=self.horizontal_align, vertical_align=self.vertical_align)) # Determine border characters. if style == 'heading': left = self.CHAR_H_OUTER_LEFT_VERTICAL if self.outer_border else '' center = self.CHAR_H_INNER_VERTICAL if self.inner_column_border else '' right = self.CHAR_H_OUTER_RIGHT_VERTICAL if self.outer_border else '' elif style == 'footing': left = self.CHAR_F_OUTER_LEFT_VERTICAL if self.outer_border else '' center = self.CHAR_F_INNER_VERTICAL if self.inner_column_border else '' right = self.CHAR_F_OUTER_RIGHT_VERTICAL if self.outer_border else '' else: left = self.CHAR_OUTER_LEFT_VERTICAL if self.outer_border else '' center = self.CHAR_INNER_VERTICAL if self.inner_column_border else '' right = self.CHAR_OUTER_RIGHT_VERTICAL if self.outer_border else '' # Yield each line. for line in build_row(cells_in_row, left, center, right): yield line
def gen_row_lines(self, row, style, inner_widths, height): r"""Combine cells in row and group them into lines with vertical borders. Caller is expected to pass yielded lines to ''.join() to combine them into a printable line. Caller must append newline character to the end of joined line. In: ['Row One Column One', 'Two', 'Three'] Out: [ ('|', ' Row One Column One ', '|', ' Two ', '|', ' Three ', '|'), ] In: ['Row One\nColumn One', 'Two', 'Three'], Out: [ ('|', ' Row One ', '|', ' Two ', '|', ' Three ', '|'), ('|', ' Column One ', '|', ' ', '|', ' ', '|'), ] :param iter row: One row in the table. List of cells. :param str style: Type of border characters to use. :param iter inner_widths: List of widths (no padding) for each column. :param int height: Inner height (no padding) (number of lines) to expand row to. :return: Yields lines split into components in a list. Caller must ''.join() line. """ cells_in_row = list() # Resize row if it doesn't have enough cells. if len(row) != len(inner_widths): row = row + [''] * (len(inner_widths) - len(row)) # Pad and align each cell. Split each cell into lines to support multi-line cells. for i, cell in enumerate(row): align = (self.justify_columns.get(i), ) inner_dimensions = (inner_widths[i], height) padding = (self.padding_left, self.padding_right, 0, 0) cells_in_row.append( align_and_pad_cell(cell, align, inner_dimensions, padding)) # Determine border characters. if style == 'heading': left = self.CHAR_H_OUTER_LEFT_VERTICAL if self.outer_border else '' center = self.CHAR_H_INNER_VERTICAL if self.inner_column_border else '' right = self.CHAR_H_OUTER_RIGHT_VERTICAL if self.outer_border else '' elif style == 'footing': left = self.CHAR_F_OUTER_LEFT_VERTICAL if self.outer_border else '' center = self.CHAR_F_INNER_VERTICAL if self.inner_column_border else '' right = self.CHAR_F_OUTER_RIGHT_VERTICAL if self.outer_border else '' else: left = self.CHAR_OUTER_LEFT_VERTICAL if self.outer_border else '' center = self.CHAR_INNER_VERTICAL if self.inner_column_border else '' right = self.CHAR_OUTER_RIGHT_VERTICAL if self.outer_border else '' # Yield each line. for line in build_row(cells_in_row, left, center, right): yield line
def test_odd_width_height_pad_space(string, align, expected): """Test odd number width, height, padding, and dots for whitespaces. :param str string: String to test. :param str align: Alignment in any dimension but one at a time. :param list expected: Expected output string. """ actual = align_and_pad_cell(string, (align, ), (5, 3), (1, 1, 1, 1), '.') assert actual == expected
def test_odd_width_height_pad_space(string, align, expected): """Test odd number width, height, padding, and dots for whitespaces. :param str string: String to test. :param str align: Alignment in any dimension but one at a time. :param list expected: Expected output string. """ actual = align_and_pad_cell(string, (align,), (5, 3), (1, 1, 1, 1), ".") assert actual == expected
def test_width(string, align, width, expected): """Test width and horizontal alignment. :param str string: String to test. :param str align: Horizontal alignment. :param int width: Expand string to this width without padding. :param list expected: Expected output string. """ actual = align_and_pad_cell(string, (align,), (width, 1), (0, 0, 0, 0)) assert actual == expected
def test_height(string, align, height, expected): """Test height and vertical alignment. :param str string: String to test. :param str align: Horizontal alignment. :param int height: Expand string to this height without padding. :param list expected: Expected output string. """ actual = align_and_pad_cell(string, (align, ), (4, height), (0, 0, 0, 0)) assert actual == expected
def test_width(string, align, width, expected): """Test width and horizontal alignment. :param str string: String to test. :param str align: Horizontal alignment. :param int width: Expand string to this width without padding. :param list expected: Expected output string. """ actual = align_and_pad_cell(string, (align, ), (width, 1), (0, 0, 0, 0)) assert actual == expected
def test_height(string, align, height, expected): """Test height and vertical alignment. :param str string: String to test. :param str align: Horizontal alignment. :param int height: Expand string to this height without padding. :param list expected: Expected output string. """ actual = align_and_pad_cell(string, (align,), (4, height), (0, 0, 0, 0)) assert actual == expected
def gen_cell_lines(self, row, widths, height): r"""Combine cells in row and group them into lines with borders. Caller is expected to pass yielded lines to ''.join() to combine them into a printable line. Caller must append newline character to the end of joined line. In: ['Row One Column One', 'Two', 'Three'] Out: [ ('|', ' Row One Column One ', '|', ' Two ', '|', ' Three ', '|'), ] In: ['Row One\nColumn One', 'Two', 'Three'], Out: [ ('|', ' Row One ', '|', ' Two ', '|', ' Three ', '|'), ('|', ' Column One ', '|', ' ', '|', ' ', '|'), ] :param iter row: One row in the table. List of cells. :param iter widths: List of inner widths (no padding) for each column. :param int height: Inner height (no padding) (number of lines) to expand row to. :return: Yields lines split into components in a list. Caller must ''.join() line. """ cells_in_row = list() # Resize row if it doesn't have enough cells. if len(row) != len(widths): row = row + [''] * (len(widths) - len(row)) # Pad and align each cell. Split each cell into lines to support multi-line cells. for i, cell in enumerate(row): align = (self.justify_columns.get(i), ) inner_dimensions = (widths[i], height) padding = (self.padding_left, self.padding_right, 0, 0) cells_in_row.append( width_and_alignment.align_and_pad_cell(cell, align, inner_dimensions, padding)) # Combine cells and borders. lines = build_row( cells_in_row, self.CHAR_VERTICAL if self.outer_border else '', self.CHAR_VERTICAL if self.inner_column_border else '', self.CHAR_VERTICAL if self.outer_border else '') # Yield each line. for line in lines: yield line
def gen_cell_lines(self, row, widths, height): r"""Combine cells in row and group them into lines with borders. Caller is expected to pass yielded lines to ''.join() to combine them into a printable line. Caller must append newline character to the end of joined line. In: ['Row One Column One', 'Two', 'Three'] Out: [ ('|', ' Row One Column One ', '|', ' Two ', '|', ' Three ', '|'), ] In: ['Row One\nColumn One', 'Two', 'Three'], Out: [ ('|', ' Row One ', '|', ' Two ', '|', ' Three ', '|'), ('|', ' Column One ', '|', ' ', '|', ' ', '|'), ] :param iter row: One row in the table. List of cells. :param iter widths: List of inner widths (no padding) for each column. :param int height: Inner height (no padding) (number of lines) to expand row to. :return: Yields lines split into components in a list. Caller must ''.join() line. """ cells_in_row = list() # Resize row if it doesn't have enough cells. if len(row) != len(widths): row = row + [''] * (len(widths) - len(row)) # Pad and align each cell. Split each cell into lines to support multi-line cells. for i, cell in enumerate(row): align = (self.justify_columns.get(i),) inner_dimensions = (widths[i], height) padding = (self.padding_left, self.padding_right, 0, 0) cells_in_row.append(width_and_alignment.align_and_pad_cell(cell, align, inner_dimensions, padding)) # Combine cells and borders. lines = build_row( cells_in_row, self.CHAR_VERTICAL if self.outer_border else '', self.CHAR_VERTICAL if self.inner_column_border else '', self.CHAR_VERTICAL if self.outer_border else '' ) # Yield each line. for line in lines: yield line
def padded_table_data(self): """Return a list of lists of strings. It's self.table_data but with the cells padded with spaces and newlines. Most of the work in this class is done here. """ if not self.table_data: return list() # Set all rows to the same number of columns. max_columns = max(len(r) for r in self.table_data) new_table_data = [r + [''] * (max_columns - len(r)) for r in self.table_data] # Pad strings in each cell, and apply text-align/justification. widths = self.column_widths for row in new_table_data: height = max([c.count('\n') for c in row] or [0]) + 1 for i in range(len(row)): align = self.justify_columns.get(i, 'left') cell = align_and_pad_cell(row[i], align, (widths[i], height, self.padding_left, self.padding_right)) row[i] = cell return new_table_data
def test_align(): """Test alignment/justifications.""" assert align_and_pad_cell('test', '', (1, 1, 0, 0)) == 'test' assert align_and_pad_cell('test', 'left', (1, 1, 0, 0)) == 'test' assert align_and_pad_cell('', 'left', (4, 1, 0, 0)) == ' ' assert align_and_pad_cell('', 'left', (0, 1, 0, 0)) == '' assert align_and_pad_cell('', 'left', (0, 1, 1, 1)) == ' ' assert align_and_pad_cell('', 'left', (1, 1, 1, 1)) == ' ' assert align_and_pad_cell('', 'left', (4, 1, 1, 1)) == ' ' assert align_and_pad_cell('test', 'left', (4, 1, 0, 0)) == 'test' assert align_and_pad_cell('test', 'left', (5, 1, 0, 0)) == 'test ' assert align_and_pad_cell('test', 'left', (6, 1, 0, 0)) == 'test ' assert align_and_pad_cell('test', 'left', (7, 1, 0, 0)) == 'test ' assert align_and_pad_cell(' test', 'left', (7, 1, 0, 0)) == ' test ' assert align_and_pad_cell('test', 'right', (1, 1, 0, 0)) == 'test' assert align_and_pad_cell('test', 'right', (4, 1, 0, 0)) == 'test' assert align_and_pad_cell('test', 'right', (5, 1, 0, 0)) == ' test' assert align_and_pad_cell('test', 'right', (6, 1, 0, 0)) == ' test' assert align_and_pad_cell('test', 'right', (7, 1, 0, 0)) == ' test' assert align_and_pad_cell('test', 'center', (1, 1, 0, 0)) == 'test' assert align_and_pad_cell('test', 'center', (4, 1, 0, 0)) == 'test' assert align_and_pad_cell('test', 'center', (5, 1, 0, 0)) == ' test' assert align_and_pad_cell('test', 'center', (6, 1, 0, 0)) == ' test ' assert align_and_pad_cell('test', 'center', (7, 1, 0, 0)) == ' test '
def test_multi_line_align_padding(): """Test alignment and padding on multi-line cells.""" assert align_and_pad_cell('test\ntest', 'left', (4, 2, 0, 0)) == 'test\ntest' assert align_and_pad_cell('test\ntest', 'left', (5, 2, 0, 0)) == 'test \ntest ' assert align_and_pad_cell('test\ntest', 'left', (6, 2, 0, 0)) == 'test \ntest ' assert align_and_pad_cell('test\ntest', 'left', (7, 2, 0, 0)) == 'test \ntest ' assert align_and_pad_cell(' test\ntest', 'left', (7, 2, 0, 0)) == ' test \ntest ' assert align_and_pad_cell(' test\n test', 'left', (7, 2, 0, 0)) == ' test \n test ' assert align_and_pad_cell('test\ntest', 'right', (4, 2, 0, 0)) == 'test\ntest' assert align_and_pad_cell('test\ntest', 'right', (5, 2, 0, 0)) == ' test\n test' assert align_and_pad_cell('test\ntest', 'right', (6, 2, 0, 0)) == ' test\n test' assert align_and_pad_cell('test\ntest', 'right', (7, 2, 0, 0)) == ' test\n test' assert align_and_pad_cell('test\ntest', 'center', (4, 2, 0, 0)) == 'test\ntest' assert align_and_pad_cell('test\ntest', 'center', (5, 2, 0, 0)) == ' test\n test' assert align_and_pad_cell('test\ntest', 'center', (6, 2, 0, 0)) == ' test \n test ' assert align_and_pad_cell('test\ntest', 'center', (7, 2, 0, 0)) == ' test \n test ' assert align_and_pad_cell('test\ntest', 'left', (4, 2, 1, 0)) == ' test\n test' assert align_and_pad_cell('test\ntest', 'left', (4, 2, 0, 1)) == 'test \ntest ' assert align_and_pad_cell('test\ntest', 'left', (4, 2, 1, 1)) == ' test \n test '
def test_padding(): """Test padding.""" assert align_and_pad_cell('test', 'left', (4, 1, 1, 0)) == ' test' assert align_and_pad_cell('test', 'left', (4, 1, 0, 1)) == 'test ' assert align_and_pad_cell('test', 'left', (4, 1, 1, 1)) == ' test '
def test_align(): """Test alignment/justifications.""" assert 'test' == align_and_pad_cell('test', '', 1, 1, 0, 0) assert 'test' == align_and_pad_cell('test', 'left', 1, 1, 0, 0) assert ' ' == align_and_pad_cell('', 'left', 4, 1, 0, 0) assert '' == align_and_pad_cell('', 'left', 0, 1, 0, 0) assert ' ' == align_and_pad_cell('', 'left', 0, 1, 1, 1) assert ' ' == align_and_pad_cell('', 'left', 1, 1, 1, 1) assert ' ' == align_and_pad_cell('', 'left', 4, 1, 1, 1) assert 'test' == align_and_pad_cell('test', 'left', 4, 1, 0, 0) assert 'test ' == align_and_pad_cell('test', 'left', 5, 1, 0, 0) assert 'test ' == align_and_pad_cell('test', 'left', 6, 1, 0, 0) assert 'test ' == align_and_pad_cell('test', 'left', 7, 1, 0, 0) assert ' test ' == align_and_pad_cell(' test', 'left', 7, 1, 0, 0) assert 'test' == align_and_pad_cell('test', 'right', 1, 1, 0, 0) assert 'test' == align_and_pad_cell('test', 'right', 4, 1, 0, 0) assert ' test' == align_and_pad_cell('test', 'right', 5, 1, 0, 0) assert ' test' == align_and_pad_cell('test', 'right', 6, 1, 0, 0) assert ' test' == align_and_pad_cell('test', 'right', 7, 1, 0, 0) assert 'test' == align_and_pad_cell('test', 'center', 1, 1, 0, 0) assert 'test' == align_and_pad_cell('test', 'center', 4, 1, 0, 0) assert ' test' == align_and_pad_cell('test', 'center', 5, 1, 0, 0) assert ' test ' == align_and_pad_cell('test', 'center', 6, 1, 0, 0) assert ' test ' == align_and_pad_cell('test', 'center', 7, 1, 0, 0)
def test_padding(): """Test padding.""" assert ' test' == align_and_pad_cell('test', 'left', 4, 1, 1, 0) assert 'test ' == align_and_pad_cell('test', 'left', 4, 1, 0, 1) assert ' test ' == align_and_pad_cell('test', 'left', 4, 1, 1, 1)
def test_multi_line(): """Test multi-line support.""" assert ' test \n ' == align_and_pad_cell('test\n', 'left', 8, 2, 1, 1) assert ' \n test ' == align_and_pad_cell('\ntest', 'left', 8, 2, 1, 1) assert ' \n test \n ' == align_and_pad_cell('\ntest\n', 'left', 8, 3, 1, 1)
def test_multi_line_align_padding(): """Test alignment and padding on multi-line cells.""" assert 'test\ntest' == align_and_pad_cell('test\ntest', 'left', 4, 2, 0, 0) assert 'test \ntest ' == align_and_pad_cell('test\ntest', 'left', 5, 2, 0, 0) assert 'test \ntest ' == align_and_pad_cell('test\ntest', 'left', 6, 2, 0, 0) assert 'test \ntest ' == align_and_pad_cell('test\ntest', 'left', 7, 2, 0, 0) assert ' test \ntest ' == align_and_pad_cell(' test\ntest', 'left', 7, 2, 0, 0) assert ' test \n test ' == align_and_pad_cell(' test\n test', 'left', 7, 2, 0, 0) assert 'test\ntest' == align_and_pad_cell('test\ntest', 'right', 4, 2, 0, 0) assert ' test\n test' == align_and_pad_cell('test\ntest', 'right', 5, 2, 0, 0) assert ' test\n test' == align_and_pad_cell('test\ntest', 'right', 6, 2, 0, 0) assert ' test\n test' == align_and_pad_cell('test\ntest', 'right', 7, 2, 0, 0) assert 'test\ntest' == align_and_pad_cell('test\ntest', 'center', 4, 2, 0, 0) assert ' test\n test' == align_and_pad_cell('test\ntest', 'center', 5, 2, 0, 0) assert ' test \n test ' == align_and_pad_cell('test\ntest', 'center', 6, 2, 0, 0) assert ' test \n test ' == align_and_pad_cell('test\ntest', 'center', 7, 2, 0, 0) assert ' test\n test' == align_and_pad_cell('test\ntest', 'left', 4, 2, 1, 0) assert 'test \ntest ' == align_and_pad_cell('test\ntest', 'left', 4, 2, 0, 1) assert ' test \n test ' == align_and_pad_cell('test\ntest', 'left', 4, 2, 1, 1)
def test_multi_line(): """Test multi-line support.""" assert align_and_pad_cell('test\n', 'left', (8, 2, 1, 1)) == ' test \n ' assert align_and_pad_cell('\ntest', 'left', (8, 2, 1, 1)) == ' \n test ' assert align_and_pad_cell('\ntest\n', 'left', (8, 3, 1, 1)) == ' \n test \n '