做一个代码统计工具:

要求:
  1.如果是文件,就直接统计文件行数,并打印结果
  2.判断是否是目录,是就遍历统计目录下所有的文件
文件统计规则:
  1.开头是#_*_或者#encoding的需要算作代码统计
  2.注释#,'''或者"""判断某一行开头是"""或者'''就不计
  3.空行不统计
  4.统计当前文件行数,还有累计行数

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

要求算法:

  1.判断是否是文件,且文件存在,则打开文件进行统计

  2.判断是目录,则变量目录下所有的文件,进行统计

统计规则算法中,注释种类有如下几种:

a.以#开头进行单行注释

#XXXXX

b.以三个双引号"""开头和结尾的单行注释

"""XXXXX"""
'''XXXXX'''

c.以三个双引号或者单引号开头换行进行注释格式如下:

"""
XXXXX
"""
'''
XXX
'''

d.以三个双引号或者单引号开头不换行进行注释格式如下:

"""XXXXX
"""
'''XXXXX
'''

e.以三个双引号或者单引号结尾不换行进行注释格式如下:

"""
XXXXX"""
'''
XXXXX'''

根据以上多行注释情况,考虑设置标志位,当标志位是True的时候统计代码,当标志位是False的时候不统计,注意考虑标志位还原的情况编写统计规则如下:

def coding_rule(filename):
    count = 0
    import string
    with open(filename,'r',encoding ='utf-8') as fp:
        flag = True
        for line in fp:
            #print(line)
            if line.strip().startswith('#encoding') or line.strip().startswith('#_*_'):
                count+=1
                #统计开头是#_*_或者#encoding的代码
            elif not line.strip():
                continue
                #如果是空行,则不计
            elif line.strip().startswith('#'):
                #如果是单行注释格式如#xxx,则不统计,匹配a的情况
                continue
            elif line.strip().startswith('"""') and line.strip().endswith('"""') and len(line.strip())>3:
                #如果是单行注释格式如"""xxx""",则不统计,匹配b的情况
                continue
            elif line.strip().startswith("'''") and line.strip().endswith("'''") and len(line.strip())>3:
                continue
                #如果是单行注释格式如'''xxx''',则不统计,匹配b的情况
            elif line.strip().startswith("'''") and len(line.strip())==3 and flag ==True:
                #如果是以三个单引号'''开始进行注释的,设置标志位为False,匹配c的情况
                flag =False
                continue
            elif line.strip().startswith('"""') and len(line.strip())==3 and flag ==True:
                #如果是以三个双引号"""开始进行注释的,设置标志位为False,匹配c的情况
                flag =False
                continue
            elif line.strip().startswith('"""') and not line.strip().endswith('"""'):
                #如果是三个双引号开头不换行进行注释,设置标志位为False,匹配d的情况
                flag =False
                continue
            elif line.strip().startswith("'''") and not line.strip().endswith("'''"):
                #如果是三个单引号引号开头不换行进行注释,设置标志位为False,匹配d的情况
                flag =False
                continue
            elif not line.strip().startswith('"""') and line.strip().endswith('"""'):
                #如果以三个双引号结尾不换行进行注释,还原标志位为True,匹配e的情况
                flag =True
            elif not line.strip().startswith("'''") and line.strip().endswith("'''"):
                #如果以三个单引号结尾不换行进行注释,还原标志位为True,匹配e的情况
                flag =True
            elif line.strip().startswith("'''") and len(line.strip())==3 and flag ==False:
                #如果是三个单引号引号开头不换行进行注释,还原标志位为True,匹配d的情况
                flag =False
                flag =True
            elif line.strip().startswith('"""') and len(line.strip())==3 and flag ==False:
                #如果是三个单引号引号开头不换行进行注释,还原标志位为True,匹配d的情况
                flag =False
                flag =True
            elif flag ==True:
                count+=1
            #print(flag)
            #print(count)
    return count

另外写一个函数,来写代码统计的要求部分,如下:

def coding_count(*source_dir):
    import os
    result ={}
    total_count =0
    except_file=[]
    for source in source_dir:
        if os.path.exists(source) and (source[-2:]=='py' or source[-3:]=='txt'):
            #如果文件存在,且是.py文件
            result[source]=coding_rule(source)
            total_count +=coding_rule(source)
        #读文件
        elif os.path.exists(source) and os.path.isdir(source):
            #如果输入的dir是一个目录,且存在
            for root,dirs,files in os.walk(source):
                #print('当前目录:%s'%root)
                for file in files:
                    file_dir = os.path.join(root,file)
                    if os.path.basename(file)[-2:]=='py' or os.path.basename(file)[-3:]=='txt':
                        try:
                            result[file]=coding_rule(file_dir)
                            total_count +=coding_rule(file_dir)
                        except:
                            except_file.append(file)
        result['total']=total_count
    return result,except_file
#print(coding_count(r'C:\Users\何发奋\Desktop\Python 习题\a.txt',r'C:\Users\何发奋\Desktop\Python 习题\习题库'))
print(coding_count(r'C:\Users\何发奋\Desktop\Python 习题\a.txt'))

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