Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python values not getting added to Tuple

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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]]]},)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading