import easygui as g
The import OS
Codingdict={}
Typedict={}
Linecountdict={}
Def ChoiceCountType () :
"' choose statistics which code file type '"'
Global codingdict
Global typedict
Global linecountdict
Choices=[". Py ", "c", "CPP", ". TXT ", "cs", ". Aspx "] # setup code file options
Usertype=g. ultchoicebox (MSG="please choose from the following list to statistics the code file type!" Title="code statistics", choices=choices)
Codingdict=dict. Fromkeys (usertype, [0, 0]) # give it an initial value type dictionary {type, [number, number of effective code]}
# typedict=dict. Fromkeys (usertype, 0)
# linecountdict=dict. Fromkeys (usertype, 0)
Dirpath=g.d iropenbox ()
# print (codingdict)
Return dirpath
Def calc_code (paths, file_name) :
"' effective function of lines of code statistics'
Codinglines=0
With the open (paths + "\ " + file_name, errors="ignore") as fp:
Print (' are analyzing file: % s... '% file_name)
Dateline=fp. Readlines ()
For the line in dateline:
If the line=="\ n" :
The continue
Elif line. Startswith (" # ") :
The continue
The else:
Codinglines +=1
Return codinglines
Def ChoiceDir (dirpath) :
"' using OS. Walk () through calculating the number of files and effective number of code were put into the corresponding list '"'
# global typedict
# global linecountdict
Global codingdict
For paths, dirs, files in OS. Walk (dirpath) :
If len (files) & gt; # 0: if there is a file, execute the next step traverse the file list
For file in files:
Ext=OS. Path. Splitext (file) [1] # section type, and then judge the type is in programming file
If ext in codingdict. Keys () : # if the type of cut out in the KEY of the dictionary
Lines=calc_code (paths, file) # current file effective statistical lines
Codingdict [ext] [1] +=# lines dictionary [the current file type] [the type effective rows] on the lines
Codingdict [ext] [0] +=1 # dictionary [the current file type] [number] this type of file on the 1
Def ShowResult () :
MSG=""
Text=""
Title="results"
Totallines=0
Chaju=0
For each in codingdict. Keys () :
Text +=(f "[each] attach file types have {codingdict [each] [0]}, total amount of effective code {codingdict [each] [1]} \ n")
Totallines +=codingdict [each] [1]
Jindu="% 2 f" % % % (totallines/1000)
If (100000 - totallines) & lt;=0:
MSG=f "you currently {totallines} were accumulated to write lines of code to the completed progress: {jindu} \ n amount of code you have already completed the 100000 lines of code, great!"
The else:
Chaju=100000 - totallines
MSG=f "you currently {totallines} were accumulated to write lines of code to the completed progress: {jindu} \ n from 100000 lines of code are {chaju}, please continue to work hard!"
G.t extbox (MSG, the title and text)
# main program
Openpath=ChoiceCountType ()
ChoiceDir (openpath)
ShowResult ()
Breakpoint testing, the problem should be out in the
for paths, dirs, files in OS. Walk (dirpath) :
If len (files) & gt; # 0: if there is a file, execute the next step traverse the file list
For file in files:
Ext=OS. Path. Splitext (file) [1] # section type, and then judge the type is in programming file
If ext in codingdict. Keys () : # if the type of cut out in the KEY of the dictionary
Lines=calc_code (paths, file) # current file effective statistical lines
Codingdict [ext] [1] +=# lines dictionary [the current file type] [the type effective rows] on the lines
Codingdict [ext] [0] +=1 # dictionary [the current file type] [number] this type of file on the 1
In this code, the original imagination is each traverse a file, if the ext in the key, the codingdict [corresponding file suffix] [0] (file number) and [1] (the type of effective rows) are corresponding increase,
Breakpoint test is found in the dictionary when all key corresponding to the value added, is not the corresponding file type increases, the old man have to do this?
CodePudding user response:
codingdict=dict. Fromkeys (usertype, (0, 0))==& gt; Codingdict=dict. Fromkeys (usertype, None)
# add one line
Codingdict [ext]=[0, 0] if codingdict (ext)==None else codingdict (ext)
Codingdict [ext] [1] +=# lines dictionary [the current file type] [the type effective rows] on the lines
Codingdict [ext] [0] +=1
CodePudding user response:
d_list=[d.s (trip) for d in codingdict. Keys () splitlines ()]