table — Create tables and print them as text.
import table
Use table.Table to build a table from a list of rows and print it with table.Table.to_string. Use table.StreamTable to print a table one row at a time. Use table.new_table_command to add a CLI command that prints a table, with the standard table arguments for sorting and formatting.
The Table_Key_*, Column_Key_* and Extra_Header_Key_* constants set the properties of a table. See the table interface for what they mean.
class table.StreamTable(key_value_def, border_style=None)
A table that is printed one row at a time.
Use this when the rows are not known in advance, for example when they are printed while the simulation runs.
key_value_def: a list of key/value pairs setting table properties, in the same format as for table.Table.border_style: the border style to use. If not given, the cli_table_border_style preference is used.The column widths are set when the table is created. A column is as wide as its Column_Key_Width property, or as its name if that property is not set. Longer values are wrapped.
The Table_Key_Stream_Header_Repeat property sets how many lines are printed before the header is printed again. The default is the height of the terminal.
table.TableException if the properties are not valid.
table.StreamTable.add_row(row_data)
Return the text for one row of the table.
row_data is a list with one value per column.
The text for the row. It can have several lines, and ends with a newline. The header is added before the row for the first row, and again each time enough lines have been printed, see table.StreamTable.
table.TableException if row_data does not have one value per column.
class table.Table(key_value_def, data, show_all_columns=False, prefs=None)
A table that can be formatted as text.
key_value_def: a list of key/value pairs setting table properties for the table.data: the two-dimensional data, where the inner list contains the columns and the outer list the rows of the table. All rows must have the same number of columns.show_all_columns: see below.prefs: not used. The settings are read from conf.prefs instead, see table.Table.to_string.The keys in key_value_def are integer constants of three types. All of them are defined in the table module:
table_key_t (Table_Key_*): the keys of the top level list.column_key_t (Column_Key_*): the keys used for each column. The value of Table_Key_Columns is a list with one entry per column, and each entry is a list of Column_Key_* key/value pairs.extra_header_key_t (Extra_Header_Key_*): the keys used for extra header rows. The value of Table_Key_Extra_Headers is a list of [Extra_Header_Key_Row, elements] pairs, where each element is a list of Extra_Header_Key_* key/value pairs.The values are strings, integers, floats, booleans or lists, depending on the key. See the table interface for what each key means and which value it takes.
By default, columns with the Column_Key_Hide_Homogeneous property are hidden when all their values are the same, and percent columns are only added for the column used for sorting. Set show_all_columns to True to always show them.
When the table is printed by an interactive CLI command and is wider than the terminal, the columns are made narrower to fit.
For output that is printed one row at a time, use table.StreamTable.
table.TableException if the properties or data are not valid.
# Create a table and print it as a string.
import table
props = [[table.Table_Key_Name, "Fruits"],
[table.Table_Key_Columns, [
[[table.Column_Key_Name, "Name"]],
[[table.Column_Key_Name, "Count"],
[table.Column_Key_Int_Radix, 10]]]]]
data = [["apple", 3]]
print(table.Table(props, data).to_string(no_row_column=True))
table.new_table_command, table, table_key_t, column_key_t, extra_header_key_t
table.Table.csv_export(filename)
Write the table to the CSV file filename.
table.Table.sortable_columns()
Return the names of the columns that the table can be sorted on.
table.Table.to_string(
sort_col=None,
reverse=None,
rows_printed=40,
float_decimals=None,
border_style=None,
force_max_width=None,
verbose=False,
no_row_column=False,
no_footers=False,
start_row=1,
ignore_column_widths=False
)
Return the formatted table as a multi-line string.
The keyword arguments match the standard table command arguments, see table.new_table_command(): sort_col, reverse, rows_printed (default 40, 0 means all), float_decimals, border_style, force_max_width, verbose, no_row_column, no_footers, start_row and ignore_column_widths.
The settings are read from conf.prefs each time this method is called, not when the table is created:
output_radix and output_grouping set how numbers are shown.cli_table_border_style is used if no border_style is given.cli_table_maximum_width is used if no force_max_width is given and the preference is larger than 0.The table as a multi-line string. If verbose is true, the table name and description are added before the table, and a table with the column descriptions after it. If all columns are hidden, the string is empty.
class table.TableException
Raised when the properties or data of a table are not valid.
table.column_names(prop_list)
Gets hold of column names from a property list.
Help function to retrieve the column names embedded in the list of table properties.
table.default_table_args(set_dict={})
Gets default table arguments.
Return the tuple representing the default table arguments. The
set_dict can contain key/value elements overriding the default value. The key is the same as the name of the table argument in the command.
table.get(properties, data, *table_args)
Fetch the formatted table.
Similar to the table.show but this function returns the table as a multi-line string.
table.get_table_arg_value(arg, table_values)
Retrieve the value for a table command argument.
Allows the user command to look at a certain named parameter to get
its value. For example, to internally make use of the -verbose flag. The arg argument specifies the argument to fetch the value of. The table_values argument is the tuple of table arguments received in the table.new_table_command() callback function.
table.new_table_command(
command_name,
cmd_func,
args=None,
doc='',
sortable_columns=None,
**kwargs
)
Register a new command which prints a table.
Register a specific command for printing the table, with all the command arguments from the print-table commands, but extendable with personalized argument handling. For example, a dedicated command could print out additional data, or filter out certain rows from the table and only print those.
All arguments here are similar to the cli.new_command() function except the sortable_columns argument.
The cmd_func function will be called with additional table argument last. The function should use the table.show() to produce the table output according to the user arguments.
If the sortable_columns argument is supplied, it should contain a list of the column namnes which can be used for sorting. This is only used for the expander function. If this argument is not set, the default expander will try to use the table interface to retrieve the column names.
table.new_tech_preview_table_command(
name,
feature,
fun,
args=[],
doc='',
sortable_columns=None,
**kwargs
)
Define a new tech preview CLI table command.
table.new_unsupported_table_command(
name,
feature,
fun,
args=[],
doc='',
sortable_columns=None,
**kwargs
)
Define a new unsupported CLI table command.
table.show(properties, data, *table_args)
Format and print the table.
This function should be used from a command function registered through the table.new_table_command() function to print out the table according to the user arguments. The properties argument is a list of key/value pairs setting table properties for the table. The data argument contains the two-dimensional data to be printed where the inner list contains the columns and the outer list the rows of the table. See the table interface for more information.
The *table_args argument represents the standard table arguments, received last in the table.new_table_command() callback function.