def test_no_cells(): """Test with no cells.""" actual = [tuple(i) for i in build_row([[]], '>', '|', '<')] expected = [ ('>', '<'), ] assert actual == expected actual = [tuple(i) for i in build_row([], '>', '|', '<')] expected = [ ('>', '<'), ] assert actual == expected
def test_no_cells(): """Test with no cells.""" actual = build_row([[]], '>', '|', '<') expected = [ ('>', '<'), ] assert actual == expected actual = build_row([], '>', '|', '<') expected = [ ('>', '<'), ] assert actual == expected
def test_single(): """Test with single cell.""" actual = [tuple(i) for i in build_row([['Cell']], '>', '|', '<')] expected = [ ('>', 'Cell', '<'), ] assert actual == expected
def test_empty(): """Test with empty cell.""" actual = [tuple(i) for i in build_row([['']], '>', '|', '<')] expected = [ ('>', '', '<'), ] assert actual == expected
def test_three_line(): """Test with three line cells.""" row = [ [ 'Left ', 'Cell1', ' ', ], [ 'Center', 'Cell2 ', ' ', ], [ 'Right', 'Cell3', ' ', ], ] actual = [tuple(i) for i in build_row(row, '>', '|', '<')] expected = [ ('>', 'Left ', '|', 'Center', '|', 'Right', '<'), ('>', 'Cell1', '|', 'Cell2 ', '|', 'Cell3', '<'), ('>', ' ', '|', ' ', '|', ' ', '<'), ] assert actual == expected
def test_empty(): """Test with empty cell.""" actual = build_row([['']], '>', '|', '<') expected = [ ('>', '', '<'), ] assert actual == expected
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_one_line(): """Test with one line cells.""" row = [ ['Left Cell'], ['Center Cell'], ['Right Cell'], ] actual = build_row(row, '>', '|', '<') expected = [ ('>', 'Left Cell', '|', 'Center Cell', '|', 'Right Cell', '<'), ] assert actual == expected
def test_one_line(): """Test with one line cells.""" row = [ ['Left Cell'], ['Center Cell'], ['Right Cell'], ] actual = [tuple(i) for i in build_row(row, '>', '|', '<')] expected = [ ('>', 'Left Cell', '|', 'Center Cell', '|', 'Right Cell', '<'), ] 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 test_two_line(): """Test with two line cells.""" row = [ [ 'Left ', 'Cell1', ], [ 'Center', 'Cell2 ', ], [ 'Right', 'Cell3', ], ] actual = build_row(row, '>', '|', '<') expected = [ ('>', 'Left ', '|', 'Center', '|', 'Right', '<'), ('>', 'Cell1', '|', 'Cell2 ', '|', 'Cell3', '<'), ] assert actual == expected