一个轻量级控制台打印和格式化工具包
wasabi的Python项目详细描述
wasabi:一个轻量级控制台打印和格式化工具包
多年来,我已经编写了无数的着色和 格式化实用程序以在库中输出消息,如 spaCy,Thinc和 Prodigy尽管还有许多其他伟大的开源 选择,我总是想要一些稍微不同或稍有不同的东西 习惯。
此包仍在进行中,旨在将这些实用程序捆绑到 一种标准化的方式,以便它们可以在我们的其他项目中共享。太棒了 轻量级,没有依赖关系,可以跨Python2和3工作
常见问题
你打算增加更多的功能吗?
是的,还有一些助手和功能需要移植。然而,新的 我们(认为我们)需要的东西会严重影响功能我一直很感激 拉请求改善现有功能-但我想保持这个 库尽可能简单、轻量级和具体
我能用这个做我的项目吗?
当然,如果你喜欢,可以随意领养!只要记住这个包裹
是非常具体的,不打算成为一个功能齐全和完全可定制的
格式化库。如果这就是你要找的,你可能想试试
其他包–例如,^{
为什么wasabi
?
我在寻找一个简短的描述性的名字,但一切都已经采取了。 所以我以我的一只老鼠芥末命名了这个包裹。
安装
pip install wasabi
API
类Printer
方法Printer.__init__
fromwasabiimportPrintermsg=Printer()
Argument | Type | Description | Default |
---|---|---|---|
^{ | bool | Pretty-print output with colors and icons. | ^{ |
^{ | bool | Don't actually print, just return. | ^{ |
^{ | dict | Add or overwrite color values, names mapped to ^{ | ^{ |
^{ | dict | Add or overwrite icon. Name mapped to unicode. | ^{ |
^{ | int | Maximum line length (for divider). | ^{ |
^{ | unicode | Steps of loading animation for ^{ | ^{ |
^{ | unicode | Alternative animation for ASCII terminals. | ^{ |
^{ | bool | Don't display animation, e.g. for logs. | ^{ |
^{ | bool | Don't output messages of type ^{ | ^{ |
^{ | unicode | Prefix for environment variables, e.g. ^{ | ^{ |
RETURNS | ^{ | The initialized printer. | - |
方法Printer.text
msg=Printer()msg.text("Hello world!")
Argument | Type | Description | Default |
---|---|---|---|
^{ | unicode | The main text to print. | ^{ |
^{ | unicode | Optional additional text to print. | ^{ |
^{ | unicode / int | Color name or value. | ^{ |
^{ | unicode | Name of icon to add. | ^{ |
^{ | bool | Whether to print or not. Can be used to only output messages under certain condition, e.g. if ^{ | ^{ |
^{ | bool | Don't actually print, just return. Overwrites global setting. | ^{ |
^{ | int | If set, perform a system exit with the given code after printing. | ^{ |
方法Printer.good
,Printer.fail
,Printer.warn
,Printer.info
打印特殊格式的邮件。
msg=Printer()msg.good("Success")msg.fail("Error")msg.warn("Warning")msg.info("Info")
Argument | Type | Description | Default |
---|---|---|---|
^{ | unicode | The main text to print. | ^{ |
^{ | unicode | Optional additional text to print. | ^{ |
^{ | bool | Whether to print or not. Can be used to only output messages under certain condition, e.g. if ^{ | ^{ |
^{ | int | If set, perform a system exit with the given code after printing. | ^{ |
方法Printer.divider
打印格式化分隔符。
msg=Printer()msg.divider("Heading")
Argument | Type | Description | Default |
---|---|---|---|
^{ | unicode | Headline text. If empty, only the line is printed. | ^{ |
^{ | unicode | Single line character to repeat. | ^{ |
^{ | bool | Whether to print or not. Can be used to only output messages under certain condition, e.g. if ^{ | ^{ |
上下文管理器Printer.loading
msg=Printer()withmsg.loading("Loading..."):# Do something here that takes longertime.sleep(10)msg.good("Successfully loaded something!")
Argument | Type | Description | Default |
---|---|---|---|
^{ | unicode | The text to display while loading. | ^{ |
方法Printer.table
,Printer.row
见Tables。
属性Printer.counts
获取特殊打印机的频率计数,例如。
MESSAGES.GOOD
。可用于打印“X警告”之类的概述
msg=Printer()msg.good("Success")msg.fail("Error")msg.warn("Error")print(msg.counts)# Counter({'good': 1, 'fail': 2, 'warn': 0, 'info': 0})
Argument | Type | Description |
---|---|---|
RETURNS | ^{ | The counts for the individual special message types. |
表格
函数table
格式化表格数据的轻量级帮助程序。
fromwasabiimporttabledata=[("a1","a2","a3"),("b1","b2","b3")]header=("Column 1","Column 2","Column 3")widths=(8,9,10)aligns=("r","c","l")formatted=table(data,header=header,divider=True,widths=widths,aligns=aligns)
Column 1 Column 2 Column 3
-------- --------- ----------
a1 a2 a3
b1 b2 b3
Argument | Type | Description | Default |
---|---|---|---|
^{ | iterable / dict | The data to render. Either a list of lists (one per row) or a dict for two-column tables. | |
^{ | iterable | Optional header columns. | ^{ |
^{ | iterable | Optional footer columns. | ^{ |
^{ | bool | Show a divider line between header/footer and body. | ^{ |
^{ | iterable / ^{ | Column widths in order. If ^{ | ^{ |
^{ | int | Maximum column width. | ^{ |
^{ | int | Number of spaces between columns. | ^{ |
^{ | iterable / unicode | Columns alignments in order. ^{ | ^{ |
RETURNS | unicode | The formatted table. |
函数row
fromwasabiimportrowdata=("a1","a2","a3")formatted=row(data)
a1 a2 a3
Argument | Type | Description | Default |
---|---|---|---|
^{ | iterable | The individual columns to format. | |
^{ | iterable / int / ^{ | Column widths, either one integer for all columns or an iterable of values. If "auto", widths will be calculated automatically based on the largest value. | ^{ |
^{ | int | Number of spaces between columns. | ^{ |
^{ | iterable | Columns alignments in order. ^{ | ^{ |
RETURNS | unicode | The formatted row. |
类TracebackPrinter
用于输出自定义格式的回溯和错误消息的帮助程序。目前 用于Thinc
方法TracebackPrinter.__init__
初始化回溯打印机。
fromwasabiimportTracebackPrintertb=TracebackPrinter(tb_base="thinc",tb_exclude=("check.py",))
Argument | Type | Description | Default |
---|---|---|---|
^{ | unicode / int | Color name or code for errors (passed to ^{ | ^{ |
^{ | unicode / int | Color name or code for traceback headline (passed to ^{ | ^{ |
^{ | unicode / int | Color name or code for highlighted text (passed to ^{ | ^{ |
^{ | int | Number of spaces to use for indentation. | ^{ |
^{ | unicode | Name of directory to use to show relative paths. For example, ^{ | ^{ |
^{ | tuple | List of filenames to exclude from traceback. | ^{ |
RETURNS | ^{ | The traceback printer. |
方法TracebackPrinter.__call__
输出自定义格式的回溯和错误。
fromwasabiimportTracebackPrinterimporttracebacktb=TracebackPrinter(tb_base="thinc",tb_exclude=("check.py",))error=tb("Some error","Error description",highlight="kwargs",tb=traceback.extract_stack())raiseValueError(error)
Some error
Some error description
Traceback:
├─ <lambda> [61] in .env/lib/python3.6/site-packages/pluggy/manager.py
├─── _multicall [187] in .env/lib/python3.6/site-packages/pluggy/callers.py
└───── pytest_fixture_setup [969] in .env/lib/python3.6/site-packages/_pytest/fixtures.py
>>> result = call_fixture_func(fixturefunc, request, kwargs)
Argument | Type | Description | Default |
---|---|---|---|
^{ | unicode | The message title. | |
^{ | unicode | Optional texts to print (one per line). | |
^{ | unicode | Optional sequence to highlight in the traceback, e.g. the bad value that caused the error. | ^{ |
^{ | iterable | The traceback, e.g. generated by ^{ | ^{ |
RETURNS | unicode | The formatted traceback. Can be printed or raised by custom exception. |
公用事业
函数color
fromwasabiimportcolorformatted=color("This is a text",fg="white",bg="green",bold=True)
Argument | Type | Description | Default |
---|---|---|---|
^{ | unicode | The text to be formatted. | - |
^{ | unicode / int | Foreground color. String name or ^{ | ^{ |
^{ | unicode / int | Background color. String name or ^{ | ^{ |
^{ | bool | Format the text in bold. | ^{ |
RETURNS | unicode | The formatted string. |
函数wrap
fromwasabiimportwrapwrapped=wrap("Hello world, this is a text.",indent=2)
Argument | Type | Description | Default |
---|---|---|---|
^{ | unicode | The text to wrap. | - |
^{ | int | Maximum line width, including indentation. | ^{ |
^{ | int | Number of spaces used for indentation. | ^{ |
RETURNS | unicode | The wrapped text with line breaks. |
环境变量
芥末也尊重以下环境变量。前缀可以是
通过env_prefix
参数在Printer
上自定义。例如,设置
env_prefix="SPACY"
需要环境变量SPACY_LOG_FRIENDLY
。
Name | Description |
---|---|
^{ | Disable colors. |
^{ | Make output nicer for logs (no colors, no animations). |
^{ | Disable pretty printing, e.g. colors and icons. |
运行测试
fork或克隆repo,确保安装了pytest
,然后运行它
在包目录中测试位于^{
pip install pytest
cd wasabi
python -m pytest wasabi