Compile a Python program to calculate the number of rows

Source: Internet
Author: User

When we use the Python programming language for program development, we will find that this powerful language can play a very important role for us. Then, we will use the implementation of a Python program to carefully analyze the uniqueness of this language.

  • In-depth discussion on the correct application of the Python ConfigParser Module
  • How to Use Python to decrypt the basic VBS application code
  • How to configure the Python CGI Environment in different operating systems
  • Python Algorithm Implementation
  • Analysis on the correct use of Python print

I have been paying attention to Python and haven't written a program in Python. Today I wrote a simple Python program for code statistics at noon. Recursively search the input path, find the code file, and calculate the number of lines of comments, empty lines, and real lines of code for each code file. If you use a program, you can write it roughly without exception handling.

The main Python script file LineCount. py contains the following content:

 
 
  1. import sys;  
  2. import os;  
  3. class LineCount:  
  4. def trim(self,docstring):  
  5. if not docstring:  
  6. return ''  
  7. lines = docstring.expandtabs().splitlines()  
  8. indent = sys.maxint  
  9. for line in lines[1:]:  
  10. stripped = line.lstrip()  
  11. if stripped:  
  12. indent = min(indent, len(line) - len(stripped))  
  13. trimmed = [lines[0].strip()]  
  14. if indent < sys.maxint: 
  15. for line in lines[1:]:  
  16. trimmed.append(line[indent:].rstrip())  
  17. while trimmed and not trimmed[-1]:  
  18. trimmed.pop()  
  19. while trimmed and not trimmed[0]:  
  20. trimmed.pop(0)  
  21. return '\n'.join(trimmed)  
  22. def FileLineCount(self,filename):  
  23. (filepath,tempfilename) = os.path.split(filename);  
  24. (shotname,extension) = os.path.splitext(tempfilename);  
  25. if extension == '.txt' or extension == '.hol' : # file type   
  26. file = open(filename,'r');  
  27. self.sourceFileCount += 1;  
  28. allLines = file.readlines();  
  29. file.close();  
  30. lineCount =0;  
  31. commentCount = 0;  
  32. blankCount = 0;  
  33. codeCount = 0;  
  34. for eachLine in allLines:  
  35. if eachLine != " " :  
  36. eachLineeachLine = eachLine.replace(" ",""); #remove space  
  37. eachLine = self.trim(eachLine); #remove tabIndent  
  38. if eachLine.find('--') == 0 : #LINECOMMENT   
  39. commentCount += 1;  
  40. else :  
  41. if eachLine == "":  
  42. blankCount += 1;  
  43. else :  
  44. codeCount += 1;  
  45. lineCountlineCount = lineCount + 1;  
  46. self.all += lineCount;  
  47. self.allComment += commentCount;  
  48. self.allBlank += blankCount;  
  49. self.allSource += codeCount;  
  50. print filename;  
  51. print ' Total :',lineCount ;  
  52. print ' Comment :',commentCount;  
  53. print ' Blank :',blankCount;  
  54. print ' Source :',codeCount;  
  55. def CalulateCodeCount(self,filename):  
  56. if os.path.isdir(filename) :  
  57. if not filename.endswith('\\'):  
  58. filename += '\\';   
  59. for file in os.listdir(filename):  
  60. if os.path.isdir(filename + file):  
  61. self.CalulateCodeCount(filename + file);  
  62. else:  
  63. self.FileLineCount(filename + file);  
  64. else:  
  65. self.FileLineCount(filename);  
  66. # Open File  
  67. def __init__(self):  
  68. self.all = 0;  
  69. self.allComment =0;  
  70. self.allBlank = 0;  
  71. self.allSource = 0;  
  72. self.sourceFileCount = 0;  
  73. filename = raw_input('Enter file name: ');  
  74. self.CalulateCodeCount(filename);  
  75. if self.sourceFileCount == 0 :  
  76. print 'No Code File';  
  77. pass;  
  78. print '\n';  
  79. print '***************** All Files **********************';  
  80. print ' Files :',self.sourceFileCount;  
  81. print ' Total :',self.all;  
  82. print ' Comment :',self.allComment;  
  83. print ' Blank :',self.allBlank;  
  84. print ' Source :',self.allSource;  
  85. print '****************************************************';  
  86. myLineCount = LineCount(); 

We can see that extension = '.txt 'or extension ='. hol 'is used to determine the file suffix and determine whether to calculate the number of lines of code. If eachLine. find ('--') = 0: This sentence is used to determine whether the current row is a single line comment. Our language does not support block comment ). To run on other machines, py2exe is used to generate an executable exe from the Python script. The content of the setup. py script is as follows:

 
 
  1. from distutils.core import setup  
  2. import py2exe  
  3. setup(  
  4. version = "0.0.1",  
  5. description = "LineCount",  
  6. name = "LineCount",  
  7. console = ["LineCount.py"],  

However, after the exe is generated, the program is too bloated, with more than 3 m. I feel that using Python is indeed a pleasant task.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.