Python平台:写的源代码统计工具
没有什么技术含量,只是加深下Python对于文件的操作,还有正则的运用。现在只写了Python语法的,其他语法的也基本类似,以后还可以做个界面神马的,不过最近有其他事忙,这个就放下了。

代码清单:
|
''' Created on 2013-1-8 @author: Oneday ''' import os import re class pyCounter(): def __init__(self): self.__isInDCommand = False self.__isInSCommand = False def __is_pyfile(self, filename): return filename.endswith('.py') def __is_multi_commandline(self, line): line = line.strip() command_result = re.findall("'''|\"\"\"", line) if 0==len(command_result): return self.__isInDCommand or self.__isInSCommand for result in command_result: if '"""' == result: self.__isInDCommand = not self.__isInDCommand else: self.__isInSCommand = not self.__isInSCommand return True def __is_single_commandline(self, line): if re.search(r'#', line): return True else: return False def __is_emptyline(self, line): if re.match(r'^\s*$', line): return True else: return False def getlinenum(self, rootdir): line_num = source_num = command_num = empty_num = 0 for parent, _, filenames in os.walk(rootdir): for filename in filenames: if self.__is_pyfile(filename): with open(parent + os.sep + filename, 'r') as fp: for line in fp.readlines(): if self.__is_multi_commandline(line): command_num += 1 elif self.__is_single_commandline(line): command_num += 1 elif self.__is_emptyline(line): empty_num += 1 else: source_num += 1 line_num += 1 return line_num, source_num, command_num, empty_num def main(): while True: rootdir = input('please input source folder, type "q" to quit->').strip() if rootdir == 'q': break if os.path.isdir(rootdir): line_num, source_num, command_num, empty_num = pyCounter().getlinenum(rootdir) print('line_num->%d'%line_num) print('source_num->%d'%source_num) print('command_num->%d'%command_num) print('empty_num->%d'%empty_num) else: raise OSError('need folder,but passed file') if __name__ == '__main__': main() |
本文来源 我爱IT技术网 http://www.52ij.com/jishu/1066.html 转载请保留链接。
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
