Skip to main content
 首页 » 编程设计

python之一个组合对象如何引用它在内部组合的对象

2024年11月24日8lhb25

我的应用程序中有以下类。

class Command:  
    def __init__(self, command_id, command_name, exec_string): 
        self.command_name = command_name 
        self.exec_string = exec_string 
 
class Checkpoint: 
    def __init__(self, checkpoint_id, checkpoint_name, commands): 
        self.checkpoint_id = checkpoint_id 
        self.checkpoint_name = checkpoint_name 
        self.commands = commands 
 
class Job: 
    def __init__(self, job_id, job_name, checkpoints): 
        self.job_id = job_id 
        self.job_name = job_name 
        self.checkpoints = checkpoints 

我还有如下的执行器类

class JobExecutor: 
    def __init__(self, checkpoint_executor): 
        self.checkpoint_executor = checkpoint_executor 
 
    def execute_job(self, job): 
        # set job state to running 
 
        # run job 
        for checkpoint in job.checkpoints: 
            self.checkpoint_executor.execute_checkpoint(checkpoint) 
 
class CheckpointExecutor: 
    def __init__(self, command_executor): 
        self.command_executor = command_executor 
 
    def execute_checkpoint(self, checkpoint): 
        # set checkpoint state to running 
 
        # run checkpoint 
        for command in checkpoint.commands: 
            self.command_executor.execute_command(command) 
 
class CommandExecutor: 
    def run_process(self, exe):  
        import subprocess 
        try: 
            p = subprocess.Popen(exe, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 
            stdout,stderr = p.communicate() 
            return stdout 
        except FileNotFoundError as error: 
            pass 
 
    def execute_command(self, command): 
        # set command state to running 
 
        # run command 
        output = run_process(command.exec_string.split())) 
 
        if output: 
            # set command state to completed 
        else: 
            # set command state to failed 

示例用法

command1 = Command(command_id = 1 , command_name = "echo", exec_string = "echo start") 
command2 = Command(command_id = 2 , command_name = "list", exec_string = "ls -i") 
command3 = Command(command_id = 3 , command_name = "wordcount", exec_string = "wc -l Decorator.ipynb") 
 
checkpoint1 = Checkpoint(checkpoint_id = 1, checkpoint_name = "first_checkpoint", commands = [command1, command2]) 
checkpoint2 = Checkpoint(checkpoint_id = 2, checkpoint_name = "second_checkpoint", commands = [command3]) 
 
job = Job(job_id = 1, job_name = "unix tutorial", checkpoints = [checkpoint1, checkpoint2]) 
 
job_executor = JobExecutor(CheckpointExecutor(CommandExecutor())) 
job_executor.execute_job(job) 

在“execute_checkpoint”方法中,我想引用包含此检查点的作业对象,在“execute_command”方法中,我想引用包含此命令的检查点对象。我怎样才能在不破坏执行方法签名的情况下做到这一点?

请您参考如下方法:

如何“引用包含此检查点的作业对象”

您可以添加一个字段来引用父级,并在创建作业时分配给该字段。

class Checkpoint: 
    def __init__(self, checkpoint_id, checkpoint_name, commands): 
        self.checkpoint_id = checkpoint_id 
        self.checkpoint_name = checkpoint_name 
        self.commands = commands 
        self.parent = None                           # <== Placeholder           
 
class Job: 
    def __init__(self, job_id, job_name, checkpoints): 
        self.job_id = job_id 
        self.job_name = job_name 
        self.checkpoints = checkpoints 
        for checkpoint in checkpoints:               # <== All checkpoints    
            checkpoint.parent = self                 # <== Store ref to parent