A collection of line text formaters
Project description
Description
A class containing a collection of text line formater methods. Each method get the same signature (except dictionary and derived).
Mandatory inputs: any object with __str__ method or list of objects.
Output: string of fixed length.
Simpe context
The string is formated in a conceptual zone of given length. On both sides, there is paddings (see pad argument), which are extra spacing, reducing the content zone. Left and right paddings can be individually specified using l_pad and r_pad arguments respectively. The content can be shifted of a signed value (see shift aguement) inside the content-zone. If the content is a list of elements, one can specify the separator using sep arguement.
Scheme explaining common key word arguments in simple context:
│<┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄length┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄>│ │ ┆ ┆ │ │<┄l_pad┄>┆ content-zone ┆<┄r_pad┄>│ │ ┆ ┆ │ │<┄┄pad┄┄>┆ <┄┄ - shift + ┄┄> ┆<┄┄pad┄┄>│
Example:
input = ["elt1", "elt2", "elt3"] length = 18 pad = 2 sep = "*" output = " elt1*elt2*elt3 "
Multiple context
In multi context, each element of given contents are formated in as many content zones. The previously described arguments are still available, and affect the main content zone (and not the contents). See length, pad, l_pad, r_pad, shift and sep. All these arguments can be applied separately on each content zones using there plural forms. See lengths, pads, l_pads, r_pads, shifts and seps. If a single value is given to these plural form, it will be applied to all the content zones. If one wants to set separate values for each plural form, a list must be given. Also, if one want to keep the default for some content zones, a None value must be filled at corresponding positions.
Scheme explaining common key word arguments in multiple context:
┃<┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄length┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄>┃ ┃ tip tip ┃ ┃ │ main content zone │ ┃ ┃ │ │ ┃ ┃ │<┄┄┄┄┄┄┄┄┄┄┄┄lengths[0]┄┄┄┄┄┄┄┄┄┄┄>│ │<┄┄┄┄┄┄┄┄┄┄┄lengths[-1]┄┄┄┄┄┄┄┄┄┄┄>│ ┃ ┃ │ ┆ ┆ │ │ ┆ ┆ │ ┃ ┃ pad │ pads[0] ┆ contents[0] ┆ pads[0] │sep│pads[-1] ┆ contents[-1] ┆pads[-1] │ pad ┃ ┃ │ ┆ ┆ │ │ ┆ ┆ │ ┃ ┃ │ ┆ <┄shifts[0]┄> ┆ │ │ ┆ <┄shifts[0]┄> ┆ │ ┃ ┃ │ ┆ ┆ │ │ ┆ ┆ │ ┃ ┃ │ │ ┃ ┃ │ <┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄ - shift + ┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄> │ ┃
Table context
Table-context is very similar to multi-context. Main differences are only some arguments with differents default values : pads is set to 1 and tip and sep are set to ‘|’ (pipe).
Scheme explaining effect of default arguments in table context:
tip sep tip ┃ ┆ ┆ ┃ ┆ ┆ ┃ ┃1┆ content[0] ┆1┃1┆ content[-1] ┆1┃ ┃ ┆ ┆ ┃ ┆ ┆ ┃
Alignments
Types of alignment:
│<┄┄content-zone┄┄>│ │ │ │ elt1 elt2 │ > center (c) │ │ │elt1 elt2 │ > left (l) │ │ │ elt1 elt2│ > right (r) │ │ │elt1 elt3 elt2│ > spread (s)
Methods
Simple context
The content (single positional argment) can be of any type.
- align:
multi-purpose simple formater
- left:
left alignment
- center:
center alignment
- right:
right alignment
- spread:
spread alignment
Multiple context
The content MUST be an iterable.
- muti:
multi-purpose formater in multiples zones
- multi_left:
left alignment in multiples zones
- multi_center:
center alignment in multiples zones
- multi_right:
right alignment in multiples zones
- multi_spread:
spread alignments in mutliples zones
Table context
Same as multi-context.
- table:
multi-purpose formater in table
- table_left:
left alignment in table
- table_center:
center alignment in table
- table_right:
right alignment in table
- table_spread:
spread alignments in table
Extra formaters
Two positoinal arguments (key, value) are awaited.
- dictionary:
double alignment for key/value : key is right justified, value is left justified. Separator is ‘:’
- multi_dictionary:
double alignments for multiple zones. Key and value positional arguments MUST be lists.
- table_dictionary:
double alignments for table.
Positional arguments
- content:
- element or list of elements (two elements for special methods dictionary and derived).int, float, string, list, …the elements to format
Key word arguments
single forms
- length:
- positive integertotal length of the returned string
- just:
- single characterflag for alignment types (see alignments section)default: “l” for align method
- pad:
- positive integerleft and right paddingsdefault: 0
- l_pad:
- positive integerleft padding (gets priority over pad)default: 0
- r_pad:
- positive integerright padding (gets priority over pad)default: 0
- shift:
- signed integershift of the content-zone (positive direction is rightward)default: 0
- sep:
- stringseparator inserted between content elementsdefault: ” “
- tip:
- stringelement inserted at the tips of the formated contentsdefault: “”
plural forms
- lengths:
- positive integer or list of positive integerslengths of each content zonesdefault: None (auto-computed)
- justs:
- single character or list of single characterflags for alignment types of each content zonesdefault: “l” for multi_align and table methods
- l_pads:
- positive integer or list of positive integersleft and right padding of each content zonesdefault: 0
- l_pads:
- positive integer or list of positive integersleft padding of each content zonesdefault: 0
- r_pads:
- positive integer or list of positive integersright padding of each content zonesdefault: 0
- shifts:
- signed integer or list of signed integersshift of each content zonesdefault: 0
- seps:
- stringseparator inserted between content elements of each content zonesdefault: ” “
- tips:
- stringelement inserted at the tips of each content zonesdefault: “”
Examples
>>> LF = LineFormater(length=20) >>> center("content") ' content '>>> LF.center("content", shift=+5) ' content '>>> LF.left("content", l_pad=2) ' content '>>> LF.right("content", r_pad=2) ' content '>>> LF.spread(["foo", "bar", "foobar"], pad=1) ' foo bar foobar '>>> LF.multi_center(["elt1", "elt2", "elt3"], length=30) ' elt1 elt2 elt3 '>>> LF.dictionary("my_var", 1) ' my_var: 1 '>>> LF.table_dictionary(["var1", "var2"], [1, 2], length=28) '| var1: 1 | var2: 2 |'
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.