一个轻量级控制台打印和格式化工具包

wasabi的Python项目详细描述


wasabi:一个轻量级控制台打印和格式化工具包

多年来,我已经编写了无数的着色和 格式化实用程序以在库中输出消息,如 spaCyThincProdigy尽管还有许多其他伟大的开源 选择,我总是想要一些稍微不同或稍有不同的东西 习惯。

此包仍在进行中,旨在将这些实用程序捆绑到 一种标准化的方式,以便它们可以在我们的其他项目中共享。太棒了 轻量级,没有依赖关系,可以跨Python2和3工作

Azure PipelinesPyPicondaGitHubCode style: black

常见问题

你打算增加更多的功能吗?

是的,还有一些助手和功能需要移植。然而,新的 我们(认为我们)需要的东西会严重影响功能我一直很感激 拉请求改善现有功能-但我想保持这个 库尽可能简单、轻量级和具体

我能用这个做我的项目吗?

当然,如果你喜欢,可以随意领养!只要记住这个包裹 是非常具体的,不打算成为一个功能齐全和完全可定制的 格式化库。如果这就是你要找的,你可能想试试 其他包–例如,^{}^{}^{}^{}^{}^{},举几个例子。

为什么wasabi

我在寻找一个简短的描述性的名字,但一切都已经采取了。 所以我以我的一只老鼠芥末命名了这个包裹。

安装

pip install wasabi

API

Printer

方法Printer.__init__

fromwasabiimportPrintermsg=Printer()
ArgumentTypeDescriptionDefault
^{}boolPretty-print output with colors and icons.^{}
^{}boolDon't actually print, just return.^{}
^{}dictAdd or overwrite color values, names mapped to ^{}-^{}.^{}
^{}dictAdd or overwrite icon. Name mapped to unicode.^{}
^{}intMaximum line length (for divider).^{}
^{}unicodeSteps of loading animation for ^{}.^{}
^{}unicodeAlternative animation for ASCII terminals.^{}
^{}boolDon't display animation, e.g. for logs.^{}
^{}boolDon't output messages of type ^{}.^{}
^{}unicodePrefix for environment variables, e.g. ^{}.^{}
RETURNS^{}The initialized printer.-

方法Printer.text

msg=Printer()msg.text("Hello world!")
ArgumentTypeDescriptionDefault
^{}unicodeThe main text to print.^{}
^{}unicodeOptional additional text to print.^{}
^{} unicode / intColor name or value.^{}
^{}unicodeName of icon to add.^{}
^{}boolWhether to print or not. Can be used to only output messages under certain condition, e.g. if ^{} flag is set.^{}
^{}boolDon't actually print, just return. Overwrites global setting.^{}
^{}intIf set, perform a system exit with the given code after printing.^{}

方法Printer.goodPrinter.failPrinter.warnPrinter.info

打印特殊格式的邮件。

msg=Printer()msg.good("Success")msg.fail("Error")msg.warn("Warning")msg.info("Info")
ArgumentTypeDescriptionDefault
^{}unicodeThe main text to print.^{}
^{}unicodeOptional additional text to print.^{}
^{}boolWhether to print or not. Can be used to only output messages under certain condition, e.g. if ^{} flag is set.^{}
^{}intIf set, perform a system exit with the given code after printing.^{}

方法Printer.divider

打印格式化分隔符。

msg=Printer()msg.divider("Heading")
ArgumentTypeDescriptionDefault
^{}unicodeHeadline text. If empty, only the line is printed.^{}
^{}unicodeSingle line character to repeat.^{}
^{}boolWhether to print or not. Can be used to only output messages under certain condition, e.g. if ^{} flag is set.^{}

上下文管理器Printer.loading

msg=Printer()withmsg.loading("Loading..."):# Do something here that takes longertime.sleep(10)msg.good("Successfully loaded something!")
ArgumentTypeDescriptionDefault
^{}unicodeThe text to display while loading.^{}

