Python script couldnt detect mismatch when $instance deleted in only one of the value nvp_add in double if else input statement
Question: This is continuation question from _stackoverflow_ question below: How do I
filter nested cases to be filter out python [How to compare the attributes
start with $ in 2 functions and display match or
mismatch](http://stackoverflow.com/questions/18325605/how-to-compare-the-
attributes-start-with-in-2-functions-and-display-match-or-m/18325700#18325700)

When i delete one of the `$apsChanConfigNumber` from `nvp_add` in first block
of if, the compare python script from link above couldn't detect the mismatch,
there are 2 `nvp_add` function under this case. How to resolve the issue
help!!!

Input file ASCII plain text contain text below:

* * *
    
    
            if(exists($snmpTrapEnterprise))
            {
                if(match($OPTION_EnableDetails, "1") or 
         match($OPTION_EnableDetails_juniper, "1")) {
                       details($snmpTrapEnterprise,$apsChanStatusSwitchovers,$apsChanStatusCurrent,$apsChanConfigGroupName,$apsChanConfigNumber)
    
                }
                @ExtendedAttr = nvp_add(@ExtendedAttr, "snmpTrapEnterprise", $snmpTrapEnterprise, "apsChanStatusSwitchovers", $apsChanStatusSwitchovers, "apsChanStatusCurrent", $apsChanStatusCurrent,
                     "apsChanConfigGroupName", , "apsChanConfigNumber",)
            }
            else
            {
                if(match($OPTION_EnableDetails, "1") or 
      match($OPTION_EnableDetails_juniper, "1")) {
    
    
    
    details($apsChanStatusSwitchovers,$apsChanStatusCurrent,$apsChanConfigGroupName,$apsChanConfigNumber)
                    }
                    @ExtendedAttr = nvp_add(@ExtendedAttr, "apsChanStatusSwitchovers", 
          $apsChanStatusSwitchovers, "apsChanStatusCurrent", $apsChanStatusCurrent, 
          "apsChanConfigGroupName", $apsChanConfigGroupName,
                         "apsChanConfigNumber", $apsChanConfigNumber)
                }
    


Answer: since ive started, ill finish :P

    
    
    import re
    import sys
    from collections import Counter
    
    #sys.stdout = open("result.txt", 'w+')
    
    
    def intersect(list1, list2):
      for o in list1:
        if o in list2:
          list1.remove(o)
          list2.remove(o)
      return list1, list2
    
    def read(in_file):
      cases = []
      caselines_index = []
      readlines = []
      readlines_num = []
      with open(in_file, 'r') as file:
        readfile = file.read().strip()
        for line in readfile.split('\n'):
          readlines_num.append(line.strip())
        regex = re.compile("switch\(\$\d\).+?\}", re.DOTALL)
        readfile = re.sub(regex, ' ', readfile)
        for line in readfile.split('\n'):
          readlines.append(line.strip())
        for line in readlines:
          case_search = re.search("case\s\".+?\"\:\s", line)
          if case_search:
            caselines_index.append(readlines.index(line))
        #print caselines_index
        caselines_index_iter = iter(caselines_index)
        try:
          int_line_index = int(next(caselines_index_iter))
        except:
          print "No cases found"
        try:
          int_next_index = int(next(caselines_index_iter))
        except:
          int_next_index = len(readlines) - 1
        while True:
          try:
            case_text = ' '.join(readlines[int_line_index:int_next_index]).strip()
            match1 = re.search("nvp_add", case_text)
            match2 = re.search("details", case_text)
            if match1 or match2:
              case = [readlines[int_line_index].strip(), readlines_num.index(readlines[int_line_index]) + 1, case_text]
              cases.append(case)
            int_line_index = int_next_index
            int_next_index = int(next(caselines_index_iter))
          except StopIteration:
            case_text = ' '.join(readlines[int_line_index:len(readlines) - 1]).strip()
            case = [readlines[int_line_index].strip(), readlines_num.index(readlines[int_line_index]), case_text]
            cases.append(case)
            break
      return cases
    
    def work(cases):
      MATCH = 1
      for case_list in cases:
        details = []
        nvp_add = []
        caseline = case_list[0].strip()
        nvp = re.findall("details\(.+?\)", case_list[2].strip())
    
        for item in nvp:
          result_list = re.findall("(\$.+?)[\,\)]", item)
    
          for result in result_list:
            if "$*" not in result:
              details.append(result)
    
        nvp = re.findall("nvp_add\(.+?\)", case_list[2].strip())
    
        for item in nvp:
          result_list = re.findall("(\$.+?)[\,\)]", item)
    
          for result in result_list:
            if "$*" not in result:
              nvp_add.append(result)
    
    
        nvp_add_c = Counter(nvp_add)
        details_c = Counter(details)
        missing_from_details = list((nvp_add_c - details_c).elements())
        missing_from_nvp_add = list((details_c - nvp_add_c).elements())
        if missing_from_nvp_add or missing_from_details:
          MATCH = 0
          print caseline + "   LINE - " + str(case_list[1] + 1)
          for mismatch in missing_from_details:
            print "Missing from details:"
            print mismatch
          for mismatch in missing_from_nvp_add:
            print "Missing from nvp_add:"
            print mismatch
          print "\n"
      if MATCH == 1:
        print "MATCH"
      else:
        print "MISMATCHES"
    
    
    def main():
      in_file = "target1.txt"
      cases = read(in_file)
      work(cases)
    
    
    if __name__=="__main__":
      main()
    

