Package lib :: Package text :: Module table
[hide private]
[frames] | no frames]

Module table

source code

Functions for the text formatting of tables.

Functions [hide private]
str
_blank(width=None, prefix=' ', postfix=' ')
Create a blank line for the table.
source code
 
_convert_to_string(data=None, justification=None, custom_format=None)
Convert all elements of the given data structures to strings in place.
source code
 
_determine_widths(data=None, widths=None, separator=None)
Determine the maximum column widths needed given the data.
source code
str
_rule(width=None, prefix=' ', postfix=' ')
Create a horizontal rule for the table.
source code
str
_table_line(text=None, widths=None, separator=' ', pad_left=' ', pad_right=' ', prefix=' ', postfix=' ', justification=None)
Format a line of a table.
source code
str
format_table(headings=None, contents=None, max_width=None, separator=' ', pad_left=' ', pad_right=' ', prefix=' ', postfix=' ', custom_format=None, spacing=False, debug=False)
Format and return the table as text.
source code
Variables [hide private]
  MULTI_COL = '@@MULTI@@'
  __package__ = 'lib.text'

Imports: deepcopy, wrap, is_float, RelaxError


Function Details [hide private]

_blank(width=None, prefix=' ', postfix=' ')

source code 

Create a blank line for the table.

Parameters:
  • width (int) - The total width of the table.
  • prefix (str) - The text to add to the start of the line.
  • postfix (str) - The text to add to the end of the line.
Returns: str
The rule.

_convert_to_string(data=None, justification=None, custom_format=None)

source code 

Convert all elements of the given data structures to strings in place.

Parameters:
  • data (list of lists) - The headings or content to convert.
  • justification (list of lists) - The structure to store the cell justification in.
  • custom_format (None or list of None and str) - This list allows a custom format to be specified for each column. The number of elements must match the number of columns. If an element is None, then the default will be used. Otherwise the elements must be valid string formatting constructs.

_determine_widths(data=None, widths=None, separator=None)

source code 

Determine the maximum column widths needed given the data.

Parameters:
  • data (list of lists of str) - Either the headings or content converted to strings to check the widths of.
  • widths (list of int) - The list of widths to start with. If data is found to be wider than this list, then the width of that column will be expanded.
  • separator (str) - The column separation string.

_rule(width=None, prefix=' ', postfix=' ')

source code 

Create a horizontal rule for the table.

Parameters:
  • width (int) - The total width of the table.
  • prefix (str) - The text to add to the start of the line.
  • postfix (str) - The text to add to the end of the line.
Returns: str
The rule.

_table_line(text=None, widths=None, separator=' ', pad_left=' ', pad_right=' ', prefix=' ', postfix=' ', justification=None)

source code 

Format a line of a table.

Parameters:
  • text (list of str or None) - The list of table elements. If not given, an empty line will be be produced.
  • widths (list of int) - The list of column widths for the table.
  • separator (str) - The column separation string.
  • pad_left (str) - The string to pad the left side of the table with.
  • pad_right (str) - The string to pad the right side of the table with.
  • prefix (str) - The text to add to the start of the line.
  • postfix (str) - The text to add to the end of the line.
  • justification (list of str) - The cell justification structure. The elements should be 'l' for left justification and 'r' for right.
Returns: str
The table line.

format_table(headings=None, contents=None, max_width=None, separator=' ', pad_left=' ', pad_right=' ', prefix=' ', postfix=' ', custom_format=None, spacing=False, debug=False)

source code 

Format and return the table as text.

If the heading or contents contains the value of the MULTI_COL constant defined in this module, then that cell will be merged with the previous cell to allow elements to span multiple columns.

Parameters:
  • headings (list of lists of str) - The table header.
  • contents (list of lists of str) - The table contents.
  • max_width (int) - The maximum width of the table.
  • separator (str) - The column separation string.
  • pad_left (str) - The string to pad the left side of the table with.
  • pad_right (str) - The string to pad the right side of the table with.
  • prefix (str) - The text to add to the start of the line.
  • postfix (str) - The text to add to the end of the line.
  • custom_format (None or list of None and str) - This list allows a custom format to be specified for each column. The number of elements must match the number of columns. If an element is None, then the default will be used. Otherwise the elements must be valid string formatting constructs.
  • spacing (bool) - A flag which if True will add blank line between each row.
  • debug (bool) - A flag which if True will activate a number of debugging printouts.
Returns: str
The formatted table.