Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Duplicate functions
#1
Hi,

So I am writing a small Python app to carry out a few tasks and print the findings to the screen using the @Click module. Although it works as expected I have a lot of duplication in my functions:

role_details = []

    get_role = ec2.describe_instances(
        Filters=[
            {
                'Name': 'tag:role',
                'Values': [role]
            },])
    for reservation in get_role['Reservations']:
        for instance in reservation['Instances']:
            role_info = {}
            role_info.update({'InstanceID':instance['InstanceId']})
            role_info.update({'State':instance['State']['Name']})
            role_info.update({'PrivateIpAddress':instance['PrivateIpAddress']})
            role_info.update({'InstanceType':instance['InstanceType']})
            for tag in instance['Tags']:
                if tag['Key'] in 'Name':
                    role_info.update({'Name':tag['Value']})
            for tag in instance['Tags']:
                if tag['Key'] in 'environment':
                    role_info.update({'Environment':tag['Value']}) 
                else:
                    role_info.setdefault('Environment','MISSING')
            for tag in instance['Tags']:
                if tag['Key'] in 'role':
                    role_info.update({'Role':tag['Value']}) 
                else:
                    role_info.setdefault('Role','MISSING')           
            role_details.append(role_info)
    return role_details
Where the Filters(Name/Value) differ between functions. I wondered whether there is a way to achieve the same results but consolidate into a single function where the Filters are the only changeable values?
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