使用python的文本接口的人性化输出

humanfriendl的Python项目详细描述


https://travis-ci.org/xolox/python-humanfriendly.svg?branch=masterhttps://coveralls.io/repos/xolox/python-humanfriendly/badge.png?branch=master

可以使用humanfriendly包中的函数和类 文本界面更加用户友好。一些示例功能:

  • 解析和格式化中的数字、文件大小、路径名和时间跨度 简单,人性化的格式。
  • 易于使用计时器进行长时间运行操作,人性化 结果时间跨度的格式。
  • 通过键入 选项的编号或选项的唯一子字符串。
  • 终端交互,包括文本样式(ANSI转义序列),用户 友好的使用消息呈现和查询终端 大小。

humanfriendly包目前在python 2.6、2.7、3.4、3.5上进行测试, Linux和MacOSX上的3.6、3.7和Pypy(2.7)。 也可以支撑窗户,你可能会遇到一些粗糙的边缘。

Getting started

开始使用humanfriendly包非常简单:

>>> import humanfriendly
>>> user_input = raw_input("Enter a readable file size: ")
Enter a readable file size: 16G
>>> num_bytes = humanfriendly.parse_size(user_input)
>>> print num_bytes
16000000000
>>> print "You entered:", humanfriendly.format_size(num_bytes)
You entered: 16 GB
>>> print "You entered:", humanfriendly.format_size(num_bytes, binary=True)
You entered: 14.9 GiB

Command line

用法:humanfriendly [OPTIONS]

命令上人性化的输入/输出(文本格式) 基于同名python包的行。

支持的选项:

OptionDescription
^{tt1}$, ^{tt2}$Execute an external command (given as the positional arguments) and render a spinner and timer while the command is running. The exit status of the command is propagated.
^{tt3}$Read tabular data from standard input (each line is a row and each whitespace separated field is a column), format the data as a table and print the resulting table to standard output. See also the ^{tt4}$ option.
^{tt5}$, ^{tt6}$Change the delimiter used by ^{tt3}$ to ^{tt8}$ (a string). By default all whitespace is treated as a delimiter.
^{tt9}$, ^{tt10}$Convert a length count (given as the integer or float ^{tt11}$) into a human readable string and print that string to standard output.
^{tt12}$, ^{tt13}$Format a number (given as the integer or floating point number ^{tt8}$) with thousands separators and two decimal places (if needed) and print the formatted number to standard output.
^{tt15}$, ^{tt16}$Convert a byte count (given as the integer ^{tt17}$) into a human readable string and print that string to standard output.
^{tt18}$, ^{tt19}$Change the output of ^{tt15}$, ^{tt21}$ to use binary multiples of bytes (base-2) instead of the default decimal multiples of bytes (base-10).
^{tt22}$, ^{tt23}$Convert a number of seconds (given as the floating point number ^{tt24}$) into a human readable timespan and print that string to standard output.
^{tt25}$Parse a human readable length (given as the string ^{tt8}$) and print the number of metres to standard output.
^{tt27}$Parse a human readable data size (given as the string ^{tt8}$) and print the number of bytes to standard output.
^{tt29}$Demonstrate changing the style and color of the terminal font using ANSI escape sequences.
^{tt30}$, ^{tt31}$Show this message and exit.

A note about size units

当我最初发布humanfriendly包时,我使用了二进制 字节的倍数(2的幂)。有人多次指出 是一个糟糕的选择(参见问题#4和拉取请求#8#9),因此 新的默认值变为字节的十进制倍数(10的幂):

UnitBinary valueDecimal value
KB10241000
MB10485761000000
GB10737418241000000000
TB10995116277761000000000000
etc

使用二进制字节倍数的选项仍然是通过传递关键字 参数binary=True指向format_size()parse_size()函数。

Contact

最新版本的humanfriendly可在PyPIGitHub上找到。这个 文档位于Read the Docs上,并包含一个changelog。对于Bug 报告请在GitHub上创建问题。如果你有问题,建议, 等等,请随时给我发一封电子邮件peter@peterodding.com

License

此软件是根据MIT license授权的。

版权所有©2018彼得·奥丁。

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

推荐PyPI第三方库


热门话题
java将日志重定向到Log4j2   在Java中,符号ANDEQUALS运算符用于什么?   java在NetBeans中与本地Maven项目一起使用“转到源代码”   java Hibernate/JPA双向级联存储更新   java动画是无法实例化的抽象   如何使用Java在SQL Server中持久化大型JSON字符串   java如何从面板中删除多个标签?   java使PDF文件上的问号可读   java多依赖微调器   java Web服务在解析时生成XML意外令牌错误   javatomcat和Ajax问题   java如何将公共类放入交换机外壳中   javascript如何使用JSON在JSP和Java控制器之间进行交互?