|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import easygui as g
import os
codingdict = {}
typedict = {}
linecountdict = {}
def ChoiceCountType():
'''选择要统计哪些代码文件类型'''
global codingdict
global typedict
global linecountdict
choices = [".py", ".c", ".cpp", ".txt", ".cs", ".aspx"] # 设置代码文件选项
usertype = g.multchoicebox(msg="请从下列列表中选择要统计的代码文件类型!", title="代码统计", choices=choices)
codingdict = dict.fromkeys(usertype, [0, 0]) # 给类型字典赋初值 {类型,[个数,有效代码数]}
# typedict=dict.fromkeys(usertype,0)
# linecountdict=dict.fromkeys(usertype,0)
dirpath = g.diropenbox()
# print(codingdict)
return dirpath
def calc_code(paths, file_name):
'''统计有效代码行的函数'''
codinglines = 0
with open(paths + "\" + file_name, errors="ignore") as fp:
print('正在分析文件:%s ...' % file_name)
dateline = fp.readlines()
for line in dateline:
if line == "\n":
continue
elif line.startswith("#"):
continue
else:
codinglines += 1
return codinglines
def ChoiceDir(dirpath):
'''用os.walk()遍历计算文件数量和有效代码数分别存进对应列表中'''
# global typedict
# global linecountdict
global codingdict
for paths, dirs, files in os.walk(dirpath):
if len(files) > 0: # 如果有文件,就执行下一步的遍历文件列表
for file in files:
ext = os.path.splitext(file)[1] # 切片出类型,然后判断出类型在不在编程类型文件里
if ext in codingdict.keys(): # 如果切出来的类型在字典的KEY中
lines = calc_code(paths, file) # 统计当前文件有效行数
codingdict[ext][1] += lines #字典[当前文件类型][该类型有效行数]自增lines
codingdict[ext][0] += 1 #字典[当前文件类型][该类型文件个数]自增1
def ShowResult():
msg = ""
text = ""
title = "统计结果"
totallines = 0
chaju = 0
for each in codingdict.keys():
text+=(f"[{each}]类型的文件有{codingdict[each][0]}个,共计有效代码量{codingdict[each][1]}行\n")
totallines+=codingdict[each][1]
jindu = "%.2f %%" % (totallines / 1000)
if (100000 - totallines) <= 0:
msg = f"您目前共累积编写了{totallines}行代码,完成进度:{jindu}\n你已经完成了10万行代码的代码量,牛逼的哟!"
else:
chaju = 100000 - totallines
msg = f"您目前共累积编写了{totallines}行代码,完成进度:{jindu}\n离 10 万行代码还差{chaju}行,请继续努力!"
g.textbox(msg, title, text)
# 主程序
openpath = ChoiceCountType()
ChoiceDir(openpath)
ShowResult()
断点测试了下,问题应该是出在了 for paths, dirs, files in os.walk(dirpath):
if len(files) > 0: # 如果有文件,就执行下一步的遍历文件列表
for file in files:
ext = os.path.splitext(file)[1] # 切片出类型,然后判断出类型在不在编程类型文件里
if ext in codingdict.keys(): # 如果切出来的类型在字典的KEY中
lines = calc_code(paths, file) # 统计当前文件有效行数
codingdict[ext][1] += lines #字典[当前文件类型][该类型有效行数]自增lines
codingdict[ext][0] += 1 #字典[当前文件类型][该类型文件个数]自增1
这段代码里,原本想象是每遍历一个文件,如果ext在key里,那codingdict[对应文件后缀][0](文件个数)及[1](该类型有效行数)都对应的增加。
断点测试时发现是字典里所有key对应的值都增加了,并不是对应的文件类型增加。老哥们这个得咋整?
本帖最后由 Twilight6 于 2020-7-28 23:25 编辑
不用 fromkeys
import easygui as g
import os
codingdict = {}
typedict = {}
linecountdict = {}
def ChoiceCountType():
'''选择要统计哪些代码文件类型'''
global codingdict
global typedict
global linecountdict
choices = [".py", ".c", ".cpp", ".txt", ".cs", ".aspx"] # 设置代码文件选项
usertype = g.multchoicebox(msg="请从下列列表中选择要统计的代码文件类型!", title="代码统计", choices=choices)
codingdict = {}
for i in usertype:
codingdict[i] = [0,0]
# typedict=dict.fromkeys(usertype,0)
# linecountdict=dict.fromkeys(usertype,0)
dirpath = g.diropenbox()
# print(codingdict)
return dirpath
def calc_code(paths, file_name):
'''统计有效代码行的函数'''
codinglines = 0
with open(paths + "\" + file_name, errors="ignore") as fp:
print('正在分析文件:%s ...' % file_name)
dateline = fp.readlines()
for line in dateline:
if line == "\n":
continue
elif line.startswith("#"):
continue
else:
codinglines += 1
return codinglines
def ChoiceDir(dirpath):
'''用os.walk()遍历计算文件数量和有效代码数分别存进对应列表中'''
# global typedict
# global linecountdict
global codingdict
for paths, dirs, files in os.walk(dirpath):
if len(files) > 0: # 如果有文件,就执行下一步的遍历文件列表
for file in files:
ext = os.path.splitext(file)[1] # 切片出类型,然后判断出类型在不在编程类型文件里
if ext in codingdict.keys(): # 如果切出来的类型在字典的KEY中
lines = calc_code(paths, file) # 统计当前文件有效行数
codingdict[ext][1] += lines #字典[当前文件类型][该类型有效行数]自增lines
codingdict[ext][0] += 1 #字典[当前文件类型][该类型文件个数]自增1
def ShowResult():
msg = ""
text = ""
title = "统计结果"
totallines = 0
chaju = 0
for each in codingdict.keys():
text+=(f"[{each}]类型的文件有{codingdict[each][0]}个,共计有效代码量{codingdict[each][1]}行\n")
totallines+=codingdict[each][1]
jindu = "%.2f %%" % (totallines / 1000)
if (100000 - totallines) <= 0:
msg = f"您目前共累积编写了{totallines}行代码,完成进度:{jindu}\n你已经完成了10万行代码的代码量,牛逼的哟!"
else:
chaju = 100000 - totallines
msg = f"您目前共累积编写了{totallines}行代码,完成进度:{jindu}\n离 10 万行代码还差{chaju}行,请继续努力!"
g.textbox(msg, title, text)
# 主程序
openpath = ChoiceCountType()
ChoiceDir(openpath)
ShowResult()
|
|