-3

这是下面stackoverflow问题的延续问题:如何过滤嵌套案例以过滤出python 如何比较2个函数中以$开头的属性并显示匹配或不匹配

当我删除if$apsChanConfigNumbernvp_add第一个块中的一个时,上面链接中的比较 python 脚本无法检测到不匹配,nvp_add在这种情况下有 2 个函数。求助怎么解决!!!

输入文件 ASCII 纯文本包含以下文本:


        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)
            }
4

1 回答 1

0

自从我开始,我就结束了: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()
于 2013-08-20T05:56:04.623 回答