文 | 遠走高飛 微信公眾號 周末求發展
def get_files_path(dir_path,queue): for root, dirs, files in os.walk(dir_path): for file in files: if os.path.splitext(file)[1] in [".py",".java",".c"]: file_path = os.path.join(root,file) queue.put(file_path) 2.實現函數,統計代碼行
1)跳過多行注釋,不進行統計
2)跳過單行注釋,不進行統計
3)跳過空白行,不進行統計
4)其它的情況,統計代碼行數def python_lines_count(file_path,encoding): lines_count_python = 0 flag_python = 0 try: with open(file_path, "r", encoding=encoding) as fp: for line in fp: if line.strip() == "'''" or line.strip()[1:4] == "'''" or line.strip()[ -3:] == "'''": flag_python += 1 elif flag_python % 2 != 0: continue elif line.strip() in string.whitespace: pass elif line.lstrip()[0] == "#" or line.lstrip()[1] == "#": pass else: lines_count_python += 1 return lines_count_python except Exception as e: print(e) return 0
def java_c_lines_count(file_path,encoding): lines_count_java_c = 0 flag_java = 0 try: with open(file_path, "r", encoding=encoding) as fp: for line in fp: if line.strip()[:2] == "/*" or line.strip()[-2:] == "*/": flag_java += 1 elif flag_java % 2 != 0: continue elif line.strip() in string.whitespace: pass elif line.lstrip()[:2] == "//": pass else: lines_count_java_c += 1 return lines_count_java_c except Exception as e: print(e) return 03.實現函數,從隊列中獲取文件路徑,並統計代碼行1)使用queue.empty()判斷,當隊列不為空時,從隊列中獲取文件路徑
2)使用os.path.splitext(),根據文件後綴判斷,調用對應統計代碼行函數,計算每個文件的代碼行數
3)將每個文件的代碼行數,分別加到對應的進程共享變量total_count_python,total_count_java,total_count_c的value值中。def get_code_lines(queue,total_count_python,total_count_java,total_count_c): while not queue.empty(): file_path = queue.get() file_path_suffix = os.path.splitext(file_path)[1] if file_path_suffix == ".py": try: lines_count_python = python_lines_count(file_path,"utf-8") except: lines_count_python = python_lines_count(file_path, "gbk") total_count_python.value += lines_count_python elif file_path_suffix in [".java",".c"]: lines_count_java_c = java_c_lines_count(file_path,"utf-8") if file_path_suffix == ".java": total_count_java.value += lines_count_java_c elif file_path_suffix == ".c": total_count_c.value += lines_count_java_c return4.使用多進程與隊列,實現代碼行數統計1)使用multiprocessing.Queue() ,定義一個隊列,用於存取代碼文件的絕對路徑。
2)使用multiprocessing.Value(「d」,0),定義主進程與子進程之間的共享變量,用於存儲每種類型的代碼行數。
3)使用multiprocessing.Process(),創建一個子進程,用於將文件路徑放到隊列中
4)使用multiprocessing.Process(),創建多個子進程,用於從隊列中獲取文件路徑,並統計代碼行數。def get_total_code_lines(dir_path):#使用多進程與隊列,實現代碼行數統計 start = time.time() queue = multiprocessing.Queue(1000) #定義一個隊列,用於存取代碼文件的絕對路徑 total_count_python = multiprocessing.Value("d", 0) # python代碼總行數,d表示數值,主進程與子進程共享這個value。(主進程與子進程都是用的同一個value) total_count_java = multiprocessing.Value("d", 0) # java代碼總行數 total_count_c = multiprocessing.Value("d", 0) # c語言代碼總行數 p_get_path = multiprocessing.Process(target=get_files_path,args=(dir_path,queue)) #創建一個進程,用於獲取所有的代碼文件絕對路徑 p_get_path.start() #啟動進程 num_cpu = multiprocessing.cpu_count() # 獲取當前計算機的cpu核數 p_get_lindes = [ multiprocessing.Process(target=get_code_lines,args=(queue,total_count_python,total_count_java,total_count_c)) for i in range(num_cpu)] #創建多個進程,計算每個文件的代碼行數 for p in p_get_lindes:#循環啟動進程 p.start() p.join() p_get_path.join() #執行join方法,待子進程全部結束後,再執行主進程的之後的代碼邏輯
print("python代碼總行數:",int(total_count_python.value)) print("java代碼總行數:", int(total_count_java.value)) print("c語言代碼總行數:", int(total_count_c.value)) end = time.time() tatal_time = end - start #計算統計代碼的總耗時 print("總耗時:",tatal_time) return int(total_count_python.value),int(total_count_java.value),int(total_count_c.value),tatal_time5.使用tkinter做一個簡單的圖形界面1)使用askdirectory()獲取選擇的文件夾路徑。
2)定義點擊事件,將獲取到的文件夾路徑做為參數傳入,調用統計代碼行的函數,計算代碼行數。
3)將計算的結果,更新到圖形界面。def selectPath(): path_ = askdirectory() path.set(path_)
def click_submit(): dir_path=path.get() total_line_count = get_total_code_lines(dir_path) total_count_python.set(str(total_line_count[0])) total_count_java.set(str(total_line_count[1])) total_count_c.set(str(total_line_count[2])) total_time.set("%.3f" %total_line_count[3])
def add_Label_Entry(row,text,textvariable): Label(windows, text=text).grid(row=row, column=2,ipadx=5, pady=5) Entry(windows, textvariable=textvariable).grid(row=row, column=3,ipadx=5, pady=5)
if __name__ == '__main__': windows = Tk() windows.title("統計代碼行工具") windows.geometry("400x400") path = StringVar() total_count_python = StringVar() total_count_java = StringVar() total_count_c = StringVar() total_time = StringVar()
Label(windows,text = "代碼行統計路徑:").grid(row = 10, column = 2,ipadx=5, pady=50) Entry(windows, textvariable=path).grid(row=10, column=3,ipadx=20, pady=20) Button(windows, text="選擇", command=selectPath).grid(row=10, column=4,ipadx=20, pady=20)
Button(windows, text="開始統計", command=click_submit).grid(row=20, column=3,ipadx=60, pady=30) add_Label_Entry(60,"python代碼總行數:",total_count_python) add_Label_Entry(70, "java代碼總行數:",total_count_java) add_Label_Entry(80, "C語言代碼總行數:",total_count_c) add_Label_Entry(90, "總耗時(s):",total_time)
windows.mainloop()6.最終效果1)點擊「選擇」按鈕,選擇需要統計代碼的目錄
2)點擊「開始統計」按鈕,進行統計
3)統計完成後,在下方展示統計結果
👆單擊連結直達、長按諮詢吳老師或公眾號內回復「2」
無論上課或自學,
你首先需要準備:
每天 2 小時+的學習時間,
每天堅持寫代碼的習慣!
有投入才有產出,
10k+的漲幅需要 1 年以上的努力!
祝你成功!
光榮之路出品
測試大佬和小白的故事
招聘QQ群:203715128