我在 Python 2.7 中有一个 ruffus 管道,但是当我用 -n
或 --just_print
调用它时,它仍然运行所有实际任务,而不仅仅是像预期的那样打印管道。我:
* 没有 -n
参数会取代内置参数(尽管我有其他命令行参数)
* 有一堆带有 @transform()
或 @merge()
装饰器的函数
* 使用 run_pipeline()
调用结束管道
有没有人遇到过这个问题?非常感谢!
从 ruffus 2.4 版开始,您可以使用内置的 ruffus.cmdline
它通过使用 argparse
的 cmdline.py
模块存储适当的标志,例如:
from ruffus import *
parser = cmdline.get_argparse(description='Example pipeline')
options = parser.parse_args()
@originate("test_out.txt")
def run_testFunction(output):
with open(output,"w") as f:
f.write("it's working!\n")
cmdline.run(options)
然后使用如下命令从终端运行您的管道:
python script.py --verbose 6 --target_tasks run_testFunction --just_print
如果您想手动执行此操作(这对于旧版本的 ruffus 是必需的),您可以调用 pipeline_printout()
而不是 pipeline_run()
,使用 argparse
以便 --just_print
标志导致适当的调用,例如:
from ruffus import *
import argparse
import sys
parser = argparse.ArgumentParser(description='Example pipeline')
parser.add_argument('--just_print', dest='feature', action='store_true')
parser.set_defaults(feature=False)
args = parser.parse_args()
@originate("test_out.txt")
def run_testFunction(output):
with open(output,"w") as f:
f.write("it's working!\n")
if args.feature:
pipeline_printout(sys.stdout, run_testFunction, verbose = 6)
else:
pipeline_run(run_testFunction, verbose = 6)
然后您将运行如下命令:
python script.py --just_print
我是一名优秀的程序员,十分优秀!