我在控制虚拟机生命周期的类中有几个方法。如启动运营,停止,终止,退休..等Python |避免重复代码块
的代码,这些方法几乎是相同的,例如:
def stop(self, instances):
"""
Stop instance or a group of instances
:param instances: List of instances
:return:
"""
try:
response = self.ec2_client.stop_instances(InstanceIds=instances, DryRun=False)
print(response)
except ClientError as e:
print(e)
return response
def start(self, instances):
"""
Start instance or a group of instances
:param instances: List of instances
:return:
"""
try:
response = self.ec2_client.start_instances(InstanceIds=instances, DryRun=False)
print(response)
except ClientError as e:
print(e)
return response
正如你所看到的,这两种方法除了API几乎相同调用以执行所需的操作(start_instances和stop_instance)。
有没有办法一般编写这样的方法或函数,并防止重复代码?
在此先感谢。
P.S.我正在考虑装饰器,实例功能,关闭 - 但只是不知道如何!
回答以下问题,激发了我以下解决方案:
@staticmethod
def _request_action_method(action, instances):
instance_ids = FleetManager._instance_to_str(instances)
def _action_method():
try:
response = action(InstanceIds=instance_ids, DryRun=False)
print(response)
except ClientError as e:
print(e)
return _action_method
我能与那些几行替换+50行代码和它的作品:)