如何使用Python Invoke将可选标志传递给预任务?

发布于 2025-01-18 21:25:00 字数 1571 浏览 2 评论 0原文

我正在使用 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

然后:

  1. clean_data使用logging = none调用,则不会出现日志消息。
  2. 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:

  1. clean_data is invoked with logging=None, so no log message appears.
  2. plot_data is then invoked with logging="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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文