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:
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_detailsWhere 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?