This article describes how to compile a Python applet to count the keywords of the test script. Examples in this article can not only count the number of keywords, but also be categorized by the primary keyword, if you need a certain program, you can refer to the general automated test project and write a lot of test code, if some basic functions and business functions need to be modified after a test script has been compiled for a long time, it is necessary to find the places that reference the modified function. Some ides support full-text search and reference search, some of them may not be simple, because statistical functions and other requirements will be used in the future, so a script is written. In addition to searching for referenced files in the full text under the same directory, you can also count the number of searched files. Multiple keywords can be searched at a time and can be classified by primary keywords.
# Encoding: UTF-8 import OS import sys import re reload (sys) sys. setdefaultencoding ("UTF-8") short_exclude = [". svn "," sendbox "] # file not checked, directory name long_exclude = [] # Full path extend_name = [". rb "] # specify the file suffix for check temp_key_words = [{" key ":" # Author: "," display ":" author "," times ":-1, "match": "include", "primary_key": True, },{ "key": "# [summary]", "display": "number of completed use cases ", "times":-1, "match": "incl Ude ",}, {" key ":" File. expand_path "," display ":" Number of stateful rows "," times ":-1," ignore_case ": True, },{" key ": "def \ s + test _", "display": "Number of valid use cases", "times":-1, "match": "regex", "ignore_case ": true ,}, {"key": "# def \ s + test _", "display": "Number of comment cases", "times":-1, "match ": "regex", "ignore_case": True,},] for kv in temp_key_words: if not "key" in kv: raise "The following list does not contain the key value! \ N % s "% kv if not" key "in kv: raise" is not displayed in the following list! \ N % s "% kv ['times '] = kv. get ('times ',-1) # The default value is if kv. get ("ignore_case", True) = False: # default case-insensitive flag = 0 else: flag = re. I kv ['pattern'] = re. compile (kv ['key'], flag) if kv. get ("primary_key", False): kv ['times '] = 1 import copy key_words = [] def deepcopy (objs): t_list = [] for obj in objs: t_list.append (copy. copy (obj) return t_list def loop_case (root_dir): t_sum = [] print root_dir sub_gen = OS. listdir (root_dir) for sub in sub_gen: if sub in short_exclude: # continue abs_path = OS. path. join (root_dir, sub) if long_exclude: is_exclude = False for exclude in long_exclude: if exclude = abs_path [-len (exclude):]: is_exclude = True break if is_exclude: continue print abs_path if OS. path. isdir (abs_path): print "dir" t_sum.extend (loop_case (abs_path) elif OS. path. isfile (abs_path): if not ". "+ abs_path.rsplit (". ", 1) [1] in extend_name: # continue print" file "global key_words = deepcopy (temp_key_words) t_sum.append (count_case (abs_path) not in the suffix Check range )) return t_sum def count_case (abs_path): t_dict ={} with open (abs_path) as f: for l in f: l = l. strip () match_rule (l) index = 0 count_result = [0] * len (key_words) for kv in key_words: if 'Primary _ key' in kv: t_dict ['Primary _ key'] = kv. get ('display') t_dict ['Primary _ key_value '] = kv. get ('Primary _ key_value ', "None ") count_result [index] =-1-kv ['times '] index + = 1 t_dict ['match _ result'] = count_result t_dict ['file _ path'] = abs_path return t_dict def match_rule (line): primary_key = None for kv in key_words: match = False if kv ['times '] = 0: # Check times are full. if kv is not checked again. get ('match', "") = "regex": # the matching method is: regular if kv ['pattern']. match (line): # match the regular expression successfully match = True else: # the default matching method is include if kv ['key'] in line: # contains the specified string match = True if match: if kv. get ('Primary _ key', False): kv ['Primary _ key_value '] = line. split (kv ['key']) [1]. strip () # kv ['Primary _ key'] = False kv ['times ']-= 1 # matching successful, similarly, the number of remaining matches-1 return primary_key def format_info (sum_list): tip_list = [] p_k_dict ={} for d in sum_list: p_k = d ['Primary _ key_value '] if p_k not in p_k_dict: p_k_dict [p_k] = [0] * len (key_words) temp_list = [] m = d ['match _ result'] temp_list.append ("File Name: % s \ n % s: % s \ n "% (d ['file _ path'], d ['Primary _ key'], d ['Primary _ key_value ']) for I in range (len (m): if 'Primary _ key' in key_words [I]: continue else: t_s = str (m [I]) temp_list.append ("% s: % s \ n" % (key_words [I] ["display"], t_s )) p_k_dict [p_k] [I] + = m [I] tip_list.append ("". join (temp_list )) p_k_dict [p_k] [0] + = 1 tip_list.append ("==================== primary Key Statistics segmentation line ================================ ") total_dict ={} for kv in key_words: if 'Primary _ key' not in kv: total_dict [kv ['display'] = 0 total_dict ['total files'] = 0 for k, v in p_k_dict.items (): temp_list = [] temp_list.append ("primary key: % s \ n total number of files: % s \ n "% (k, v [0]) for I in range (1, len (v )): temp_list.append ("% s: % s \ n" % (key_words [I] ["display"], str (v [I]) total_dict [key_words [I] ["display"] + = v [I] tip_list.append ("". join (temp_list )) total_dict ['total files'] + = v [0] tip_list.append ("================ ===== all statistical split lines ================================= ") temp_list = [] for k, v in total_dict.items (): temp_list.append ("All % s: % s \ n" % (k, v) tip_list.append ("". join (temp_list) tip_msg = "\ n ". join (tip_list) print tip_msg open (r "sum_case.log", "w "). write (tip_msg) if _ name __= = "_ main _": if len (sys. argv)> 1: root_list = sys. argv [1:] else: root_list = [OS. curdir] sum_list = [] for root_dir in root_list: if OS. path. exists (root_dir) and OS. path. isdir (root_dir): sum_list.extend (loop_case (root_dir) format_info (sum_list) else: print "the specified root directory is invalid \ n % s" % root_dir
You can check the keyword, file type, and files and directories by setting the beginning of the configuration.