probes.probes targets
API Reference Manual  /  Python API  / 

table

Name

table — Create tables and print them as text.

Synopsis

Python

import table

Description

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

Synopsis

class table.StreamTable(key_value_def, border_style=None)

Description

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.

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.

Exceptions

table.TableException if the properties are not valid.

See Also

table.Table

table.StreamTable.add_row

Synopsis

table.StreamTable.add_row(row_data)

Description

Return the text for one row of the table.

row_data is a list with one value per column.

Return Value

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.

Exceptions

table.TableException if row_data does not have one value per column.

class table.Table

Synopsis

class table.Table(key_value_def, data, show_all_columns=False, prefs=None)

Description

A table that can be formatted as text.

The keys in key_value_def are integer constants of three types. All of them are defined in the table module:

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.

Exceptions

table.TableException if the properties or data are not valid.

Examples

# 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))

See Also

table.new_table_command, table, table_key_t, column_key_t, extra_header_key_t

table.Table.csv_export

Synopsis

table.Table.csv_export(filename)

Description

Write the table to the CSV file filename.

table.Table.sortable_columns

Synopsis

table.Table.sortable_columns()

Description

Return the names of the columns that the table can be sorted on.

table.Table.to_string

Synopsis

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
)

Description

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:

Return Value

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

Synopsis

class table.TableException

Description

Raised when the properties or data of a table are not valid.

table.column_names

Synopsis

table.column_names(prop_list)

Description

Gets hold of column names from a property list.

Help function to retrieve the column names embedded in the list of table properties.

See Also

table.show

table.default_table_args

Synopsis

table.default_table_args(set_dict={})

Description

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.

SEE ALSO

table.new_table_command table.get

table.get

Synopsis

table.get(properties, data, *table_args)

Description

Fetch the formatted table.

Similar to the table.show but this function returns the table as a multi-line string.

See Also

table.new_table_command, table.get

table.get_table_arg_value

Synopsis

table.get_table_arg_value(arg, table_values)

Description

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

Synopsis

table.new_table_command(
    command_name,
    cmd_func,
    args=None,
    doc='',
    sortable_columns=None,
    **kwargs
)

Description

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.

SEE ALSO

cli.new_command, table.show

table.new_tech_preview_table_command

Synopsis

table.new_tech_preview_table_command(
    name,
    feature,
    fun,
    args=[],
    doc='',
    sortable_columns=None,
    **kwargs
)

Description

Define a new tech preview CLI table command.

table.new_unsupported_table_command

Synopsis

table.new_unsupported_table_command(
    name,
    feature,
    fun,
    args=[],
    doc='',
    sortable_columns=None,
    **kwargs
)

Description

Define a new unsupported CLI table command.

table.show

Synopsis

table.show(properties, data, *table_args)

Description

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.

See Also

table.new_table_command, table.get

probes.probes targets