arbol -- Python的树状打印
项目描述
arbol | Python的树状打印包
你有一个脚本,一个命令行工具,或者一些Python工作流程,其中包含大量的 'print' 语句,但当你看到控制台上的数百行时,你无法理解它?听起来很熟悉吗?
arbol 会按照你的代码结构组织你的 stdout 打印。使用简单的上下文管理器来定义层次结构和 'aprint' 命令,而不是使用 print,然后就可以实现了。最后,当可选的依赖项安装完成后,打印的树和文本会用精心制作的颜色组合进行着色,使其更加美观。
如果你想知道,'arbol' 在西班牙语中意味着 '树'。
为什么不使用更传统的Python日志记录?我们选择了坚持一个简单直观的方案,以匹配 'print' 语句的使用。
特性
你可以使用 'aprint' 替换内置的 'print'。你可以使用 'asection' 上下文管理器在树中创建一个新的 '节点'。此外,arbol 会测量树中每个节点的耗时,并方便地显示。Arbol类有几个配置标志可以调整。如果你想捕获来自第三方代码的打印语句,你可以使用 'acapture' 上下文管理器来捕获 stdout(和 stderr)-- 这有一点实验性,最好少量使用。最好的文档就是下面的演示...
安装
使用pip安装
pip install arbol
可选依赖
如果你想使用颜色,请安装 ansicolors 包
pip install ansicolors
为了确保你可以在所有操作系统上获得颜色(尤其是Windows),请安装 colorama 包
pip install colorama
注意:colorama和ansicolors都是可选的 -- arbol 即使不安装它们也可以正常工作。
示例
这是一个简单且易于理解的示例
from arbol import Arbol, aprint, section, asection, acapture
import arbol
# for colors, install the ansicolors package: 'pip install ansicolors',
# and for windows install the colorama package: 'pip install colorama'
# You can limit the tree depth:
Arbol.max_depth = 4
# use aprint (=arbol print) instead of the standard print
aprint('Test')
# You can decorate functions:
@section('function')
def fun(x):
if x >= 0:
with asection('recursive call to f'):
aprint(f"f(x)+1={fun(x - 1)}")
# The context manager let's you go down one level in the tree
with asection('a section'):
aprint('a line')
aprint('another line')
aprint('we are done \n or are we? \n someone gotta check!')
with asection('a subsection'):
aprint('another line')
aprint('we are done')
# works through function calls and the like...
fun(2)
# You can capture stdout if you want, usefull when a 3rd party library has printouts that you want to capture...
with acapture():
print("No escape is possible")
aprint("Even this works...\n")
# Don't push it.. sections will not work right now...
# You can deactivate the elapsed time measurement and printing:
Arbol.elapsed_time = False
fun(100)
aprint('demo is finished...')
# You can also turn off all output with one switch:
Arbol.enable_output = False
aprint('you will not see that')
这就是它的外观
路线图
一些从严肃到高度推测性的可能考虑的想法
- 更多可供选择的颜色风格
- 从C代码中拦截标准输出,以便Python中调用的库的打印输出也格式化,不清楚这是否可行。
- 通过检查堆栈自动生成树?
- 与日志包的互操作性?
- 如何处理多个线程/进程?目前,打印输出随机交织,混乱且难以理解。一个想法是捕获每个线程的所有输出,保留这些输出直到线程完成(可能通过专门的“保留”上下文管理器),然后按顺序和单独输出。可能可行。
贡献
欢迎拉取请求!
作者
Loic A. Royer (@loicaroyer) Ahmet Can Solak (@_ahmetcansolak)
项目详情
arbol-2021.5.27.641.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 5b944a29bbbe488f71251a2cd247bb3eacfdc1e927350e7f9cd9de797b5d1f70 |
|
MD5 | dc7f504a3f76eb7aefdef032d686ad53 |
|
BLAKE2b-256 | fb487e9e1321969eb9452ec3d3f78e9ad75f6c45f3c9d76d9d5951ae78b0667d |
arbol-2021.5.27.641-py2.py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 442769234c4ad2d74ac1409c3d999a791e6398ec2f7eb4917413a745981ea1e7 |
|
MD5 | 02df9f8fc9613970fd618d5bbc757251 |
|
BLAKE2b-256 | 4511659b2e97d3c2f11ae9f6afc79f24d5cf785646413bff328694f1394c799a |