简单的命令执行管道,加上统一的commandlineinterface入口点
simppl的Python项目详细描述
辛普森
用于编写简单命令行管道的包,以及有组织的命令行工具箱。
该软件包由两个独立但相互交织的模块组成:
- simple_pipeline帮助编写命令行管道,开销最小。可以集成到任何python脚本中。在
- cli-帮助将cli python脚本集合转换为有组织的工具箱cli
这两个模块打包在一起,可以自然地一起使用。 每个模块都有相互使用的功能。在
简单管道
simple\u pipeline模块定义SimpleLine类。
SimpleLine可以方便地将pythoncli脚本(从终端执行)转换为操作系统命令的管道。在
- 它允许按顺序/并发地运行操作系统命令。在
- 使用多处理来轻松实施:wqment散射聚集图案
- 每个命令批处理都有一个索引。在
- 用户可以通过指定-fc(第一个_命令)和-lc(最后一个_命令)标志来运行命令的子序列。在
- 可以选择使用-d标志来运行管道。在
- 每个命令在执行前都会打印出来,而且还可以选择定时。在
- 收集并记录子命令的输出/错误。在
使用简单管道
最简单的用法如下:
from simppl.simple_pipeline import SimplePipeline
sp = SimplePipeline(start=0, end=100):
sp.print_and_run('<YOUR_FIRST_OS_COMMAND>')
sp.print_and_run('<YOUR_SECOND_OS_COMMAND>')
要同时运行多个命令,请使用:
^{pr2}$最后,如果您的项目使用cli模块,您可以直接运行另一个命令行工具作为管道的一部分。 另一个工具将从同一进程运行,但它将作为管道中的另一个命令从日志中显示。 调用这些工具和其他重构工具可以使调试更加平滑。在
from example_module import example_tool
sp.print_and_run_clt(example_tool.run, ['first_number', 'second_nmber'],
{'-key1': 'val1', '-key2': 'val2'},
{'--flag'})
请注意,为了查看打印的命令,您需要配置日志记录。参见示例_模块/日志记录_配置.ini例如。
命令行接口
cli支持将python可执行脚本集合转换为统一的cli。在
- 创建用于运行命令行工具的单个入口点
- 标准化工具开发和文档
- 添加一个手动打印输出,列出所有可用的工具和软件包,开发开销最小
使用cli:
- example_模块提供了一个如何在项目中使用CommandLineInterface的示例
- 要求:
- main.py-定义工具箱徽标,构造并运行CommandLineInterface。在
- init.py-设置日志记录配置
- 测井_配置.ini- python.logging配置
- 工具-每个定义为命令行工具的脚本将自动添加到工具箱中
将脚本定义为命令行工具:
from simppl.cli import command_line_tool
@command_line_tool
def run(argv):
"""
Tool description that will appear in main man printout
"""
# Do something here using any python code
打印手册(不带参数运行包):
python -m your_toolbox_package_name
其中,your_toolbox_package_name是包含main.py的文件夹的名称
运行特定工具:
python -m your_toolbox_package_name tool_name <tool_args>
其中tool_name是包含@command_line_tool定义的py文件的名称
示例
命令行工具示例:
- 参见示例_module/add_two_数字.py在
python -m example_module add_two_numbers 5 6
- 应该打印11.0到标准输出
使用SimpleLine
运行命令行工具的示例python -m example_module analyze_file_pipeline resources/analyze_file_pipeline_input.txt test_outputs
- 应将以下内容(日期时间除外)打印到标准输出:
python -m <module_name> analyze_file_pipeline resources/analyze_file_pipeline_input.txt test_outputs
2020-09-11 14:31:05,639 - analyze_file_pipeline - INFO - 1) wc resources/analyze_file_pipeline_input.txt > test_outputs/wc.txt
2020-09-11 14:31:05,643 - analyze_file_pipeline - INFO - Time elapsed wc: 0 s
2020-09-11 14:31:05,643 - analyze_file_pipeline - INFO - 2) ls -l resources/analyze_file_pipeline_input.txt > test_outputs/ls.txt
2020-09-11 14:31:05,648 - analyze_file_pipeline - INFO - Time elapsed ls: 0 s
2020-09-11 14:31:05,649 - analyze_file_pipeline - INFO - 3) sed 's/\s/\n/g' resources/analyze_file_pipeline_input.txt | sort | uniq -c | sort -n > test_outputs/word_count.txt
2020-09-11 14:31:05,653 - analyze_file_pipeline - INFO - Time elapsed sed: 0 s
分配
按照本手册进行分发: https://packaging.python.org/tutorials/packaging-projects/
- 项目
标签: