希望这个术语是正确的。我有这个装饰器函数,它读取一个文本文件:
def read_commands(inner, path=BATCH_PATH):
with open(path) as f:
commands = ['python ' + line.replace('\n', '') for line in f]
def wrapper(*args, **kwargs):
for command in commands:
inner(command, *args, **kwargs)
return wrapper
这是它装饰的功能之一:
@read_commands
def execute_multi_commands(command, count):
LOG.info(f'Executing command {count}: {command}')
os.system(command)
count += 1
我希望能够在调用时更改默认路径execute_multi_commands,就像我的main:
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-b', '--batch', action='store', type=str, dest='batch')
args = parser.parse_args()
count = 1
execute_multi_commands(count, path=args.batch)
然而,显然这不起作用,因为pathis not a argument in execute_multi_commands. 当我调用时,我是否可以传递path给装饰器函数?- 或者,更有可能的是,任何功能等效的替代方案?read_commandsexecute_multi_commands
白猪掌柜的
相关分类