Text Align - Python Text Alignment

API

Text Align - Python Text Alignment.

class aligntext.Align(rtrim=False, strip_empty_cols=False)[source]

Bases: object

Align table data.

add_row(*cols)[source]

Add a row with cols.

Column values are accepted as tuple, list or positional arguments.

The data is always extended to the width of the broadest row.

>>> al = Align()
>>> al.add_row([1, 2, 3])     # list
>>> al.add_row((4, 5, 6, 7))  # tuple
>>> al.add_row(8, 9, 10)      # positional arguments
>>> for row in al:
...     row
'1 2 3   '
'4 5 6  7'
'8 9 10  '
add_rows(*rows)[source]

Add rows.

Rows are accepted as tuple, list or positional arguments.

The data is always extended to the width of the broadest row.

>>> al = Align()
>>> al.add_rows([[11, 12, 13], [14, 15, 16]])  # list of lists
>>> al.add_rows(((21, 22, 23), (24, 25, 26)))  # tuple of tuples
>>> al.add_rows((31, 32, 33), [34, 35, 36])    # positional arguments
>>> for row in al:
...     row
'11 12 13'
'14 15 16'
'21 22 23'
'24 25 26'
'31 32 33'
'34 35 36'
add_spacer(spacer=None)[source]

Add unaligned spacer.

>>> al = Align(rtrim=True)
>>> al.add_row(111, 12, 13)
>>> al.add_spacer()
>>> al.add_row(31, 321, 33)
>>> al.add_spacer("===============")
>>> al.add_row(21, 22, 231)
>>> for row in al:
...     row
'111 12  13'
''
'31  321 33'
'==============='
'21  22  231'
add_spacers(*spacers)[source]

Add unaligned spacers.

>>> al = Align()
>>> al.add_row(111, 12, 13)
>>> al.add_spacers("---", None)
>>> al.add_row(31, 321, 33)
>>> al.add_spacers("===============")
>>> al.add_row(21, 22, 231)
>>> for row in al:
...     row
'111 12  13 '
'---'
''
'31  321 33 '
'==============='
'21  22  231'
clear()[source]

Remove all added rows.

get()[source]

Return aligned data.

static norm(cell)[source]

Normalize function for any table cell.

Return an empty string, when cell is None, otherwise convert cell to string.

This static method can be overwritten to implement any other type behaviour.

set_alignments(*alignments)[source]

Set the alignment for every column.

All left aligned is the default.

If there are less alignments specified than columns available, the last alignment is used for all remaining columns. If there are more alignments specified than columns available, the dispensable ones are ignored.

Alignments shall be left, center, right or any function with the arguments cell, width, which returns an aligned string.

Example: >>> al = Align() >>> al.add_rows([[1, 33, 1.001], … [“foo”, “barcelona”, “tschustify”], … ]) >>> al.set_alignments(center) >>> for row in al: … row ‘ 1 33 1.001 ‘ ‘foo barcelona tschustify’

>>> al.set_alignments(center, right)
>>> for row in al:
...     row
' 1         33      1.001'
'foo barcelona tschustify'
>>> al.set_alignments(center, right, left, right)
>>> for row in al:
...     row
' 1         33 1.001     '
'foo barcelona tschustify'
>>> al.set_alignments()
>>> for row in al:
...     row
'1   33        1.001     '
'foo barcelona tschustify'
set_separators(*separators, first=None, last=None, rtrim=None)[source]

Set the column separators.

Parameters:

*separators (str) – separators. Default is ” “.

Keyword Arguments:
  • first (str) – prefix for every row. Empty by default.

  • last (str) – suffix for every row. Empty by default.

  • rtrim – Remove whitespaces at the end of the line. Do not change setting by default.

If there are less separators specified than columns available, the last alignment is used for all remaining columns. If there are more separators specified than columns available, the dispensable ones are ignored.

Example: >>> al = Align() >>> al.add_rows([“a”, “bb”, “ccc”], [“zz”, “yyyy”, “x”]) >>> al.set_separators(“|”, rtrim=True) >>> for row in al: … row ‘a |bb |ccc’ ‘zz|yyyy|x’

