From 8e49e5a0b4b21f70740e025a40e4ac0f6d573c4d Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Tue, 26 Jun 2018 00:34:50 -0700 Subject: table_display.py example now uses tableformatter instead of tabulate Changed the table_display.py example to use the tableformatter module which resuls in nicer looking tables. --- examples/table_display.py | 77 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 60 insertions(+), 17 deletions(-) (limited to 'examples/table_display.py') diff --git a/examples/table_display.py b/examples/table_display.py index 2e6ea804..c81b6751 100755 --- a/examples/table_display.py +++ b/examples/table_display.py @@ -1,36 +1,78 @@ #!/usr/bin/env python # coding=utf-8 """A simple example demonstrating the following: - 1) How to display tabular data within a cmd2 application + 1) How to display tabular data 2) How to display output using a pager NOTE: IF the table does not entirely fit within the screen of your terminal, then it will be displayed using a pager. You can use the arrow keys (left, right, up, and down) to scroll around the table as well as the PageUp/PageDown keys. You can quit out of the pager by typing "q". You can also search for text within the pager using "/". -WARNING: This example requires the tabulate module. +WARNING: This example requires the tableformatter module: https://github.com/python-tableformatter/tableformatter +- pip install tableformatter """ import functools import cmd2 -import tabulate +import tableformatter as tf -# Format to use with tabulate module when displaying tables -TABLE_FORMAT = 'grid' +try: + from colored import bg + BACK_PRI = bg(4) + BACK_ALT = bg(2) +except ImportError: + try: + from colorama import Back + BACK_PRI = Back.LIGHTBLUE_EX + BACK_ALT = Back.LIGHTYELLOW_EX + except ImportError: + BACK_PRI = '' + BACK_ALT = '' + +# Format to use for the grid style when displaying tables with the tableformatter module +grid_style = tf.AlternatingRowGrid(BACK_PRI, BACK_ALT) # Use rows of alternating color to assist as a visual guide # Create a function to format a fixed-width table for pretty-printing using the desired table format -table = functools.partial(tabulate.tabulate, tablefmt=TABLE_FORMAT) +table = functools.partial(tf.generate_table, grid_style=grid_style) + # Population data from Wikipedia: https://en.wikipedia.org/wiki/List_of_cities_proper_by_population -EXAMPLE_DATA = [['Shanghai', 'Shanghai', 'China', 'Asia', 24183300, 6340.5, 3814], - ['Beijing', 'Hebei', 'China', 'Asia', 20794000, 1749.57, 11885], - ['Karachi', 'Sindh', 'Pakistan', 'Asia', 14910352, 615.58, 224221], - ['Shenzen', 'Guangdong', 'China', 'Asia', 13723000, 1493.32, 9190], - ['Guangzho', 'Guangdong', 'China', 'Asia', 13081000, 1347.81, 9705], - ['Mumbai', ' Maharashtra', 'India', 'Asia', 12442373, 465.78, 27223], - ['Istanbul', 'Istanbul', 'Turkey', 'Eurasia', 12661000, 620.29, 20411], +EXAMPLE_DATA = [['Shanghai', 'Shanghai', 'China', 'Asia', 24183300, 6340.5], + ['Beijing', 'Hebei', 'China', 'Asia', 20794000, 1749.57], + ['Karachi', 'Sindh', 'Pakistan', 'Asia', 14910352, 615.58], + ['Shenzen', 'Guangdong', 'China', 'Asia', 13723000, 1493.32], + ['Guangzho', 'Guangdong', 'China', 'Asia', 13081000, 1347.81], + ['Mumbai', 'Maharashtra', 'India', 'Asia', 12442373, 465.78], + ['Istanbul', 'Istanbul', 'Turkey', 'Eurasia', 12661000, 620.29], ] -EXAMPLE_HEADERS = ['City', 'Province', 'Country', 'Continent', 'Population', 'Area (km^2)', 'Pop. Density (/km^2)'] + +# Calculate population density +for row in EXAMPLE_DATA: + row.append(row[-2]/row[-1]) + + +# TODO: Color row text foreground based on population density + + +def no_dec(num: float) -> str: + """Format a floating point number with no decimal places.""" + return "{}".format(round(num)) + + +def two_dec(num: float) -> str: + """Format a floating point number with 2 decimal places.""" + return "{0:.2f}".format(num) + + +# # Column headers plus optional formatting info for each column +columns = [tf.Column('City', 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^2)', cell_halign=tf.ColumnAlignment.AlignRight, formatter=two_dec), + tf.Column('Pop. Density (/km^2)', width=12, cell_halign=tf.ColumnAlignment.AlignRight, formatter=no_dec), + ] class TableDisplay(cmd2.Cmd): @@ -51,14 +93,15 @@ class TableDisplay(cmd2.Cmd): - if `headers="keys"`, then dictionary keys or column indices are used - Otherwise, a headerless table is produced """ - formatted_table = table(tabular_data, headers=headers) - self.ppaged(formatted_table) + formatted_table = table(rows=tabular_data, columns=headers) + self.ppaged(formatted_table, chop=True) def do_table(self, _): """Display data on the Earth's most populated cities in a table.""" - self.ptable(tabular_data=EXAMPLE_DATA, headers=EXAMPLE_HEADERS) + self.ptable(EXAMPLE_DATA, columns) if __name__ == '__main__': app = TableDisplay() + app.debug = True app.cmdloop() -- cgit v1.2.1 From ac370cb59b0d138f34ae980271c3c0c953cbfc57 Mon Sep 17 00:00:00 2001 From: Eric Lin Date: Tue, 26 Jun 2018 10:14:17 -0400 Subject: Updating the example with unicode characters as well as demonstrating the same table entries formatted as both an iterable of iterable as well as an iterable of objects. --- examples/table_display.py | 107 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 86 insertions(+), 21 deletions(-) (limited to 'examples/table_display.py') diff --git a/examples/table_display.py b/examples/table_display.py index c81b6751..7df178db 100755 --- a/examples/table_display.py +++ b/examples/table_display.py @@ -30,29 +30,13 @@ except ImportError: BACK_ALT = '' # Format to use for the grid style when displaying tables with the tableformatter module -grid_style = tf.AlternatingRowGrid(BACK_PRI, BACK_ALT) # Use rows of alternating color to assist as a visual guide +grid_style = tf.AlternatingRowGrid(BACK_PRI, BACK_ALT) # Use rows of alternating color to assist as a visual guide # Create a function to format a fixed-width table for pretty-printing using the desired table format table = functools.partial(tf.generate_table, grid_style=grid_style) -# Population data from Wikipedia: https://en.wikipedia.org/wiki/List_of_cities_proper_by_population -EXAMPLE_DATA = [['Shanghai', 'Shanghai', 'China', 'Asia', 24183300, 6340.5], - ['Beijing', 'Hebei', 'China', 'Asia', 20794000, 1749.57], - ['Karachi', 'Sindh', 'Pakistan', 'Asia', 14910352, 615.58], - ['Shenzen', 'Guangdong', 'China', 'Asia', 13723000, 1493.32], - ['Guangzho', 'Guangdong', 'China', 'Asia', 13081000, 1347.81], - ['Mumbai', 'Maharashtra', 'India', 'Asia', 12442373, 465.78], - ['Istanbul', 'Istanbul', 'Turkey', 'Eurasia', 12661000, 620.29], - ] - -# Calculate population density -for row in EXAMPLE_DATA: - row.append(row[-2]/row[-1]) - - -# TODO: Color row text foreground based on population density - +# Formatter functions def no_dec(num: float) -> str: """Format a floating point number with no decimal places.""" @@ -64,17 +48,94 @@ def two_dec(num: float) -> str: return "{0:.2f}".format(num) +# Population data from Wikipedia: https://en.wikipedia.org/wiki/List_of_cities_proper_by_population + + +# ############ Table data formatted as an iterable of iterable fields ############ + +EXAMPLE_ITERABLE_DATA = [['Shanghai', 'Shanghai', 'China', 'Asia', 24183300, 6340.5], + ['Beijing', 'Hebei', 'China', 'Asia', 20794000, 1749.57], + ['Karachi', 'Sindh', 'Pakistan', 'Asia', 14910352, 615.58], + ['Shenzen', 'Guangdong', 'China', 'Asia', 13723000, 1493.32], + ['Guangzho', 'Guangdong', 'China', 'Asia', 13081000, 1347.81], + ['Mumbai', 'Maharashtra', 'India', 'Asia', 12442373, 465.78], + ['Istanbul', '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', 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^2)', cell_halign=tf.ColumnAlignment.AlignRight, formatter=two_dec), - tf.Column('Pop. Density (/km^2)', width=12, cell_halign=tf.ColumnAlignment.AlignRight, formatter=no_dec), + 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 + self.country = country + self.continent = continent + self._population = population + self._area = area + + def get_population(self): + """Population of the city""" + return self._population + + def get_area(self): + """Area of city in km²""" + return self._area + + +def pop_density(data: CityInfo): + """Calculate the population density from the data entry""" + if isinstance(data, CityInfo): + return no_dec(data.get_population() / data.get_area()) + + return '' + + +EXAMPLE_OBJECT_DATA = [CityInfo('Shanghai', 'Shanghai', 'China', 'Asia', 24183300, 6340.5), + CityInfo('Beijing', 'Hebei', 'China', 'Asia', 20794000, 1749.57), + CityInfo('Karachi', 'Sindh', 'Pakistan', 'Asia', 14910352, 615.58), + CityInfo('Shenzen', 'Guangdong', 'China', 'Asia', 13723000, 1493.32), + CityInfo('Guangzho', 'Guangdong', 'China', 'Asia', 13081000, 1347.81), + CityInfo('Mumbai', 'Maharashtra', 'India', 'Asia', 12442373, 465.78), + CityInfo('Istanbul', 'Istanbul', 'Turkey', 'Eurasia', 12661000, 620.29), + ] + +# If table entries are python objects, all columns must be defined with the object attribute to query for each field +# - attributes can be fields or functions. If a function is provided, the formatter will automatically call +# the function to retrieve the value +obj_cols = [tf.Column('City', attrib='city', header_halign=tf.ColumnAlignment.AlignCenter), + tf.Column('Province', attrib='province', header_halign=tf.ColumnAlignment.AlignCenter), + tf.Column('Country', attrib='country'), + tf.Column('Continent', attrib='continent', cell_halign=tf.ColumnAlignment.AlignCenter), + tf.Column('Population', attrib='get_population', cell_halign=tf.ColumnAlignment.AlignRight, + formatter=tf.FormatCommas()), + tf.Column('Area (km²)', attrib='get_area', 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, obj_formatter=pop_density), + ] + +# TODO: Color row text foreground based on population density + + class TableDisplay(cmd2.Cmd): """Example cmd2 application showing how you can display tabular data.""" @@ -98,7 +159,11 @@ class TableDisplay(cmd2.Cmd): def do_table(self, _): """Display data on the Earth's most populated cities in a table.""" - self.ptable(EXAMPLE_DATA, columns) + self.ptable(EXAMPLE_ITERABLE_DATA, columns) + + def do_object_table(self, _): + """Display data on the Earth's most populated cities in a table.""" + self.ptable(EXAMPLE_OBJECT_DATA, obj_cols) if __name__ == '__main__': -- cgit v1.2.1 From 83f74c746c4389076bdb950be29cfd3e6370842e Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Tue, 26 Jun 2018 21:29:02 -0700 Subject: Added city names in their native tongue in parentheses to demonstrate unicode capability - Mandarin, Urdu, and Turkish all seem to be working fine - But something is up with Hindi - the table/colum width calculation is messed up for Mumbai --- examples/table_display.py | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) (limited to 'examples/table_display.py') diff --git a/examples/table_display.py b/examples/table_display.py index 7df178db..e461dd47 100755 --- a/examples/table_display.py +++ b/examples/table_display.py @@ -52,14 +52,13 @@ def two_dec(num: float) -> str: # ############ Table data formatted as an iterable of iterable fields ############ - -EXAMPLE_ITERABLE_DATA = [['Shanghai', 'Shanghai', 'China', 'Asia', 24183300, 6340.5], - ['Beijing', 'Hebei', 'China', 'Asia', 20794000, 1749.57], - ['Karachi', 'Sindh', 'Pakistan', 'Asia', 14910352, 615.58], - ['Shenzen', 'Guangdong', 'China', 'Asia', 13723000, 1493.32], - ['Guangzho', 'Guangdong', 'China', 'Asia', 13081000, 1347.81], - ['Mumbai', 'Maharashtra', 'India', 'Asia', 12442373, 465.78], - ['Istanbul', 'Istanbul', 'Turkey', 'Eurasia', 12661000, 620.29], +EXAMPLE_ITERABLE_DATA = [['Shanghai (上海)', 'Shanghai', 'China', 'Asia', 24183300, 6340.5], + ['Beijing (北京市)', 'Hebei', 'China', 'Asia', 20794000, 1749.57], + ['Karachi (کراچی)', 'Sindh', 'Pakistan', 'Asia', 14910352, 615.58], + ['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 @@ -68,7 +67,7 @@ for row in EXAMPLE_ITERABLE_DATA: # # Column headers plus optional formatting info for each column -columns = [tf.Column('City', header_halign=tf.ColumnAlignment.AlignCenter), +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), @@ -109,14 +108,10 @@ def pop_density(data: CityInfo): return '' -EXAMPLE_OBJECT_DATA = [CityInfo('Shanghai', 'Shanghai', 'China', 'Asia', 24183300, 6340.5), - CityInfo('Beijing', 'Hebei', 'China', 'Asia', 20794000, 1749.57), - CityInfo('Karachi', 'Sindh', 'Pakistan', 'Asia', 14910352, 615.58), - CityInfo('Shenzen', 'Guangdong', 'China', 'Asia', 13723000, 1493.32), - CityInfo('Guangzho', 'Guangdong', 'China', 'Asia', 13081000, 1347.81), - CityInfo('Mumbai', 'Maharashtra', 'India', 'Asia', 12442373, 465.78), - CityInfo('Istanbul', 'Istanbul', 'Turkey', 'Eurasia', 12661000, 620.29), - ] +# Convert the Iterable of Iterables data to an Iterable of non-iterable objects for demonstration purposes +EXAMPLE_OBJECT_DATA = [] +for city in EXAMPLE_ITERABLE_DATA: + EXAMPLE_OBJECT_DATA.append(CityInfo(city[0], city[1], city[2], city[3], city[4], city[5])) # If table entries are python objects, all columns must be defined with the object attribute to query for each field # - attributes can be fields or functions. If a function is provided, the formatter will automatically call -- cgit v1.2.1 From 428e6d9817612c0de91a148361c34a91a15479cf Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Wed, 27 Jun 2018 06:15:37 -0700 Subject: Fixed table width for Mumbai Changed to use Hindi name for Mumbai based on Google Translate instead of a Google search and the table width and underlying wide character width calculation appears to be working fine. --- examples/table_display.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'examples/table_display.py') diff --git a/examples/table_display.py b/examples/table_display.py index e461dd47..2d562f72 100755 --- a/examples/table_display.py +++ b/examples/table_display.py @@ -50,14 +50,13 @@ def two_dec(num: float) -> str: # Population data from Wikipedia: https://en.wikipedia.org/wiki/List_of_cities_proper_by_population - # ############ Table data formatted as an iterable of iterable fields ############ EXAMPLE_ITERABLE_DATA = [['Shanghai (上海)', 'Shanghai', 'China', 'Asia', 24183300, 6340.5], ['Beijing (北京市)', 'Hebei', 'China', 'Asia', 20794000, 1749.57], ['Karachi (کراچی)', 'Sindh', 'Pakistan', 'Asia', 14910352, 615.58], ['Shenzen (深圳市)', 'Guangdong', 'China', 'Asia', 13723000, 1493.32], ['Guangzho (广州市)', 'Guangdong', 'China', 'Asia', 13081000, 1347.81], - ['Mumbai (बॉम्बे हिंदी)', 'Maharashtra', 'India', 'Asia', 12442373, 465.78], + ['Mumbai (मुंबई)', 'Maharashtra', 'India', 'Asia', 12442373, 465.78], ['Istanbul (İstanbuld)', 'Istanbul', 'Turkey', 'Eurasia', 12661000, 620.29], ] -- cgit v1.2.1 From 981a7edd65dfae9973a68761a6546120b9e2e73c Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Wed, 27 Jun 2018 06:48:32 -0700 Subject: table and object_table commands now accept argparse arguments for formatting the grid style --- examples/table_display.py | 48 +++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 20 deletions(-) (limited to 'examples/table_display.py') diff --git a/examples/table_display.py b/examples/table_display.py index 2d562f72..f458aafd 100755 --- a/examples/table_display.py +++ b/examples/table_display.py @@ -11,7 +11,7 @@ You can quit out of the pager by typing "q". You can also search for text withi WARNING: This example requires the tableformatter module: https://github.com/python-tableformatter/tableformatter - pip install tableformatter """ -import functools +import argparse import cmd2 import tableformatter as tf @@ -30,14 +30,10 @@ except ImportError: BACK_ALT = '' # Format to use for the grid style when displaying tables with the tableformatter module -grid_style = tf.AlternatingRowGrid(BACK_PRI, BACK_ALT) # Use rows of alternating color to assist as a visual guide - -# Create a function to format a fixed-width table for pretty-printing using the desired table format -table = functools.partial(tf.generate_table, grid_style=grid_style) +DEFAULT_GRID = tf.AlternatingRowGrid(BACK_PRI, BACK_ALT) # Use rows of alternating color to assist as a visual guide # Formatter functions - def no_dec(num: float) -> str: """Format a floating point number with no decimal places.""" return "{}".format(round(num)) @@ -136,28 +132,40 @@ class TableDisplay(cmd2.Cmd): def __init__(self): super().__init__() - def ptable(self, tabular_data, headers=()): + def ptable(self, rows, columns, grid_args): """Format tabular data for pretty-printing as a fixed-width table and then display it using a pager. - :param tabular_data: required argument - can be a list-of-lists (or another iterable of iterables), a list of - named tuples, a dictionary of iterables, an iterable of dictionaries, a two-dimensional - NumPy array, NumPy record array, or a Pandas dataframe. - :param headers: (optional) - to print nice column headers, supply this argument: - - headers can be an explicit list of column headers - - if `headers="firstrow"`, then the first row of data is used - - if `headers="keys"`, then dictionary keys or column indices are used - - Otherwise, a headerless table is produced + :param rows: required argument - can be a list-of-lists (or another iterable of iterables), a two-dimensional + NumPy array, or an Iterable of non-iterable objects + :param columns: column headers and formatting options per column + :param grid_args: argparse arguments for formatting the grid """ - formatted_table = table(rows=tabular_data, columns=headers) + if grid_args.color: + grid = tf.AlternatingRowGrid(BACK_PRI, BACK_ALT) + elif grid_args.fancy: + grid = tf.FancyGrid() + elif grid_args.sparse: + grid = tf.SparseGrid() + else: + grid = None + + formatted_table = tf.generate_table(rows=rows, columns=columns, grid_style=grid) self.ppaged(formatted_table, chop=True) - def do_table(self, _): + table_parser = argparse.ArgumentParser() + table_parser.add_argument('-c', '--color', action='store_true', help='Enable color') + table_parser.add_argument('-f', '--fancy', action='store_true', help='Fancy Grid') + table_parser.add_argument('-s', '--sparse', action='store_true', help='Sparse Grid') + + @cmd2.with_argparser(table_parser) + def do_table(self, args): """Display data on the Earth's most populated cities in a table.""" - self.ptable(EXAMPLE_ITERABLE_DATA, columns) + self.ptable(EXAMPLE_ITERABLE_DATA, columns, args) - def do_object_table(self, _): + @cmd2.with_argparser(table_parser) + def do_object_table(self, args): """Display data on the Earth's most populated cities in a table.""" - self.ptable(EXAMPLE_OBJECT_DATA, obj_cols) + self.ptable(EXAMPLE_OBJECT_DATA, obj_cols, args) if __name__ == '__main__': -- cgit v1.2.1 From 926c2cf078da9740838663b2651c372a5340eb5f Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Wed, 27 Jun 2018 07:02:30 -0700 Subject: Deleted unused constant for DEFAULT_GRID Also: - Improved some comments - Put the argparse args for selecting grid style in a mutually exclusive group --- examples/table_display.py | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) (limited to 'examples/table_display.py') diff --git a/examples/table_display.py b/examples/table_display.py index f458aafd..9af15175 100755 --- a/examples/table_display.py +++ b/examples/table_display.py @@ -16,6 +16,7 @@ import argparse import cmd2 import tableformatter as tf +# Configure colors for when users chooses the "-c" flag to enable color in the table output try: from colored import bg BACK_PRI = bg(4) @@ -29,9 +30,6 @@ except ImportError: BACK_PRI = '' BACK_ALT = '' -# Format to use for the grid style when displaying tables with the tableformatter module -DEFAULT_GRID = tf.AlternatingRowGrid(BACK_PRI, BACK_ALT) # Use rows of alternating color to assist as a visual guide - # Formatter functions def no_dec(num: float) -> str: @@ -54,24 +52,24 @@ EXAMPLE_ITERABLE_DATA = [['Shanghai (上海)', 'Shanghai', 'China', 'Asia', 2418 ['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), +# 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 + '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 ######### @@ -105,13 +103,14 @@ def pop_density(data: CityInfo): # Convert the Iterable of Iterables data to an Iterable of non-iterable objects for demonstration purposes EXAMPLE_OBJECT_DATA = [] -for city in EXAMPLE_ITERABLE_DATA: - EXAMPLE_OBJECT_DATA.append(CityInfo(city[0], city[1], city[2], city[3], city[4], city[5])) +for city_data in EXAMPLE_ITERABLE_DATA: + # Pass all city data other than population density to construct CityInfo + EXAMPLE_OBJECT_DATA.append(CityInfo(*city_data[:-1])) # If table entries are python objects, all columns must be defined with the object attribute to query for each field # - attributes can be fields or functions. If a function is provided, the formatter will automatically call # the function to retrieve the value -obj_cols = [tf.Column('City', attrib='city', header_halign=tf.ColumnAlignment.AlignCenter), +OBJ_COLS = [tf.Column('City', attrib='city', header_halign=tf.ColumnAlignment.AlignCenter), tf.Column('Province', attrib='province', header_halign=tf.ColumnAlignment.AlignCenter), tf.Column('Country', attrib='country'), tf.Column('Continent', attrib='continent', cell_halign=tf.ColumnAlignment.AlignCenter), @@ -121,7 +120,7 @@ obj_cols = [tf.Column('City', attrib='city', header_halign=tf.ColumnAlignment.Al 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, obj_formatter=pop_density), - ] + ] # TODO: Color row text foreground based on population density @@ -153,19 +152,20 @@ class TableDisplay(cmd2.Cmd): self.ppaged(formatted_table, chop=True) table_parser = argparse.ArgumentParser() - table_parser.add_argument('-c', '--color', action='store_true', help='Enable color') - table_parser.add_argument('-f', '--fancy', action='store_true', help='Fancy Grid') - table_parser.add_argument('-s', '--sparse', action='store_true', help='Sparse Grid') + table_item_group = table_parser.add_mutually_exclusive_group() + table_item_group.add_argument('-c', '--color', action='store_true', help='Enable color') + table_item_group.add_argument('-f', '--fancy', action='store_true', help='Fancy Grid') + table_item_group.add_argument('-s', '--sparse', action='store_true', help='Sparse Grid') @cmd2.with_argparser(table_parser) def do_table(self, args): - """Display data on the Earth's most populated cities in a table.""" - self.ptable(EXAMPLE_ITERABLE_DATA, columns, args) + """Display data in iterable form on the Earth's most populated cities in a table.""" + self.ptable(EXAMPLE_ITERABLE_DATA, COLUMNS, args) @cmd2.with_argparser(table_parser) def do_object_table(self, args): - """Display data on the Earth's most populated cities in a table.""" - self.ptable(EXAMPLE_OBJECT_DATA, obj_cols, args) + """Display data in object form on the Earth's most populated cities in a table.""" + self.ptable(EXAMPLE_OBJECT_DATA, OBJ_COLS, args) if __name__ == '__main__': -- cgit v1.2.1 From 28f080b2ea338b72fbff2992ba4745c6456f651f Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Wed, 27 Jun 2018 08:42:30 -0700 Subject: Made table more readable when colored module is installed --- examples/table_display.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/table_display.py') diff --git a/examples/table_display.py b/examples/table_display.py index 9af15175..c2aff0c1 100755 --- a/examples/table_display.py +++ b/examples/table_display.py @@ -20,7 +20,7 @@ import tableformatter as tf try: from colored import bg BACK_PRI = bg(4) - BACK_ALT = bg(2) + BACK_ALT = bg(22) except ImportError: try: from colorama import Back -- cgit v1.2.1 From c038c83498afdd0f7002df125b945a3089516e7c Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Thu, 28 Jun 2018 20:34:38 -0700 Subject: Rows with very high population density now have foreground text color set to red --- examples/table_display.py | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) (limited to 'examples/table_display.py') diff --git a/examples/table_display.py b/examples/table_display.py index c2aff0c1..75eada85 100755 --- a/examples/table_display.py +++ b/examples/table_display.py @@ -12,6 +12,7 @@ WARNING: This example requires the tableformatter module: https://github.com/pyt - pip install tableformatter """ import argparse +from typing import Tuple import cmd2 import tableformatter as tf @@ -93,12 +94,11 @@ class CityInfo(object): return self._area -def pop_density(data: CityInfo): +def pop_density(data: CityInfo) -> str: """Calculate the population density from the data entry""" - if isinstance(data, CityInfo): - return no_dec(data.get_population() / data.get_area()) - - return '' + if not isinstance(data, CityInfo): + raise AttributeError("Argument to pop_density() must be an instance of CityInfo") + return no_dec(data.get_population() / data.get_area()) # Convert the Iterable of Iterables data to an Iterable of non-iterable objects for demonstration purposes @@ -122,7 +122,24 @@ OBJ_COLS = [tf.Column('City', attrib='city', header_halign=tf.ColumnAlignment.Al cell_halign=tf.ColumnAlignment.AlignRight, obj_formatter=pop_density), ] -# TODO: Color row text foreground based on population density + +EXTREMELY_HIGH_POULATION_DENSITY = 25000 + + +def high_density_tuples(row_tuple: Tuple) -> dict: + """Color rows with extremely high population density red.""" + opts = dict() + if len(row_tuple) >= 7 and row_tuple[6] > EXTREMELY_HIGH_POULATION_DENSITY: + opts[tf.TableFormatter.ROW_OPT_TEXT_COLOR] = tf.TableColors.TEXT_COLOR_RED + return opts + + +def high_density_objs(row_obj: CityInfo) -> dict: + """Color rows with extremely high population density red.""" + opts = dict() + if float(pop_density(row_obj)) > EXTREMELY_HIGH_POULATION_DENSITY: + opts[tf.TableFormatter.ROW_OPT_TEXT_COLOR] = tf.TableColors.TEXT_COLOR_RED + return opts class TableDisplay(cmd2.Cmd): @@ -131,13 +148,14 @@ class TableDisplay(cmd2.Cmd): def __init__(self): super().__init__() - def ptable(self, rows, columns, grid_args): + def ptable(self, rows, columns, grid_args, row_stylist): """Format tabular data for pretty-printing as a fixed-width table and then display it using a pager. :param rows: required argument - can be a list-of-lists (or another iterable of iterables), a two-dimensional NumPy array, or an Iterable of non-iterable objects :param columns: column headers and formatting options per column :param grid_args: argparse arguments for formatting the grid + :param row_stylist: function to determine how each row gets styled """ if grid_args.color: grid = tf.AlternatingRowGrid(BACK_PRI, BACK_ALT) @@ -148,7 +166,7 @@ class TableDisplay(cmd2.Cmd): else: grid = None - formatted_table = tf.generate_table(rows=rows, columns=columns, grid_style=grid) + formatted_table = tf.generate_table(rows=rows, columns=columns, grid_style=grid, row_tagger=row_stylist) self.ppaged(formatted_table, chop=True) table_parser = argparse.ArgumentParser() @@ -160,12 +178,12 @@ class TableDisplay(cmd2.Cmd): @cmd2.with_argparser(table_parser) def do_table(self, args): """Display data in iterable form on the Earth's most populated cities in a table.""" - self.ptable(EXAMPLE_ITERABLE_DATA, COLUMNS, args) + self.ptable(EXAMPLE_ITERABLE_DATA, COLUMNS, args, high_density_tuples) @cmd2.with_argparser(table_parser) def do_object_table(self, args): """Display data in object form on the Earth's most populated cities in a table.""" - self.ptable(EXAMPLE_OBJECT_DATA, OBJ_COLS, args) + self.ptable(EXAMPLE_OBJECT_DATA, OBJ_COLS, args, high_density_objs) if __name__ == '__main__': -- cgit v1.2.1