方法Printer.tablePrinter.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})
ArgumentTypeDescription
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
ArgumentTypeDescriptionDefault
^{}iterable / dictThe data to render. Either a list of lists (one per row) or a dict for two-column tables.
^{}iterableOptional header columns.^{}
^{}iterableOptional footer columns.^{}
^{}boolShow a divider line between header/footer and body.^{}
^{}iterable / ^{}Column widths in order. If ^{}, widths will be calculated automatically based on the largest value.^{}
^{}intMaximum column width.^{}
^{}intNumber of spaces between columns.^{}
^{}iterable / unicodeColumns alignments in order. ^{} (left, default), ^{} (right) or ^{} (center). If If a string, value is used for all columns.^{}
RETURNSunicodeThe formatted table.

函数row

fromwasabiimportrowdata=("a1","a2","a3")formatted=row(data)
a1   a2   a3
ArgumentTypeDescriptionDefault
^{}iterableThe 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.^{}
^{}intNumber of spaces between columns.^{}
^{}iterableColumns alignments in order. ^{} (left), ^{} (right) or ^{} (center).^{}
RETURNSunicodeThe formatted row.

TracebackPrinter

用于输出自定义格式的回溯和错误消息的帮助程序。目前 用于Thinc

方法TracebackPrinter.__init__

初始化回溯打印机。

fromwasabiimportTracebackPrintertb=TracebackPrinter(tb_base="thinc",tb_exclude=("check.py",))
ArgumentTypeDescriptionDefault
^{}unicode / intColor name or code for errors (passed to ^{} helper).^{}
^{}unicode / intColor name or code for traceback headline (passed to ^{} helper).^{}
^{}unicode / intColor name or code for highlighted text (passed to ^{} helper).^{}
^{}intNumber of spaces to use for indentation.^{}
^{}unicodeName of directory to use to show relative paths. For example, ^{} will look for the last occurence of ^{} in a path and only show path to the right of it.^{}
^{}tupleList 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)
ArgumentTypeDescriptionDefault
^{}unicodeThe message title.
^{}unicodeOptional texts to print (one per line).
^{}unicodeOptional sequence to highlight in the traceback, e.g. the bad value that caused the error.^{}
^{}iterableThe traceback, e.g. generated by ^{}.^{}
RETURNSunicodeThe formatted traceback. Can be printed or raised by custom exception.

公用事业

函数color

fromwasabiimportcolorformatted=color("This is a text",fg="white",bg="green",bold=True)
ArgumentTypeDescriptionDefault
^{}unicodeThe text to be formatted.-
^{}unicode / intForeground color. String name or ^{} - ^{}.^{}
^{}unicode / intBackground color. String name or ^{} - ^{}.^{}
^{}boolFormat the text in bold.^{}
RETURNSunicodeThe formatted string.

函数wrap

fromwasabiimportwrapwrapped=wrap("Hello world, this is a text.",indent=2)
ArgumentTypeDescriptionDefault
^{}unicodeThe text to wrap.-
^{}intMaximum line width, including indentation.^{}
^{}intNumber of spaces used for indentation.^{}
RETURNSunicodeThe wrapped text with line breaks.

环境变量

芥末也尊重以下环境变量。前缀可以是 通过env_prefix参数在Printer上自定义。例如,设置 env_prefix="SPACY"需要环境变量SPACY_LOG_FRIENDLY

NameDescription
^{}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

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

推荐PyPI第三方库


热门话题
在java中将字符串转换为音频输入   java如何使用Spring Boot RestTemplate解组json列表   计算机上C++ OpenCV项目转换为OpenCV Android的java步骤   Java Spring Telegraf数据表不工作   openurlconnection与connect之间的java差异?   java Solr字段搜索、通配符和转义字符   java如何使用DLFolderLocalServiceUtil类的getFolders()方法?   尝试用Java编写文件   java连续双缓冲解决方案不起作用   两个数组的Java乘积   java Jackson,如何正确编写自定义反序列化程序   java将布尔标志按一定顺序排列以获得更好的性能是否有意义   java Vaadin无缓冲网格无法关闭   java在MySQL中以同一用户身份同时从不同客户端登录安全吗?   java如何使用安卓asynchttp库   java无法在Android中使用AlarmManager停止服务   java在Hibernate的XML配置文件中指定默认值   Spring启动应用程序中的java内存SQLITE不工作   Java代码无法写入文本文件,