• 代码行数统计


    最近工作不忙,写了个脚本统计代码行数.记录于此方便以后查阅.python版本2.7

     1 # -*- coding: cp936 -*-
     2 import os
     3 
     4 totalLineCounts = 0
     5 fileCounts = 0
     6 
     7 def GetLines(fullFilename):
     8     lineNum = 0
     9     endCommentLine = 0
    10     startCommentLine = 0
    11     totalBlankLineNum = 0
    12     totalCommentLineNum = 0
    13     efficientCodesLineNum = 0
    14 
    15     InCommentArea = 0  #防止重复统计.比如/* */块内包含有//开头的行
    16     
    17     for line in open(fullFilename).readlines():
    18         line = line.strip()   #去除首尾的空格
    19         lineNum += 1
    20 
    21         if InCommentArea:           
    22             if line[-2:] == '*/':
    23                 InCommentArea = 0   #出注释区
    24                 endCommentLine = lineNum
    25                 #print ('注释区:%d行-%d行' % (startCommentLine,endCommentLine))
    26                 totalCommentLineNum += (endCommentLine - startCommentLine + 1) 
    27         else:                
    28             if line == '':          #空行
    29                 totalBlankLineNum += 1
    30             elif line == '//':      #以//开头的注释
    31                 totalCommentLineNum += 1
    32             elif line[0:2] == '/*':
    33                 InCommentArea = 1   #进入注释区
    34                 startCommentLine = lineNum
    35             else:
    36                 efficientCodesLineNum += 1       
    37     #print ('空白行数量:%d,注释行数量:%d,有效代码行数量:%d,总行数:%d' % (totalBlankLineNum,totalCommentLineNum,efficientCodesLineNum,lineNum))
    38     return [totalBlankLineNum,totalCommentLineNum,efficientCodesLineNum,lineNum]
    39 
    40 
    41 exts = ['cpp','h','txt']
    42 #key:ext value:[totalBlankLineNum,totalCommentLineNum,efficientCodesLineNum,lineNum]
    43 statistic = {'cpp':[0,0,0,0],'h':[0,0,0,0],'txt':[0,0,0,0]}
    44 
    45 codesDir = 'D:\SvnCodes\main_branch\code\billing'
    46 #codesDir = 'D:TestSub1sub1.1sub1.1.1' 
    47 if __name__ == '__main__':
    48     for curRoot,curSubDirs,curFiles in os.walk(codesDir):
    49         #print curRoot,curSubDirs,curFiles
    50         for file in curFiles:
    51             fileExt = file.split('.')[-1]
    52             if fileExt in exts:
    53                 fullFilename = curRoot + os.path.sep + file
    54                 r = GetLines(fullFilename)
    55                 statistic[fileExt][0] += r[0]
    56                 statistic[fileExt][1] += r[1]
    57                 statistic[fileExt][2] += r[2]
    58                 statistic[fileExt][3] += r[3]
    59         
    60     print '{ext:[blank,comment,efficient,total]}'
    61     print statistic
  • 相关阅读:
    真香!PySpark整合Apache Hudi实战
    Apache Hudi又双叕被国内顶级云服务提供商集成了!
    Apache Hudi集成Apache Zeppelin实战
    实战 | 将Apache Hudi数据集写入阿里云OSS
    实战|使用Spark Structured Streaming写入Hudi
    Apache Hudi 设计与架构最强解读
    【Flink】Flink作业调度流程分析
    【Flink】深入理解Flink-On-Yarn模式
    【Flink】Flink 底层RPC框架分析
    【MyBatis】MyBatis自动生成代码之查询爬坑记
  • 原文地址:https://www.cnblogs.com/sdu20112013/p/4882725.html
Copyright © 2020-2023  润新知