我的应用程序中有以下课程。
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”方法内,我希望引用其中包含此命令的检查点对象。如何在不破坏执行方法签名的情况下做到这一点?
答案 0 :(得分:1)
创建作业后,您可以添加一个字段以引用父级,并分配给该字段。
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