Python脚本文件LineCount.py的相关代码介绍

开发 后端
以下的文章是通过Python脚本文件LineCount.py的相关实际代码来介绍Python脚本文件LineCount.py在实际操作中的具体应用的介绍。

Python脚本文件LineCount.py在实际运行的过程中会有很多简捷的技巧可供我们大家借鉴,我们可以将其使用在python脚本文件中,既Python脚本文件LineCount.py,如果你对Python脚本文件感兴趣的话,你就可以点击以下的文章。

因为最近在作的项目很特殊,所使用的语言是一个公司内部的IDE环境,而这个IDE所产生的代码并不是以文本方式存放的,都是放在二进制文件中,而且由于这门语言外界几乎接触不到,所以没有针对它的代码统计程序,当一个模块完成后要统计代码行数会很困难,要统计的话必须先把代码编辑器中的内容拷贝到一个文本类型的文件中。

正好一直在关注python,还没有用python写过程序,今天就利用中午休息的时间写了一个简单的代码统计程序。对输入的路径作递归,查找代码文件,对每一个代码文件计算它的注释行数,空行数,真正的代码行数。自己用的程序,就写的粗糙了,也没加异常处理。

主要的Python脚本文件LineCount.py的内容如下:

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

可以看到extension == '.txt' or extension == '.hol'这句是判断文件的后缀,来确定是否要计算代码行数。if eachLine.find('--') == 0 :这句来判断当前行是不是单行注释(我们的这门语言不支持块注释)以上就是对Python脚本文件LineCount.py的相关代码的介绍。为了能在其他机器上运行,使用了py2exe来把python脚本生成可执行的exe,setup.py脚本内容如下:

  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"],  
  8. )   

 

不过生成exe后程序臃肿很多,有3M多。感觉使用python确实是件很惬意的事。 以上的文章就是对python写的代码行数统计程序的相关内容的介绍。

【编辑推荐】

  1. 对Python源代码组织的相关实际操作步骤解析
  2. Python安装的步骤操作其实是件很容易的事
  3. 对Python源代码组织的相关实际操作步骤解析
  4. Python编程语言在Zope软件开发中具体应用
  5. Python嵌入C/C++在释放资源中的实际操作步骤详解
责任编辑:佚名 来源: 互联网
相关推荐

2010-04-08 09:27:44

Oracle 安装脚本

2010-02-01 16:32:49

Python脚本

2010-03-25 13:19:57

Python_ast.

2010-03-19 14:44:30

Python模块级函数

2009-12-03 10:06:33

Ubuntushell脚本

2009-06-12 18:30:12

Groovy 静态ma

2010-03-26 15:28:05

Python编写

2010-03-29 17:37:17

Nginx resin

2010-03-25 10:13:03

Python代码

2010-03-23 13:50:24

python教程

2010-01-15 16:21:45

VB.NET读写文本文

2010-03-25 12:50:45

Python代码

2010-03-26 16:17:24

Python嵌入

2010-03-26 16:35:29

Python open

2010-03-23 08:56:38

Python随机数模块

2010-03-11 16:50:27

Python应用

2021-04-24 23:00:43

Windows 10Windows微软

2009-02-01 10:29:04

Oracle数据库管理

2010-03-19 16:51:53

Java Socket

2010-03-17 19:24:38

Java多线程循环
点赞
收藏

51CTO技术栈公众号