行文本格式化程序的集合

line-formater的Python项目详细描述


说明

包含文本行格式化程序方法集合的类。 每个方法都得到相同的签名(除了dictionary和派生)。

强制输入:任何具有__str__方法或对象列表的对象。

output:固定长度的字符串。

简单上下文

字符串是在给定length的概念区域中格式化的。 两边都有填充(见pad参数),它们是额外的间距,减少了内容区域。 左填充和右填充可以分别使用l_padr_pad参数指定。 内容可以在内容区域内移动有符号值(请参见shiftaguement)。 如果内容是元素列表,则可以使用separguement指定分隔符。

解释simple context中常见关键字参数的方案

│<┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄length┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄>│
│         ┆                          ┆         │
│<┄l_pad┄>┆       content-zone       ┆<┄r_pad┄>│
│         ┆                          ┆         │
│<┄┄pad┄┄>┆    <┄┄ - shift + ┄┄>     ┆<┄┄pad┄┄>│

示例:

input = ["elt1", "elt2", "elt3"]
length = 18
pad = 2
sep = "*"
output = "  elt1*elt2*elt3  "

多上下文

在多上下文中,给定内容的每个元素在尽可能多的内容区域中格式化。 前面描述的参数仍然可用,并影响主内容区域(而不是内容)。 请参阅lengthpadl_padr_padshiftsep。 所有这些参数都可以使用复数形式分别应用于每个内容区域。 请参阅lengthspadsl_padsr_padsshiftsseps。 如果给这些复数形式一个值,它将应用于所有内容区域。 如果要为每个复数形式设置单独的值,必须给出一个列表。 此外,如果要保留某些内容区域的默认值,则必须在相应位置填充“无”值。

解释多上下文中常见关键字参数的方案

┃<┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄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 + ┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄>      │     ┃

表上下文

表上下文与多上下文非常相似。主要的区别只是一些具有不同默认值的参数:pads被设置为1,tipsep被设置为“”(管道)。

解释表上下文中默认参数的效果的方案

tip               sep                tip
 ┃ ┆             ┆ ┃ ┆              ┆ ┃
 ┃1┆ content[0]  ┆1┃1┆ content[-1]  ┆1┃
 ┃ ┆             ┆ ┃ ┆              ┆ ┃

对齐

对齐类型:

│<┄┄content-zone┄┄>│
│                  │
│elt1 elt2         │  > left (l)
│                  │
│    elt1 elt2     │  > center (c)
│                  │
│         elt1 elt2│  > right (r)
│                  │
│elt1   elt3   elt2│  > spread (s)

方法

简单上下文

内容(单个位置参数)可以是任何类型。

align:multi-purpose simple formater
left:left alignment
center:center alignment
right:right alignment
spread:spread alignment

多上下文

内容必须是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: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

额外格式化程序

等待两个正定参数(键、值)。

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.

位置参数

content:
A single object with __str__ method or an iterable (two elements for special methods ^{tt1}$ and derived).
The elements to format.

关键字参数

单一形式

length:
Positive integer.
Length of formated string.
Default is 80.
just:
Single character.
Justification type among:
“l”: left
“c”: center
“r”: right
“s”: spread (similar to justify)
Default is “l”.
pad:
Positive integer.
Left and right paddings (ie extra spaces).
Paddings reduce the content to keep length.
Default is “l”.
l_pad:
Positive integer.
Left padding (ie extra spaces on left tip).
Default is 0.
r_pad:
Positive integer.
Right padding (ie extra spaces on right tip).
Default is 0.
shift:
Signed integer.
Shift of the content, rightward is positive.
Default is 0.
sep:
String.
Separator between elements if an iterator is given as input.
Default is ” “.
tip:
String.
Characters at left and right tips.
These reduce the content to keep length.
Default is “”.
crop:
Boolean.
True: crop the content that doesn’t match the length.
False: non length matching content is displayed on several lines.
Default is True.

复数形式

lengths:
Positive integer or list of positive integers.
Lengths of each content zones.
Default: None (auto-computed).
justs:
Single character or list of single character.
Flags for alignment types of each content zones.
Default: “l” for ^{tt25}$ and ^{tt26}$ methods.
l_pads:
Positive integer or list of positive integers.
Left and right padding of each content zones.
Paddings reduce the contents to keep lengths.
Default: 0.
l_pads:
Positive integer or list of positive integers.
Left padding of each content zones.
Default: 0.
r_pads:
Positive integer or list of positive integers.
Right padding of each content zones.
Default: 0.
shifts:
Signed integer or list of signed integers.
Shift of each content zones.
Default: 0.
seps:
String or list of Strings.
Separator inserted between content elements of each content zones (if contents are iterables).
Default: ” “.
tips:
String or list of Strings.
Characters inserted at the tips of each content zones.
These reduce the content to keep length.
Default: “”.
crops:
Boolean or list of Booleans.
True: crop the contents that doesn’t match the lengths.
False: non length matching contents are displayed on several lines.
Default is True.

如果list与复数形式一起使用,Nonevalue可用于保持特定列的默认值。

示例

>>> 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    |'

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java使用Spring Boot JPA配置多个数据源   java如何对时间范围进行通用建模,以允许任何时间段或更具体的时间段的范围   java Hystrix拒绝完全未来返回类型   带有MongoDB数据源错误的java Spring安全性   Java中的windows多线程:不同的操作系统提供不同的性能吗?   java使用KeyCode和修饰符获取keyCharValue   JDBC语句中的javasysdate在查询同一个Oracle数据库时似乎返回不同的时间   将具体类型的列表强制转换为Java中的接口列表   java在安卓中经过一段时间后重新执行代码   将数据加载到java时出现NullPointerException。util。属性对象   Java根据对象的名称调用对象并使用它们   java使用Android创建强大的密码生成器   java组织。莫基托。例外情况。滥用。MissingMethodInvocation异常   java Spring数据JPA无法找到id为的对象   datetime如何使用Java计算相对时间跨度(代码转换问题)?   java如何在Android应用程序的新窗口中显示字符串?   java确定应该在其上运行测试用例的目录   java文件io中的向下投射和向上投射?   企业JavaBean中方法的java访问修饰符