使用python的文本接口的人性化输出
humanfriendl的Python项目详细描述
可以使用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包的行。
支持的选项:
Option | Description |
---|---|
^{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的幂):
Unit | Binary value | Decimal value |
KB | 1024 | 1000 |
MB | 1048576 | 1000000 |
GB | 1073741824 | 1000000000 |
TB | 1099511627776 | 1000000000000 |
etc |
使用二进制字节倍数的选项仍然是通过传递关键字
参数binary=True
指向format_size()和parse_size()函数。
Contact
最新版本的humanfriendly
可在PyPI和GitHub上找到。这个
文档位于Read the Docs上,并包含一个changelog。对于Bug
报告请在GitHub上创建问题。如果你有问题,建议,
等等,请随时给我发一封电子邮件peter@peterodding.com。