Sep-18-2020, 02:39 PM
(This post was last modified: Sep-18-2020, 02:39 PM by deanhystad.)
Something like this?
Look for the similarities and mark the block of repeated code. Now look at each difference to determine if it has to be different. In your code there ec2_details, ip_details and role_details play the same part and can have the same name (you know this because you changed these names when you were cutting and pasting your code). Change these to all be the same for each copy. Now the only thing that should be different between copies is the code that makes each function unique. Look for a way that you can remove this code or make it more general. In my first example I removed the code, in the second I made it more general.
def item_details(items): details = [] for reservation in items['Reservations']: for instance in reservation['Instances']: item_info = {} item_info.update({'InstanceID':instance['InstanceId']}) item_info.update({'State':instance['State']['Name']}) item_info.update({'PrivateIpAddress':instance['PrivateIpAddress']}) item_info.update({'InstanceType':instance['InstanceType']}) for tag in instance['Tags']: if tag['Key'] in 'Name': item_info.update({'Name':tag['Value']}) for tag in instance['Tags']: if tag['Key'] in 'environment': item_info.update({'Environment':tag['Value']}) else: ec2_info.setdefault('Environment','MISSING') for tag in instance['Tags']: if tag['Key'] in 'role': item_info.update({'Role':tag['Value']}) else: item_info.setdefault('Role','MISSING') details.append(item_info) return details items = ec2.describe_instances( Filters=[{'Name': 'tag:environment', 'Values': [???]}, ] ) instance_details = item_details(items) items = ec2.describe_instances( Filters=[{'Name': 'private-ip-address', 'Values': [???]},]) ip_details = item_details(items)Or maybe this:
def item_details(name, value): items = ec2.describe_instances( Filters=[{'Name': name, 'Values': [value]}, ]) details = [] for reservation in items['Reservations']: for instance in reservation['Instances']: item_info = {} item_info.update({'InstanceID':instance['InstanceId']}) item_info.update({'State':instance['State']['Name']}) item_info.update({'PrivateIpAddress':instance['PrivateIpAddress']}) item_info.update({'InstanceType':instance['InstanceType']}) for tag in instance['Tags']: if tag['Key'] in 'Name': item_info.update({'Name':tag['Value']}) for tag in instance['Tags']: if tag['Key'] in 'environment': item_info.update({'Environment':tag['Value']}) else: ec2_info.setdefault('Environment','MISSING') for tag in instance['Tags']: if tag['Key'] in 'role': item_info.update({'Role':tag['Value']}) else: item_info.setdefault('Role','MISSING') details.append(item_info) return details instance_details = item_details('tag:environment', ???) ip_details = item_details('private-ip-address', ???)Which one works better depends on how much flexibility you need it selecting the items of interest.
Look for the similarities and mark the block of repeated code. Now look at each difference to determine if it has to be different. In your code there ec2_details, ip_details and role_details play the same part and can have the same name (you know this because you changed these names when you were cutting and pasting your code). Change these to all be the same for each copy. Now the only thing that should be different between copies is the code that makes each function unique. Look for a way that you can remove this code or make it more general. In my first example I removed the code, in the second I made it more general.