def test_fmt_tuple_rows(): expected = ''' ╔═══════════╤═══════════════════╤═══════╤═══════════╤════════════╗ ║ First │ Second │ Num 1 │ Num 2 │ Multiplied ║ ╠═══════════╪═══════════════════╪═══════╪═══════════╪════════════╣ ║ │ │ 17 │ Four │ 68 ║ ║ 123.00 B │ 123 │ 5 │ Fifty-six │ 280 ║ ║ 123.00 B │ 123 │ 5 │ Fifty-six │ 280 ║ ║ 12.06 KB │ 12,345 │ 23 │ Eight │ 184 ║ ║ 11.77 MB │ 12,345,678 │ 4 │ Nine │ 36 ║ ║ 1.15 GB │ 1,234,567,890 │ 7 │ Five │ 35 ║ ║ 1.12 TB │ 1,234,567,890,123 │ 7 │ Five │ 35 ║ ╚═══════════╧═══════════════════╧═══════╧═══════════╧════════════╝ '''.lstrip('\n') rows = [(None, None, 17, 4, None), ('123', '123', 5, 56, None), (123, 123, 5, 56, None), (12345, 12345, 23, 8, None), (12345678, 12345678, 4, 9, None), (1234567890, 1234567890, 7, 5, None), (1234567890123, 1234567890123, 7, 5, None)] columns = (tf.Column('First', width=20, formatter=tf.FormatBytes(), cell_halign=tf.ColumnAlignment.AlignRight), tf.Column('Second', formatter=tf.FormatCommas(), cell_halign=tf.ColumnAlignment.AlignRight), tf.Column('Num 1'), tf.Column('Num 2', formatter=int2word), tf.Column('Multiplied', obj_formatter=multiply_tuple)) table = tf.generate_table(rows, columns) assert table == expected
def test_fmt_obj_rows(): expected = ''' ╔═══════════╤═══════════════════╤═════╤═══════╤════════════╗ ║ │ │ Num │ │ ║ ║ First │ Second │ 1 │ Num 2 │ Multiplied ║ ╠═══════════╪═══════════════════╪═════╪═══════╪════════════╣ ║ │ │ 17 │ 4 │ 68 ║ ║ 123.00 B │ 123 │ 5 │ 56 │ 280 ║ ║ 123.00 B │ 123 │ 5 │ 56 │ 280 ║ ║ 12.06 KB │ 12,345 │ 23 │ 8 │ 184 ║ ║ 11.77 MB │ 12,345,678 │ 4 │ 9 │ 36 ║ ║ 1.15 GB │ 1,234,567,890 │ 7 │ 5 │ 35 ║ ║ 1.12 TB │ 1,234,567,890,123 │ 7 │ 5 │ 35 ║ ╚═══════════╧═══════════════════╧═════╧═══════╧════════════╝ '''.lstrip('\n') rows = [ MyRowObject(None, None, 17, 4), MyRowObject('123', '123', 5, 56), MyRowObject(123, 123, 5, 56), MyRowObject(12345, 12345, 23, 8), MyRowObject(12345678, 12345678, 4, 9), MyRowObject(1234567890, 1234567890, 7, 5), MyRowObject(1234567890123, 1234567890123, 7, 5) ] columns = (tf.Column('First', width=20, attrib='field1', formatter=tf.FormatBytes(), cell_halign=tf.ColumnAlignment.AlignRight), tf.Column('Second', attrib='field2', formatter=tf.FormatCommas(), cell_halign=tf.ColumnAlignment.AlignRight), tf.Column('Num 1', width=3, attrib='get_field3'), tf.Column('Num 2', attrib='field4'), tf.Column('Multiplied', obj_formatter=multiply)) table = tf.generate_table(rows, columns) assert table == expected
], ] # Calculate population density for row in EXAMPLE_ITERABLE_DATA: row.append(row[-2] / row[-1]) # Column headers plus optional formatting info for each column COLUMNS = [ tf.Column('City', width=11, header_halign=tf.ColumnAlignment.AlignCenter), tf.Column('Province', header_halign=tf.ColumnAlignment.AlignCenter), 'Country', # NOTE: If you don't need any special effects, you can just pass a string tf.Column('Continent', cell_halign=tf.ColumnAlignment.AlignCenter), tf.Column('Population', cell_halign=tf.ColumnAlignment.AlignRight, formatter=tf.FormatCommas()), tf.Column('Area (km²)', width=7, header_halign=tf.ColumnAlignment.AlignCenter, cell_halign=tf.ColumnAlignment.AlignRight, formatter=two_dec), tf.Column('Pop. Density (/km²)', width=12, header_halign=tf.ColumnAlignment.AlignCenter, cell_halign=tf.ColumnAlignment.AlignRight, formatter=no_dec), ] # ######## Table data formatted as an iterable of python objects #########
['Shenzen (深圳市)', 'Guangdong', 'China', 'Asia', 13723000, 1493.32], ['Guangzho (广州市)', 'Guangdong', 'China', 'Asia', 13081000, 1347.81], ['Mumbai (मुंबई)', 'Maharashtra', 'India', 'Asia', 12442373, 465.78], ['Istanbul (İstanbuld)', 'Istanbul', 'Turkey', 'Eurasia', 12661000, 620.29], ] # Calculate population density for row in EXAMPLE_ITERABLE_DATA: row.append(row[-2] / row[-1]) # Column headers plus optional formatting info for each column COLUMNS = [tf.Column('City', width=11, header_halign=tf.ColumnAlignment.AlignCenter), tf.Column('Province', header_halign=tf.ColumnAlignment.AlignCenter), 'Country', # NOTE: If you don't need any special effects, you can just pass a string tf.Column('Continent', cell_halign=tf.ColumnAlignment.AlignCenter), tf.Column('Population', cell_halign=tf.ColumnAlignment.AlignRight, formatter=tf.FormatCommas()), tf.Column('Area (km²)', width=7, header_halign=tf.ColumnAlignment.AlignCenter, cell_halign=tf.ColumnAlignment.AlignRight, formatter=two_dec), tf.Column('Pop. Density (/km²)', width=12, header_halign=tf.ColumnAlignment.AlignCenter, cell_halign=tf.ColumnAlignment.AlignRight, formatter=no_dec), ] # ######## Table data formatted as an iterable of python objects ######### class CityInfo(object): """City information container""" def __init__(self, city: str, province: str, country: str, continent: str, population: int, area: float): self.city = city self.province = province