未提供项目说明
snick的Python项目详细描述
用于管理缩进文本的小工具
这个库提供了几个文本操作小工具,这些小工具在 处理文本中的缩进。在以下情况下,您可能会发现它们很有用:
- 记录文本块
- 测试输出
- 以人类可读的方式格式化机器生成的文本
名字是怎么回事?在
动词“indent”没有很好的同义词。但是,那里 在某物上制造一个小凹痕。我的一个 最喜欢的是“snick”。意思是“切开一个小切口”。我 我想我会用那个!在
方法
这些方法中的大多数都有可以使用的附加选项和参数 增加产量。这只是一个粗略的概述。请咨询 源代码获取更多详细信息
dedent
此方法通过将所有行与最左边对齐来取消文本块的缩进
如果您希望在代码中使用python三引号字符串,这非常好, 喜欢在自己的行开始文字,但不希望缩进:
class Whatever:
@staticmethod
def print_some_stuff():
print(snick.dedent("""
Here is some text
here is some other text
we don't want this indented
when it's printed
(to the console)
"""))
调用Whatever.print_some_stuff()
将导致dedented输出:
缩进
此方法缩进文本块。它是textwrap.indent()
周围的薄薄包装。
但是,默认前缀为4。如果你想的话,这个可能很方便
要缩进用换行符连接的文本行:
print(snick.indent('\n'.join([
'would be so nice',
'to indent these',
'i guess',
])))
上面的代码片段将生成:
would be so nice
to indent these
i guess
展开
此方法展开文本块。它通过将所有的线连接到 一根绳子。它也适用于缩进文本。这可能很方便 如果你有一个非常缩进的代码块,你需要键入一个长字符串 出去。您可以展开一个三引号的块:
if True:
if True:
if True:
if True:
if True:
if True:
if True:
if True:
print(snick.unwrap("""
I need to have a very long string here, but
it would go way outside of the line length
limit and cause me all sorts of grief with
the style checker. So, unwrap can help me
here
"""))
上面的代码块将打印:
I need to have a very long string here, but it would go way outside of the line length limit and cause me all sorts of grief with the style checker. So, unwrap can help me here
删除空格
此方法只删除字符串中的所有空白。包括换行符, 制表符、空格等。这种方法对于编写需要忽略的测试非常方便 用于可读性/格式的空白:
print(snick.strip_whitespace("""
some text with whitespace
and whatnot
"""))
上面的代码块将打印出以下内容:
sometextwithwhitespaceandwhatnot
缩进换行
此方法用于包装长字符串并缩进每个换行的行。可能吧 对于包装和缩进一些编程生成的字符串非常有用
print("Here's some filler text:")
print(f" {snick.indent_wrap(lorem.text())}")
上面的代码块可能会生成如下代码:
Here's some filler text:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
漂亮的印花
此方法可用于漂亮地打印词典:
snick.pretty_print("'a': {'b': 1, 'c': {'d': 2}, 'e': 3}, 'f': 4}")
上面的代码块将生成如下格式的输出:
{
'a': {
'b': 1,
'c': {
'd': 2,
},
'e': 3,
},
'f': 4,
}
漂亮的\u格式
此方法与pretty\u print相同,但返回字符串而不是 打印到IO流
去氧化
这个方法只是在文本周围画一个方框。这对于 当你想让某个东西真正弹出时记录:
print(snick.enboxify("""
here's some text that we
want to put into a box.
That will make it look
so very nice
"""))
上面的代码块将产生如下输出:
****************************
* here's some text that we *
* want to put into a box. *
* That will make it look *
* so very nice *
****************************
- 项目
标签: