In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you what the Python script file LineCount.py related code is, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.
Because the recent project is very special, the language used is an internal IDE environment, and the code generated by this IDE is not stored in text, but in binaries, and because the language is almost inaccessible to the outside world, there is no code statistics program for it, so it is difficult to count lines of code when a module is completed. To count, you must first copy the contents of the code editor to a text type file.
I happen to be following python all the time. I haven't written a program in python yet, so I wrote a simple code statistics program during the noon break today. Recursively enter the path, find the code file, and calculate the number of comment lines, blank lines, and real lines of code for each code file. The program I use is rough, and there is no exception handling.
The contents of the main Python script file LineCount.py are as follows:
Import sys; import os Class LineCount: def trim (self,docstring): if not docstring: return''lines = docstring.expandtabs () .splitlines () indent = sys.maxint for line in lines [1:]: stripped = line.lstrip () if stripped: indent = min (indent Len (line)-len (stripped) trimmed = [lines [0] .strip ()] if indent < sys.maxint: for line in lines [1:]: trimmed.append (line[ indent:] .rstrip () while trimmed and not trimmed [- 1]: trimmed.pop () while trimmed and not trimmed [0]: trimmed.pop (0) return'\ n'.join (trimmed) def FileLineCount (self,filename): (filepath,tempfilename) = os.path.split (filename) (shotname,extension) = os.path.splitext (tempfilename); if extension = '.txt' or extension = = '.hol': # file type file = open (filename,'r'); self.sourceFileCount + = 1; allLines = file.readlines (); file.close (); lineCount = 0; commentCount = 0; blankCount = 0; codeCount = 0; for eachLine in allLines: if eachLine! = ": eachLineeachLine = eachLine.replace (", ") # remove space eachLine = self.trim (eachLine); # remove tabIndent if eachLine.find ('-') = = 0: # LINECOMMENT commentCount + = 1; else: if eachLine = "": blankCount + = 1; else: codeCount + = 1; lineCountlineCount = lineCount + 1; self.all + = lineCount; self.allComment + = commentCount; self.allBlank + = blankCount; self.allSource + = codeCount; print filename; print 'Total:', lineCount; print 'Comment:', commentCount; print 'Blank:', blankCount Print 'Source:', codeCount; def CalulateCodeCount (self,filename): if os.path.isdir (filename): if not filename.endswith ('\'): filename + ='\\'; for file in os.listdir (filename): if os.path.isdir (filename + file): self.CalulateCodeCount (filename + file); else: self.FileLineCount (filename + file); else: self.FileLineCount (filename); # Open File def _ init__ (self): self.all = 0 Self.allComment = 0; self.allBlank = 0; self.allSource = 0; self.sourceFileCount = 0; filename = raw_input ('Enter filename:'); self.CalulateCodeCount (filename); if self.sourceFileCount = 0: print'No Code File'; pass; print'\ n; print'* All Files * *' Print 'Files:', self.sourceFileCount; print 'Total:', self.all; print 'Comment:', self.allComment; print 'Blank:', self.allBlank; print 'Source:', self.allSource; print'* *'; myLineCount = LineCount ()
You can see that the sentence extension = '.txt' or extension = = '.hol' determines the suffix of the file to determine whether to count the number of lines of code. If eachLine.find ('- -') = = 0: this sentence is used to determine whether the current line is a single-line comment (our language does not support block comments) the above is an introduction to the code related to the Python script file LineCount.py. In order to run on other machines, py2exe is used to generate python scripts into executable exe,setup.py scripts as follows:
From distutils.core import setup import py2exe setup (version = "0.0.1", description = "LineCount", name = "LineCount", console = ["LineCount.py"],)
However, after generating exe, the program is very bloated, with more than 3M. I feel that it is really comfortable to use python.
The above content is the Python script file LineCount.py related code is, have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.