Home > Back-end >  Python values not getting added to Tuple
Python values not getting added to Tuple

Time:02-14

Hello I have a situation where I am trying to make a filter in my code customizable so there is minimum duplication of code below is the code that I want to update

for ent in NACLS:
        response = ec2.describe_network_acls(Filters=[{'Name': 'network-acl-id', 'Values': [naclID]},
                                                      {'Name': 'entry.rule-number', 'Values': [str(ent[0])]},
                                                      {'Name': 'entry.protocol', 'Values': [ProtoDict[ent[1]]]},
                                                      {'Name': 'entry.port-range.from', 'Values': [str(ent[2])]},
                                                      {'Name': 'entry.port-range.to', 'Values': [str(ent[2])]},
                                                      {'Name': 'entry.rule-action', 'Values': ["deny"]},
                                                      ])

I want the filter to customizable for example

for ent in NACLS:
  if add = True: 
     response = ec2.describe_network_acls(Filters=[{'Name': 'network-acl-id', 'Values': [naclID]},
                                                   {'Name': 'entry.rule-number', 'Values': [str(ent[0])]},
                                              ])
  else:
    response = ec2.describe_network_acls(Filters=[{'Name': 'network-acl-id', 'Values': [naclID]},
                                                  {'Name': 'entry.rule-number', 'Values': [str(ent[0])]},
                                                  {'Name': 'entry.protocol', 'Values': [ProtoDict[ent[1]]]},
                                                  {'Name': 'entry.port-range.from', 'Values': [str(ent[2])]},
                                                  {'Name': 'entry.port-range.to', 'Values': [str(ent[2])]},
                                                  {'Name': 'entry.rule-action', 'Values': ["deny"]},
                                                  ])

This is what I was wanting to do but doesn't work please let me know if there is a better way to achieve this

for ent in NACLS:
    filters = {'Name': 'network-acl-id', 'Values': [naclID]}, {'Name': 'entry.rule-number', 'Values': [str(ent[0])]}
    filters = filters   tuple({'Name': 'entry.protocol', 'Values': [ProtoDict[ent[1]]]}) //we can add more but this was just to test
print(str(filters)[1:-1])

The output for this is -

{'Name': 'network-acl-id', 'Values': ['acl-08128a2540']}, {'Name': 'entry.rule-number', 'Values': ['80']}, 'Name', 'Values'

When I try to add the value in tuple it shows as blank can someone please guide me on what I am doing wrong here?

CodePudding user response:

I suspect that by adding tuples, you meant to add the dictionary to the tuple of filters, which is not what you are doing in your current script. I suggest that you replace

filters = filters   tuple({'Name': 'entry.protocol', 'Values': [ProtoDict[ent[1]]]})

with

filters = filters   ({'Name': 'entry.protocol', 'Values': [ProtoDict[ent[1]]]},)
  • Related