将函数作为脚本运行
tasker的Python项目详细描述
tasker是一种组织脚本的方法。如果有几十个小脚本,却一直忘了它们的作用和参数,tasker适合您。不要编写函数脚本,而是将它们放在一个或几个文件中,然后运行主tasker脚本,为它们提供任务名称和任务参数,如下所示:
run_task <task-name> [<task-parameter>]...
任务help将扫描任务包,并为您提供任务的签名和文档字符串。
要视为任务,函数名应以_task结尾。任务名是函数名减去后缀。假设我们有以下文件结构:
run_task tasks/ __init__.py moretasks.py
__init__.py
defone_task(p1):print'in one_task, parameters:',p1deftwo_task(p1,p2):print'in two_task, parameters:',p1,p2
以及moretasks.py
deft1_task(p1):print'in t1_task, parameters:',p1deft2_task(p1,p2=None):print'in t2_task, parameters:',p1,p2
然后我们可以运行以下示例:
run_task one foo run_task two foo baz run_task two foo # fails, not enough parameters run_task moretasks.t1 spam run_task moretasks.t2 spam ham run_task moretasks.t2 spam # works too because of default value of the second parameter
要在当前目录中生成run_task脚本,请运行:
python -m tasker <fully-qualified-name-of-tasks-package> # default = "tasks"
重要提示:所有任务参数都是字符串。
您可以在项目的分发版中包含任务,并使用单个安装的脚本运行所有任务。假设您的项目如下:
someproject/ __init__.py somestuff.py tasks/
然后您可以在__init__.py中包含以下代码片段:
defrun_task():importtaskertasker.main('tasks')
然后在基于setup.py的设置工具中包括以下内容:
entry_points={ 'console_scripts': [ 'someproject_task = someproject:run_task', ], }
此安装程序将创建脚本someproject_task,该脚本将了解您的任务。
安装
pip install tasker
原点
这是受ruby的rake实用程序启发的。我用了一段时间shovel python克隆了rake,直到我对它不满意为止。^{tt11}的重要区别$ (除了简单之外)它不依赖于当前的工作目录。