玩蛇网提供最新Python编程技术信息以及Python资源下载!

Python统计代码行数的快捷方法

python 培训

想知道一段Python教程代码有多少行,除了看标注的行号外还可以用Python统计代码行数的快捷方法。需要用到python sys模块python os模块,这两个模块可以说是万能的最常被使用到的Python模块。

这个python源代码统计工具还不是十分的完善。本人希望能做的是一款能统计所有类型源代码行数的工具,可支支持当下所有的格式,能做到统计准确速度快最好了。希望有能力的朋友们帮我完善,做成能为所有不同计算机编程语言的程序员服务的工具。

Python统计代码行数的快捷方法源码如下:

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 != " " :
                    eachLine = 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;
                lineCount = 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);

#www.iplaypython.com

    def __init__(self):
        self.all = 0;
        self.allComment =0;
        self.allBlank = 0;
        self.allSource = 0;
        self.sourceFileCount = 0;
        filename = raw_input('Enter file name: ');
        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();

玩蛇网原创,转载请注明文章出处和来源网址:http://www.iplaypython.com/code/base/b2642.html



微信公众号搜索"玩蛇网Python之家"加关注,每日最新的Python资讯、图文视频教程可以让你一手全掌握。强烈推荐关注!

微信扫描下图可直接关注

玩蛇网Python新手QQ群,欢迎加入: ① 240764603 玩蛇网Python新手群
文章发布日期:2016-01-15 14:47 玩蛇网 www.iplaypython.com

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
相关文章推荐
别人正在看
特别推荐
去顶部去底部