Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

python - How to compare the attributes start with $ in 2 functions and display match or mismatch

My input file contain attributes

        if(match($OPTION_EnableDetails, "1") or match($OPTION_EnableDetails_juniper, 
   "1")) {
            details($juniFileXferStatus,$juniFileXferTimeStamp,$juniFileXferIndex)
        }
        @ExtendedAttr = nvp_add(@ExtendedAttr, "juniFileXferStatus", 
    $juniFileXferStatus, "juniFileXferTimeStamp", $juniFileXferTimeStamp, 
  "juniFileXferIndex",
 $juniFileXferIndex)

I got lot of cases in my input file, How can i compare all instances start with $ in details and instances start with $ in nvp_add here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
import re


caselines_index = []
cases = []
readlines = []

def read(in_file):
  global cases
  global caselines_index
  global readlines
  with open(in_file, 'r') as file:
    for line in file.readlines():
      readlines.append(line.strip())
    for line in readlines:
      case_search = re.search("cases".+?":s", line)
      if case_search:
        caselines_index.append(readlines.index(line))
    #print caselines_index
    caselines_index_iter = iter(caselines_index)
    int_line_index = int(next(caselines_index_iter))
    int_next_index = int(next(caselines_index_iter))
    while True:
      try:
        case_text = ' '.join(readlines[int_line_index:int_next_index]).strip()
        case = [readlines[int_line_index].strip(), 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(), case_text]
        cases.append(case)
        break

def work():
  MATCH = 1
  for case_list in cases:
    details = []
    nvp_add = []
    caseline = case_list[0].strip()
    nvp = re.findall("details(.+?)", case_list[1].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[1].strip())

    for item in nvp:
      result_list = re.findall("($.+?)[,)]", item)

      for result in result_list:
        if "$*" not in result:
          nvp_add.append(result)

    missing_from_details, missing_from_nvp_add = [], []
    missing_from_details = [o for o in nvp_add if o not in set(details)]
    missing_from_nvp_add = [o for o in details if o not in set(nvp_add)]
    if missing_from_nvp_add or missing_from_details:
      MATCH = 0
      print caseline + "   LINE - " + str(readlines.index(caseline) + 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 "
"
  if MATCH == 1:
    print "MATCH"
  else:
    print "MISMATCHES"


def main():
  in_file = "target1.txt"
  read(in_file)
  work()


if __name__=="__main__":
  main()

If i understand correctly this is it


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...