Skip to content

Commit 981a7ed

Browse files
committed
table and object_table commands now accept argparse arguments for formatting the grid style
1 parent 64f6db1 commit 981a7ed

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

examples/table_display.py

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
WARNING: This example requires the tableformatter module: https://github.com/python-tableformatter/tableformatter
1212
- pip install tableformatter
1313
"""
14-
import functools
14+
import argparse
1515

1616
import cmd2
1717
import tableformatter as tf
@@ -30,14 +30,10 @@
3030
BACK_ALT = ''
3131

3232
# Format to use for the grid style when displaying tables with the tableformatter module
33-
grid_style = tf.AlternatingRowGrid(BACK_PRI, BACK_ALT) # Use rows of alternating color to assist as a visual guide
34-
35-
# Create a function to format a fixed-width table for pretty-printing using the desired table format
36-
table = functools.partial(tf.generate_table, grid_style=grid_style)
33+
DEFAULT_GRID = tf.AlternatingRowGrid(BACK_PRI, BACK_ALT) # Use rows of alternating color to assist as a visual guide
3734

3835

3936
# Formatter functions
40-
4137
def no_dec(num: float) -> str:
4238
"""Format a floating point number with no decimal places."""
4339
return "{}".format(round(num))
@@ -136,28 +132,40 @@ class TableDisplay(cmd2.Cmd):
136132
def __init__(self):
137133
super().__init__()
138134

139-
def ptable(self, tabular_data, headers=()):
135+
def ptable(self, rows, columns, grid_args):
140136
"""Format tabular data for pretty-printing as a fixed-width table and then display it using a pager.
141137
142-
:param tabular_data: required argument - can be a list-of-lists (or another iterable of iterables), a list of
143-
named tuples, a dictionary of iterables, an iterable of dictionaries, a two-dimensional
144-
NumPy array, NumPy record array, or a Pandas dataframe.
145-
:param headers: (optional) - to print nice column headers, supply this argument:
146-
- headers can be an explicit list of column headers
147-
- if `headers="firstrow"`, then the first row of data is used
148-
- if `headers="keys"`, then dictionary keys or column indices are used
149-
- Otherwise, a headerless table is produced
138+
:param rows: required argument - can be a list-of-lists (or another iterable of iterables), a two-dimensional
139+
NumPy array, or an Iterable of non-iterable objects
140+
:param columns: column headers and formatting options per column
141+
:param grid_args: argparse arguments for formatting the grid
150142
"""
151-
formatted_table = table(rows=tabular_data, columns=headers)
143+
if grid_args.color:
144+
grid = tf.AlternatingRowGrid(BACK_PRI, BACK_ALT)
145+
elif grid_args.fancy:
146+
grid = tf.FancyGrid()
147+
elif grid_args.sparse:
148+
grid = tf.SparseGrid()
149+
else:
150+
grid = None
151+
152+
formatted_table = tf.generate_table(rows=rows, columns=columns, grid_style=grid)
152153
self.ppaged(formatted_table, chop=True)
153154

154-
def do_table(self, _):
155+
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')
159+
160+
@cmd2.with_argparser(table_parser)
161+
def do_table(self, args):
155162
"""Display data on the Earth's most populated cities in a table."""
156-
self.ptable(EXAMPLE_ITERABLE_DATA, columns)
163+
self.ptable(EXAMPLE_ITERABLE_DATA, columns, args)
157164

158-
def do_object_table(self, _):
165+
@cmd2.with_argparser(table_parser)
166+
def do_object_table(self, args):
159167
"""Display data on the Earth's most populated cities in a table."""
160-
self.ptable(EXAMPLE_OBJECT_DATA, obj_cols)
168+
self.ptable(EXAMPLE_OBJECT_DATA, obj_cols, args)
161169

162170

163171
if __name__ == '__main__':

0 commit comments

Comments
 (0)