Chinaunix首页 | 论坛 | 博客
  • 博客访问: 29420036
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: Python/Ruby

2010-04-20 15:23:10

#-*-coding:utf-8-*-
"""
统计一个PY代码里面代码的行数,注释的行数还有空行个数
版本:2010-04-20 号
心动不如行动。有想法就要去实践!下一步:将其稍做改善统计一个文件夹下面的全部py代码总数。评估一下每天写了多少代码量
author:hkebao@126.com
"
""
import os,sys
def trims(docString):
    if not docString:
        return ''
    lines = docString.expandtabs().splitlines()
    indent = sys.maxint
    for line in lines[0:]:
        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(filename):
    (filepath,tmp) = os.path.split(filename)
    (shortname,extension) = os.path.splitext(tmp)
    if extension == '.py':
        file = open(filename,'r')
        allLines = file.readlines()
        file.close()
        
        lineCount = 0
        commentCount = 0
        blankCount = 0
        codeCount = 0
        commentstart = False
        i = 0
        for eachline in allLines:
            if eachline != " ":
                eachline = eachline.replace(" ","")
                eachline = trims(eachline)
                if eachline.find('#') == 0:
                    commentCount += 1
                elif commentstart:
                    i += 1;
                    commentCount += 1
                    if eachline.count('"""') > 0 and i > 1:
                        commentstart = False;i = 0
                elif eachline[0:3] == '"""' :
                    commentCount += 1
                    if eachline.count('"""') > 1:pass
                    else:commentstart = True;i=1
                else:
                    if eachline == "":
                        blankCount += 1
                    else:
                        codeCount += 1
            lineCount = lineCount+1;
        
        print filename;
        print 'total: ',lineCount
        print 'comment: ',commentCount
        print 'blank: ' , blankCount
        print 'source: ',codeCount
FileLineCount('com.py')






阅读(1123) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~