pyyeti.writer.formheader

pyyeti.writer.formheader(headers, widths, formats, sep=(0, 2), just=-1, ulchar='-')[source]

Form a nice table header for formatted output via f.write().

Parameters:
  • headers (list or tuple) – List or tuple of column header strings, eg: [‘Desc’, ‘Maximum’, ‘Time’]. Can also be a list of lists (or tuples) to support multiple header lines, eg: [[‘Maximum’, ‘Minimum’, ‘Time’], [‘(lbs)’, ‘(lbs)’, ‘(sec)’]]

  • widths (iterable) – Iterable of field widths, eg: (25, 10, 8) or [25, 10, 8]. If an element in widths is < length of corresponding word in a header-line, the length of the word is used for that field. Note that if this doesn’t match with formats, the columns will not line up nicely.

  • formats (list or tuple) – List or tuple of format specifiers for the values in the table, eg: [‘{:25s}’, ‘{:10f}’, ‘{:8.3f}’]

  • sep (string, list, tuple, or integer) –

    Defines ‘spacer’ in front of each word:

    • if a string, that string is used in front of all headers

    • use a list or tuple of strings for complete control

    • if an integer, that many spaces are used in front of all headers

    • use a vector of integers to specify a variable number of spaces

    • if len(sep) < len(headers), the last element is used for all remaining elements

  • just (string or integer or list) –

    Justification flag or flags for each header string:

    • ‘l’, ‘c’, ‘r’ (or -1, 0, 1) to left, center, or right justify headers in their fields

    • can be a list or tuple of len(headers) for complete control

  • ulchar (string) – Character to use for underlining of headers.

Returns:

  • hu (string) – Contains formatted header string(s) and the underline string.

  • f (string) – Final formatting string.

Examples

>>> import numpy as np
>>> import sys
>>> from pyyeti import writer
>>> descs = ['Item 1', 'A different item']
>>> mx = np.array([[1.2, 2.3], [3.4, 4.5]]) * 1000
>>> time = np.array([[1.234], [2.345]])
>>> headers = [['The']*3, ['Descriptions', 'Maximum', 'Time']]
>>> formats = ['{:<25s}', '{:10.2f}', '{:8.3f}']
>>> widths  = [25, 10, 8]
>>> hu, f = writer.formheader(headers, widths, formats,
...                           sep=[4, 5, 2], just=0)
>>> fout = sys.stdout
>>> if 1:   # just so all output is together
...     b = fout.write(hu)
...     writer.vecwrite(fout, f, descs, mx, time)
               The                   The        The
           Descriptions            Maximum      Time
    -------------------------     ----------  --------
    Item 1                           1200.00  2300.000
    A different item                 3400.00  4500.000