Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的惰性参数绑定_Python_Oop_Design Patterns_Functional Programming_Workflow - Fatal编程技术网

Python中的惰性参数绑定

Python中的惰性参数绑定,python,oop,design-patterns,functional-programming,workflow,Python,Oop,Design Patterns,Functional Programming,Workflow,我尝试使用复合模式设计工作流,示例代码如下所示: 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.p

我尝试使用
复合模式设计工作流,示例代码如下所示:

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()
workflow = Workflow()
raw_files = ["file1", "file2", "file3"]
for i in raw_files:
    head_command = ShellCommand("head {input} {output}", 
                                input = i, 
                                output = i+"_head")
    workflow.add(head_command)
merge_command = ShellCommand("cat {input} > {output}", 
                             input=" ".join([i+"_head" for i in rawfiles]), 
                             output="merged")
workflow.add(merge_command)

workflow.invoke()
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()
可以这样使用:

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()
workflow = Workflow()
raw_files = ["file1", "file2", "file3"]
for i in raw_files:
    head_command = ShellCommand("head {input} {output}", 
                                input = i, 
                                output = i+"_head")
    workflow.add(head_command)
merge_command = ShellCommand("cat {input} > {output}", 
                             input=" ".join([i+"_head" for i in rawfiles]), 
                             output="merged")
workflow.add(merge_command)

workflow.invoke()
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()
但是,有时一个命令的输入可能是另一个命令的结果,例如:

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()
我不能使用工作流来合成这两个命令,因为
b
使用
a
的结果作为参数,只有在调用a之后才可以看到该结果。然而,在某些情况下,像以前的代码一样的工作流管理器仍然是必要的。我想我需要这样的东西:

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()
workflow = Workflow()
raw_files = ["file1", "file2", "file3"]
for i in raw_files:
    head_command = ShellCommand("head {input} {output}", 
                                input = i, 
                                output = i+"_head")
    workflow.add(head_command)
merge_command = ShellCommand("cat {input} > {output}", 
                             input=" ".join([i+"_head" for i in rawfiles]), 
                             output="merged")
workflow.add(merge_command)

workflow.invoke()
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()
这样的实现来设计工作流吗?

为什么不想将结果存储在工作流实例中?