如何使用Python Invoke将可选标志传递给预任务?
我正在使用 Invoke 并有两个任务:一个清洁原始数据文件以产生干净的数据文件,并且另一个从干净的数据文件中产生多个图:
RAW_FILE = "raw.csv"
CLEAN_FILE = "clean.csv"
PLOT_FILE = "plot.svg"
@task(optional=["logging"])
def clean_data(c, logging=None):
"""Produce cleaned-up dataset."""
print("CLEAN", logging)
_configure_logging(logging)
df = clean_raw_data(RAW_FILE)
df.to_csv(CLEAN_FILE, index=False)
@task(pre=[clean_data], optional=["logging"])
def plot_data(c, logging=None):
"""Create plots of data."""
print("PLOT", logging)
_configure_logging(logging)
make_plot(CLEAN_FILE, PLOT_FILE)
def _configure_logging(log_level):
"""Initialize logging."""
if log_level is not None:
print("SETTING LOGGING TO", log_level)
CONFIG["LOGGING_LEVEL"] = log_level.upper()
如果我运行:
$ invoke clean-data --logging info
然后将记录设置为info
,我从内部获取消息clean_raw_raw_data
。但是,如果我运行:
$ invoke plot-data --logging info
然后:
clean_data
使用logging = none
调用,则不会出现日志消息。plot_data
然后使用loggging =“ info”
调用,因此出现其日志消息。
我的期望是,命令行标志将被传递给依赖任务。我尝试手动执行此操作:
@task(pre=[call(clean_data, logging=logging)], optional=["logging"])
def plot_data(c, logging=None):
...as before...
但是这会产生错误消息,因为记录
在调用@task
装饰器时未定义。
有没有办法在所需的时尚中链接可选的论点?
I am using Invoke and have two tasks: one cleans a raw data file to produce a clean data file, and the other produces several plots from the clean data file:
RAW_FILE = "raw.csv"
CLEAN_FILE = "clean.csv"
PLOT_FILE = "plot.svg"
@task(optional=["logging"])
def clean_data(c, logging=None):
"""Produce cleaned-up dataset."""
print("CLEAN", logging)
_configure_logging(logging)
df = clean_raw_data(RAW_FILE)
df.to_csv(CLEAN_FILE, index=False)
@task(pre=[clean_data], optional=["logging"])
def plot_data(c, logging=None):
"""Create plots of data."""
print("PLOT", logging)
_configure_logging(logging)
make_plot(CLEAN_FILE, PLOT_FILE)
def _configure_logging(log_level):
"""Initialize logging."""
if log_level is not None:
print("SETTING LOGGING TO", log_level)
CONFIG["LOGGING_LEVEL"] = log_level.upper()
If I run:
$ invoke clean-data --logging info
then logging is set to INFO
and I get a message from inside clean_raw_data
. However, if I run:
$ invoke plot-data --logging info
then:
clean_data
is invoked withlogging=None
, so no log message appears.plot_data
is then invoked withlogging="info"
, so its log message appears.
My expectation was that command-line flags would be passed down to dependent tasks. I tried doing this manually:
@task(pre=[call(clean_data, logging=logging)], optional=["logging"])
def plot_data(c, logging=None):
...as before...
but this produces an error message because logging
isn't defined at the point the @task
decorator is invoked.
Is there a way to chain optional arguments in the desired fashioned?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论