9. pypsi.format - Stream output formatting

Provides functions and classes for dealing with command line input and output.

class pypsi.format.Column(text='', mode=0)[source]

Bases: object

A table column.

Parameters:
  • text (str) – the column name
  • mode (int) – the column size mode
Grow = 1

Size mode to have the column grow to the maximum width it can have

Shrink = 0

Size mode to have the column shrink to its contents

class pypsi.format.FixedColumnTable(widths)[source]

Bases: object

A table that has preset column widths.

Parameters:widths (list) – the list of column widths (int)
add_cell(fp, col)[source]

Add a single cell to the table. The current row is printed if the column completes the row.

flush(fp)[source]

Force a write of the table.

Parameters:fp (file) – the output file stream
write_row(fp, *args)[source]

Print a single row.

Parameters:
  • fp (file) – the output file stream (usually sys.stdout or sys.stderr)
  • args (list) – the column values for the row
class pypsi.format.Table(columns, width=80, spacing=1, header=True)[source]

Bases: object

Variable width table.

Parameters:
  • columns (multiple) – a list of either class:Column instances or a single int defining how many columns to create
  • width (int) – the maximum width of the entire table
  • spacing (int) – the amount of space characters to display between columns
  • header (bool) – whether to display header row with the column names
append(*args)[source]

Add a row to the table.

Parameters:args (list) – the column values
extend(*args)[source]

Add multiple rows to the table, each argument should be a list of column values.

write(fp)[source]

Print the table to a specified file stream.

Parameters:fp (file) – output stream
pypsi.format.file_size_str(value)[source]

Get the human readable file size string from a number. This will convert a value of 1024 to 1Kb and 1048576 to 1Mb.

Parameters:value (int) – the value to convert
Returns str:the human readable file size string
pypsi.format.get_lines(txt)[source]

Break text to individual lines.

Returns tuple:a tuple containing the next line and whether the line contained a newline charater.
pypsi.format.highlight(target, term, color='1;32')[source]

Find and highlight a term inside of a block of text.

Parameters:
  • target (str) – the text to search in
  • term (str) – the term to search for
  • color (str) – the color to output when the term is found
Returns str:

the input string with all occurrences of the term highlighted

pypsi.format.obj_str(obj, max_children=3, stream=None)[source]

Pretty format an object with colored type information. Examples:

  • list: list( item1, item2, item3, ...)
  • bool: bool( True )
  • None: <null>
Parameters:
  • obj (object) – object to format
  • max_children (int) – maximum number of children to print for lists
  • stream (file) – target stream, used to determine if color will be used.
Returns str:

the formatted object

pypsi.format.wrap_line(txt, width, wrap_prefix=None)[source]

Word wrap a single line.

Parameters:
  • txt (str) – the line to wrap
  • width (int) – the maximum width of a wrapped line
Returns str:

the next wrapped line