Skip to content

Commit 926c2cf

Browse files
committed
Deleted unused constant for DEFAULT_GRID
Also: - Improved some comments - Put the argparse args for selecting grid style in a mutually exclusive group
1 parent 981a7ed commit 926c2cf

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

examples/table_display.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import cmd2
1717
import tableformatter as tf
1818

19+
# Configure colors for when users chooses the "-c" flag to enable color in the table output
1920
try:
2021
from colored import bg
2122
BACK_PRI = bg(4)
@@ -29,9 +30,6 @@
2930
BACK_PRI = ''
3031
BACK_ALT = ''
3132

32-
# Format to use for the grid style when displaying tables with the tableformatter module
33-
DEFAULT_GRID = tf.AlternatingRowGrid(BACK_PRI, BACK_ALT) # Use rows of alternating color to assist as a visual guide
34-
3533

3634
# Formatter functions
3735
def no_dec(num: float) -> str:
@@ -54,24 +52,24 @@ def two_dec(num: float) -> str:
5452
['Guangzho (广州市)', 'Guangdong', 'China', 'Asia', 13081000, 1347.81],
5553
['Mumbai (मुंबई)', 'Maharashtra', 'India', 'Asia', 12442373, 465.78],
5654
['Istanbul (İstanbuld)', 'Istanbul', 'Turkey', 'Eurasia', 12661000, 620.29],
57-
]
55+
]
5856

5957
# Calculate population density
6058
for row in EXAMPLE_ITERABLE_DATA:
6159
row.append(row[-2]/row[-1])
6260

6361

64-
# # Column headers plus optional formatting info for each column
65-
columns = [tf.Column('City', width=11, header_halign=tf.ColumnAlignment.AlignCenter),
62+
# Column headers plus optional formatting info for each column
63+
COLUMNS = [tf.Column('City', width=11, header_halign=tf.ColumnAlignment.AlignCenter),
6664
tf.Column('Province', header_halign=tf.ColumnAlignment.AlignCenter),
67-
'Country', # NOTE: If you don't need any special effects, you can just pass a string
65+
'Country', # NOTE: If you don't need any special effects, you can just pass a string
6866
tf.Column('Continent', cell_halign=tf.ColumnAlignment.AlignCenter),
6967
tf.Column('Population', cell_halign=tf.ColumnAlignment.AlignRight, formatter=tf.FormatCommas()),
7068
tf.Column('Area (km²)', width=7, header_halign=tf.ColumnAlignment.AlignCenter,
7169
cell_halign=tf.ColumnAlignment.AlignRight, formatter=two_dec),
7270
tf.Column('Pop. Density (/km²)', width=12, header_halign=tf.ColumnAlignment.AlignCenter,
7371
cell_halign=tf.ColumnAlignment.AlignRight, formatter=no_dec),
74-
]
72+
]
7573

7674

7775
# ######## Table data formatted as an iterable of python objects #########
@@ -105,13 +103,14 @@ def pop_density(data: CityInfo):
105103

106104
# Convert the Iterable of Iterables data to an Iterable of non-iterable objects for demonstration purposes
107105
EXAMPLE_OBJECT_DATA = []
108-
for city in EXAMPLE_ITERABLE_DATA:
109-
EXAMPLE_OBJECT_DATA.append(CityInfo(city[0], city[1], city[2], city[3], city[4], city[5]))
106+
for city_data in EXAMPLE_ITERABLE_DATA:
107+
# Pass all city data other than population density to construct CityInfo
108+
EXAMPLE_OBJECT_DATA.append(CityInfo(*city_data[:-1]))
110109

111110
# If table entries are python objects, all columns must be defined with the object attribute to query for each field
112111
# - attributes can be fields or functions. If a function is provided, the formatter will automatically call
113112
# the function to retrieve the value
114-
obj_cols = [tf.Column('City', attrib='city', header_halign=tf.ColumnAlignment.AlignCenter),
113+
OBJ_COLS = [tf.Column('City', attrib='city', header_halign=tf.ColumnAlignment.AlignCenter),
115114
tf.Column('Province', attrib='province', header_halign=tf.ColumnAlignment.AlignCenter),
116115
tf.Column('Country', attrib='country'),
117116
tf.Column('Continent', attrib='continent', cell_halign=tf.ColumnAlignment.AlignCenter),
@@ -121,7 +120,7 @@ def pop_density(data: CityInfo):
121120
cell_halign=tf.ColumnAlignment.AlignRight, formatter=two_dec),
122121
tf.Column('Pop. Density (/km²)', width=12, header_halign=tf.ColumnAlignment.AlignCenter,
123122
cell_halign=tf.ColumnAlignment.AlignRight, obj_formatter=pop_density),
124-
]
123+
]
125124

126125
# TODO: Color row text foreground based on population density
127126

@@ -153,19 +152,20 @@ def ptable(self, rows, columns, grid_args):
153152
self.ppaged(formatted_table, chop=True)
154153

155154
table_parser = argparse.ArgumentParser()
156-
table_parser.add_argument('-c', '--color', action='store_true', help='Enable color')
157-
table_parser.add_argument('-f', '--fancy', action='store_true', help='Fancy Grid')
158-
table_parser.add_argument('-s', '--sparse', action='store_true', help='Sparse Grid')
155+
table_item_group = table_parser.add_mutually_exclusive_group()
156+
table_item_group.add_argument('-c', '--color', action='store_true', help='Enable color')
157+
table_item_group.add_argument('-f', '--fancy', action='store_true', help='Fancy Grid')
158+
table_item_group.add_argument('-s', '--sparse', action='store_true', help='Sparse Grid')
159159

160160
@cmd2.with_argparser(table_parser)
161161
def do_table(self, args):
162-
"""Display data on the Earth's most populated cities in a table."""
163-
self.ptable(EXAMPLE_ITERABLE_DATA, columns, args)
162+
"""Display data in iterable form on the Earth's most populated cities in a table."""
163+
self.ptable(EXAMPLE_ITERABLE_DATA, COLUMNS, args)
164164

165165
@cmd2.with_argparser(table_parser)
166166
def do_object_table(self, args):
167-
"""Display data on the Earth's most populated cities in a table."""
168-
self.ptable(EXAMPLE_OBJECT_DATA, obj_cols, args)
167+
"""Display data in object form on the Earth's most populated cities in a table."""
168+
self.ptable(EXAMPLE_OBJECT_DATA, OBJ_COLS, args)
169169

170170

171171
if __name__ == '__main__':

0 commit comments

Comments
 (0)