16
16
import cmd2
17
17
import tableformatter as tf
18
18
19
+ # Configure colors for when users chooses the "-c" flag to enable color in the table output
19
20
try :
20
21
from colored import bg
21
22
BACK_PRI = bg (4 )
29
30
BACK_PRI = ''
30
31
BACK_ALT = ''
31
32
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
-
35
33
36
34
# Formatter functions
37
35
def no_dec (num : float ) -> str :
@@ -54,24 +52,24 @@ def two_dec(num: float) -> str:
54
52
['Guangzho (广州市)' , 'Guangdong' , 'China' , 'Asia' , 13081000 , 1347.81 ],
55
53
['Mumbai (मुंबई)' , 'Maharashtra' , 'India' , 'Asia' , 12442373 , 465.78 ],
56
54
['Istanbul (İstanbuld)' , 'Istanbul' , 'Turkey' , 'Eurasia' , 12661000 , 620.29 ],
57
- ]
55
+ ]
58
56
59
57
# Calculate population density
60
58
for row in EXAMPLE_ITERABLE_DATA :
61
59
row .append (row [- 2 ]/ row [- 1 ])
62
60
63
61
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 ),
66
64
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
68
66
tf .Column ('Continent' , cell_halign = tf .ColumnAlignment .AlignCenter ),
69
67
tf .Column ('Population' , cell_halign = tf .ColumnAlignment .AlignRight , formatter = tf .FormatCommas ()),
70
68
tf .Column ('Area (km²)' , width = 7 , header_halign = tf .ColumnAlignment .AlignCenter ,
71
69
cell_halign = tf .ColumnAlignment .AlignRight , formatter = two_dec ),
72
70
tf .Column ('Pop. Density (/km²)' , width = 12 , header_halign = tf .ColumnAlignment .AlignCenter ,
73
71
cell_halign = tf .ColumnAlignment .AlignRight , formatter = no_dec ),
74
- ]
72
+ ]
75
73
76
74
77
75
# ######## Table data formatted as an iterable of python objects #########
@@ -105,13 +103,14 @@ def pop_density(data: CityInfo):
105
103
106
104
# Convert the Iterable of Iterables data to an Iterable of non-iterable objects for demonstration purposes
107
105
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 ]))
110
109
111
110
# If table entries are python objects, all columns must be defined with the object attribute to query for each field
112
111
# - attributes can be fields or functions. If a function is provided, the formatter will automatically call
113
112
# 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 ),
115
114
tf .Column ('Province' , attrib = 'province' , header_halign = tf .ColumnAlignment .AlignCenter ),
116
115
tf .Column ('Country' , attrib = 'country' ),
117
116
tf .Column ('Continent' , attrib = 'continent' , cell_halign = tf .ColumnAlignment .AlignCenter ),
@@ -121,7 +120,7 @@ def pop_density(data: CityInfo):
121
120
cell_halign = tf .ColumnAlignment .AlignRight , formatter = two_dec ),
122
121
tf .Column ('Pop. Density (/km²)' , width = 12 , header_halign = tf .ColumnAlignment .AlignCenter ,
123
122
cell_halign = tf .ColumnAlignment .AlignRight , obj_formatter = pop_density ),
124
- ]
123
+ ]
125
124
126
125
# TODO: Color row text foreground based on population density
127
126
@@ -153,19 +152,20 @@ def ptable(self, rows, columns, grid_args):
153
152
self .ppaged (formatted_table , chop = True )
154
153
155
154
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' )
159
159
160
160
@cmd2 .with_argparser (table_parser )
161
161
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 )
164
164
165
165
@cmd2 .with_argparser (table_parser )
166
166
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 )
169
169
170
170
171
171
if __name__ == '__main__' :
0 commit comments