Python中的延迟参数绑定

2024-09-20 06:46:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用composite pattern设计工作流,示例代码如下:

class CommandInterface(object):
    def __init__(self, name, template=None, tool=None, param : dict={},input=None, output=None ):
        self.name = name
        self.input = input
        self.output = output
        self.param = param
        self.template = template
    def before_invoke(self):
        pass   # check before invoke
    def invoke(self):
        pass
    def after_invoke(self):
        pass  # check after invoke
class ShellCommand(CommandInterface):    
    def render(self):
        cmd = self.template.format(input=self.input,
                                    output=self.output,
                                    param=self.param,
                                    )
        return cmd
    def before_invoke(self):
        pass
    def invoke(self):
        os.system(self.render())

class Workflow(CommandInterface):
    def __init__(self, name):
       self.name = name
       self._input = []
       self._output = []
       self._commands = []
       self._tool = []
    def add(self, command : CommandInterface):
        self._commands.append(command)
        return self

    def invoke(self):
        for command in self._commands:
            command.invoke()

可以这样使用:

^{pr2}$

但是,有时一个命令的输入可能是另一个命令的结果,例如:

class PythonCommand(CommandInterface):    
    def invoke(self):
        self._result = self.template(input=self.input, output=self.output, param=self.param)

def a_command(input, output, param):
    take_long_time(input, output, param)
    return {"a_result": "some result will be used in b_command"}

def b_command(input, output, param):
    def need_the_result_of_a_command(input, output, param):
        return param["a_result"]
    need_the_result_of_a_command(input, output, param)
    return "success"

a = PythonCommand(a_command, input_, output_, param_)
a.invoke()
b = PythonCommand(b_command, input_, output_, param= a._result)
b.invoke()

我不能使用工作流来组合这2个命令,因为b使用a的结果作为参数,只有在调用a之后才可见。但是,在某些情况下,像前面的代码一样的工作流管理器仍然是必需的。我想我需要这样的东西:

workflow = Workflow()
a = PythonCommand(a_command, input_, output_, param_)
workflow.add(a)
b = PythonCommand(b_command, input_, output_, param= {"a_result": a.lazy()._result})
workflow.add(b)
workflow.invoke()

有人知道如何用这样的.lazy()实现来设计工作流吗?在


Tags: nameselfnoneinputoutputreturnparamdef