我在控制虛擬機生命週期的類中有幾個方法。如啓動運營,停止,終止,退休..等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行代碼和它的作品:)