我想使用 for 循环根据
config.yaml
文件的内容生成一组规则。这是一个最小的工作示例。
config={'person1': 'Amy', 'person2': 'Bruce'}
for thiskey,thisperson in config.items():
rule:
name: f'{thisperson}s_rule'
output: f'{thiskey}.txt'
run:
print(f'\n{thiskey} is called {thisperson}!\n')
rule people:
input: [this+'.txt' for this in config.keys()]
如果您运行它(使用
-k
开关继续运行并忽略丢失的文件),您将看到两个文件的屏幕上都写入了相同的内容 (person2 is called Bruce!
),而您希望看到给艾米和布鲁斯的类似信息。
这是设置规则的明智方式吗?或者我应该找到解决方法吗?
谢谢,
马克
config = {'person1': 'Amy', 'person2': 'Bruce'}
def create_rule(thiskey, thisperson):
rule:
name: f"{thisperson}s_rule"
output: f"{thiskey}.txt"
run:
print(f'\n{thiskey} is called {thisperson}!\n')
# Generate rules
for thiskey, thisperson in config.items():
create_rule(thiskey, thisperson)
rule people:
input: [this + '.txt' for this in config.keys()]
尝试将循环变量作为默认参数传递给内联函数,强制在每次迭代时对其求值。