gpt4 book ai didi

python - 将数据保存在 Python 的调用库(pyinvoke)的上下文变量中

转载 作者:太空宇宙 更新时间:2023-11-04 05:25:27 25 4
gpt4 key购买 nike

使用 Invoke library for Python ,我想先运行一个设置任务,在 context variable 中保存一些值然后所有以下任务都可以读取和使用。

The documentation is quite short on that topic , 然而, 它指出

A common problem task runners face is transmission or storage of values which are “global” for the current session - values loaded from configuration files (or other configuration vectors), CLI flags, values set by ‘setup’ tasks, etc.

(emphasis by me)

,所以这实际上应该是可能的。

但是,当我写这篇文章的时候,

from invoke import task

@task
def set_context(context):
context.foo = 'bar'

@task
def read_context(context):
print(context.foo)

然后通过 invoke read_context set_context 从命令行运行文件,然后我得到

Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/invoke/config.py", line 69, in __getattr__
return self._get(key)
File "/usr/local/lib/python3.4/dist-packages/invoke/config.py", line 113, in _get
value = self.config[key]
File "/usr/local/lib/python3.4/dist-packages/invoke/config.py", line 110, in __getitem__
return self._get(key)
File "/usr/local/lib/python3.4/dist-packages/invoke/config.py", line 113, in _get
value = self.config[key]
KeyError: 'foo'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/bin/invoke", line 11, in <module>
sys.exit(program.run())
File "/usr/local/lib/python3.4/dist-packages/invoke/program.py", line 270, in run
self.execute()
File "/usr/local/lib/python3.4/dist-packages/invoke/program.py", line 381, in execute
executor.execute(*self.tasks)
File "/usr/local/lib/python3.4/dist-packages/invoke/executor.py", line 113, in execute
result = call.task(*args, **call.kwargs)
File "/usr/local/lib/python3.4/dist-packages/invoke/tasks.py", line 111, in __call__
result = self.body(*args, **kwargs)
File "/home/jenkins/workspace/Deploy_Birmingham_URL_Service_on_Docker_host/deployment/tasks.py", line 14, in read_context
print(context.foo)
File "/usr/local/lib/python3.4/dist-packages/invoke/config.py", line 81, in __getattr__
raise AttributeError(err)
AttributeError: No attribute or config key found for 'foo'

Valid keys: ['run', 'tasks']

Valid real attributes: ['from_data', 'run']

invoke 中将某些内容保存在上下文变量中并在其他任务中重复使用的正确方法是什么?我正在使用 Python 3.5.2 并在重要时调用 0.13.0。


future 用户须知:0.15 修复了这个问题(github bug link)

最佳答案

使用ctx.update

from invoke import task

@task
def set_context(context):
context.update({'foo': 'bar'})

@task
def read_context(context):
print(context.foo) # => 'bar'

关于python - 将数据保存在 Python 的调用库(pyinvoke)的上下文变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38916469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com