column_print

This module provides Class: ColumnPrinter, and Function: print_list.

ColumnPrinter: A context manager class, can print any sequence of strings without knowing the length or number of strings in advance.

print_list: A function, prints a list to the terminal in equally spaced columns.

Note

This module is NOT intended for printing tables, but simply to display long lists more conveniently. For printing tables, tabulate or columnize may be more suitable.

Module: column_print

Print in columns.

Unlike many similar utilities, it is NOT necessary for the strings to be in a list before printing. column_print can print any sequence of strings without knowing the length or number of strings in advance.

Note that this is NOT intended for printing tables, but simply to display a long list more conveniently. For printing tables, “tabulate” or “columnize” may be more suitable.

ColumnPrinter

A context manager class to print strings with padding to form columns.

param columns

int, default=2 The number of columns in layout.

param width

int, default=80 Total width of layout. If not supplied, column_print will attempt to read the width of the terminal (using shutil), and will fall back to 80 character with if the terminal with cannot be determined.

param colsep

str, default=” ” Column separator.

Note that if a string is longer than the width of a column, it will occupy more than one column, and the next printed item will be shifted to the next available column.

Usage:

with ColumnPrinter(columns=int, width=int, colsep=str) as VAR:
    VAR(<printable-expression>)

Class: ColumnPrinter

class column_print.ColumnPrinter(columns=2, width=None, colsep=' ')

Context manager class for printing columns.

columns

Number of columns in layout.

Type

int, default=2

width

Total width of layout (in characters).

If not supplied, ColumnPrinter attempts to calculate width of terminal.

Type

int, default=80

colsep

Column separator.

Type

str, default=” “


Function: print_list

column_print.print_list(data, width=None, colsep=' ')

Print a list of items in equal spaced columns.

The column width is calculated so that most, if not all the printed items fit in a single column, but if there is an occasional longer item, it may occupy two or more columns.

Note that this is NOT intended for printing tables, but simply to display a long list more conveniently.

Parameters
  • data (list) – The list of printable items.

  • width (int, default=80) – Total width of layout. If not supplied, column_print will attempt to read the width of the terminal (using shutil), and will fall back to 80 character with if the terminal with cannot be determined.

  • colsep (str, default=" ") – Column separator.

Usage:

column_print.print_list(list-of-printables, width=int, colsep=str)