>>> al.set_separators("|", "=", "&")
>>> for row in al:
...     row
'a |bb  =ccc&'
'zz|yyyy=x  &'
>>> al.set_separators("|", first="> ", last=" <")
>>> for row in al:
...     row
'> a |bb  |ccc <'
'> zz|yyyy|x   <'
>>> al.set_separators("|", "=", "&", last="END")
>>> for row in al:
...     row
'a |bb  =cccEND'
'zz|yyyy=x  END'
aligntext.align(*rows, seps=None, sepfirst=None, seplast=None, alignments=None, rtrim=True, strip_empty_cols=False, header=None)[source]

Align rows using separators seps and align to alignments.

Keyword Arguments:
  • seps (list, tuple) – separators (see set_separators()).

  • sepfirst (str) – prefix for every row. Empty by default. (see set_separators()).

  • seplast (str) – suffix for every row. Empty by default. (see set_separators()).

  • alignments (list, tuple) – alignments (see set_alignments()).

  • rtrim (bool) – Remove whitespaces at the end of the line.

  • strip_empty_cols (bool) – Skip the column and the seperator of empty columns.

>>> print(align([["a", "bb", "ccc"], ["zzz", "yyyy", "x"]], rtrim=True))
a   bb   ccc
zzz yyyy x
>>> print(align([["a", "bb", "ccc"], ["zzz", "yyyy", "x"]], header=('Col1', 'C2', 'Column3'),
...       rtrim=True))
Col1 C2   Column3
---- --   -------
a    bb   ccc
zzz  yyyy x
>>> print(align(["a", "bb", "ccc"], ["zzz", "yyyy", "x"], seps=["|"], sepfirst="B", seplast="E",
...       rtrim=True))
Ba  |bb  |cccE
Bzzz|yyyy|x  E
>>> print(align(["a", "bb", "ccc"], ["zzz", "yyyy", "x"], alignments=(center, right), rtrim=True))
 a    bb ccc
zzz yyyy   x

Generators are also allowed:

>>> def myiter(num):
...     for idx in range(num):
...         yield idx
>>> def myiteriter(rows, cols):
...     for idx in range(rows):
...         yield myiter(cols)
>>> align(myiteriter(2, 3))
'0 1 2\n0 1 2'
aligntext.center(cell, width)[source]

Center cell to width.

aligntext.iter_align(*rows, seps=None, sepfirst=None, seplast=None, alignments=None, rtrim=True, strip_empty_cols=False)[source]

Iterate over aligned rows using separators seps and align to alignments.

Keyword Arguments:
  • seps (list, tuple) – separators (see set_separators()).

  • sepfirst (str) – prefix for every row. Empty by default. (see set_separators()).

  • seplast (str) – suffix for every row. Empty by default. (see set_separators()).

  • alignments (list, tuple) – alignments (see set_alignments()).

  • rtrim (bool) – Remove whitespaces at the end of the line.

  • strip_empty_cols (bool) – Skip the column and the seperator of empty columns.

>>> '\n'.join(iter_align([["a", "bb", "ccc"], ["zzz", "yyyy", "x"]]))
'a   bb   ccc\nzzz yyyy x'
>>> '\n'.join(iter_align(["a", "bb", "ccc"], ["zzz", "yyyy", "x"], seps=["|"], sepfirst="B", seplast="E"))
'Ba  |bb  |cccE\nBzzz|yyyy|x  E'
>>> '\n'.join(iter_align(["a", "bb", "ccc"], ["zzz", "yyyy", "x"], alignments=(center, right)))
' a    bb ccc\nzzz yyyy   x'

Generators are also allowed:

>>> def myiter(num):
...     for idx in range(num):
...         yield idx
>>> def myiteriter(rows, cols):
...     for idx in range(rows):
...         yield myiter(cols)
>>> '\n' .join(iter_align(myiteriter(2, 3)))
'0 1 2\n0 1 2'
aligntext.left(cell, width)[source]

Justify cell to left with width.

aligntext.right(cell, width)[source]

Justify cell to right with width.

Indices and tables