0

I have a function

def static_nat_fwsm_check(mapped_to_real_address_pairs,FWSM_ACLs):
  amended_static_nat_acl=[]
  for acl_line in FWSM_ACLs:
    for i,j in mapped_to_real_address_pairs:
      if j in acl_line:
        amended_static_nat_acl.append('!STATIC NAT SUBSTITUTION FOR '+acl_line)
        amended_static_nat_acl.append(re.sub(j,i,acl_line))
        break
    else:
      amended_static_nat_acl.append(acl_line)
  return amended_static_nat_acl

The idea behind the function is to take a list of tuples and a list of strings as arguments. The tuples contain a mapping of real addresses to mapped addresses. If the mapped address is found in the string in the list, then it is replaced with the real address

The last line in the list of strings is added twice to the new list I am creating. When I step through the function adding print statements, it looks like the last tuple in the list is evaluated twice.

I could sidestep the problem by adding another if statement to the else clause, but I'd like to understand where I'm going wrong. Any assistance greatly appreciated!

Keiwan
  • 8,031
  • 5
  • 36
  • 49
  • 2
    The way your indentation looks, that `else` clause actually belongs to the `for` statement ([which is perfectly legal](http://stackoverflow.com/questions/9979970/why-does-python-use-else-after-for-and-while-loops)) - is that what you intended / what your code really looks like? – Lukas Graf May 31 '16 at 17:00
  • 1
    Maybe the problem is not in this function. Is it possible the last string is actually in `FWSM_ACLs` twice? – evergreen May 31 '16 at 18:29
  • @evergreen that's the first thing I checked. It's only there once. LukasGraf I tested this. If the else clause is moved to the first for loop, then it only evaluates lines where substitutions need to be made, and the very last line in the FWSM_ACLs. I need to go through FWSM_ACLs, substitute j with i, and also append the line "as is" if no substitution is made. I wonder if I am evaluating the right way. Maybe I should move the tuples to the outer for loop instead, and make the tuple list an empty list by default to catch the case where there are no tuples to evaluate against the list. – The Stupid Engineer Jun 01 '16 at 08:29
  • @evergreen looks like the input to the function is generated differently to how I thought! Thanks for pointing me in that direction :) How can I mark your comment as the answer? – The Stupid Engineer Jun 01 '16 at 08:53
  • 1
    No need. I didn't really have the whole answer. I just couldn't see any way that the code you posted could put the same string in the list more than once, so I started wondering if the problem was elsewhere. – evergreen Jun 01 '16 at 09:39

1 Answers1

0

User error. I used a previous function to generate input for this function. Loosely defined regex, plus an ACL name that was a subset of other ACL names caused the duplicate output.