Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Duplicate functions
#4
Something like this?
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.
Reply


Messages In This Thread
Duplicate functions - by Allegin - Sep-18-2020, 01:20 PM
RE: Duplicate functions - by deanhystad - Sep-18-2020, 01:31 PM
RE: Duplicate functions - by Allegin - Sep-18-2020, 02:02 PM
RE: Duplicate functions - by deanhystad - Sep-18-2020, 02:39 PM
RE: Duplicate functions - by Allegin - Sep-18-2020, 03:43 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020