使用python对py文件程序代码复用度检查,,#!/user/bi


#!/user/bin/env python# @Time     :2018/6/5 14:58# @Author   :PGIDYSQ#@File      :PyCheck.pyfrom os.path import isfile as isfilefrom time import time as timeResult ={}AllLines =[]FileName = r‘C:\Users\PGIDYSQ\Desktop\fibo.py‘#访问.py文件路径‘‘‘py文件程序代码复用度检查‘‘‘def PreOperate():    global AllLines    with open(FileName,‘r‘,encoding=‘UTF-8‘) as fp:        for line in fp:            line =‘ ‘.join(line.split())            AllLines.append(line)def IfHasDuplicated(Index1):    for item in Result.values():        for it in item:            if Index1 == it[0]:                return it[1]    return Falsedef IsInSpan(Index2):    for item in Result.values():        for i in item:            if i[0] <=Index2<i[0]+i[1]:                return True    return Falsedef MainCheck():    global Result    TotalLen = len(AllLines)    Index1 =0    while Index1 < TotalLen -1:        span = IfHasDuplicated(Index1)        if span:            Index1 += span            continue        Index2 = Index1 + 1        while Index2 < TotalLen:            if IsInSpan(Index2):                Index2 += 1                continue            src = ‘‘            des = ‘‘            for i in range(10):                if Index2 + 1>=TotalLen:                    break                src += AllLines[Index1 + 1]                des += AllLines[Index2 + 1]                if src == des:                    t = Result.get(Index1,[])                    for tt in t:                        if tt[0] == Index2:                            tt[1] = i+1                            break                        else:                            t.append([Index2,i+1])                        Result[Index1] = t                else:                    break            t = Result.get(Index1, [])            for tt in t:                if tt[0] == Index2:                    Index2 += tt[1]                    break            else:                Index2 += 1        Result[Index1] = Result.get(Index1,[])        for n in Result[Index1][::-1]:            if n[1] < 3:                Result[Index1].remove(n)        if not Result[Index1]:            del Result[Index1]        a = [ttt[1] for ttt in Result.get(Index1,[[Index1,1]])]        if a:            Index1 += max(a)        else:            Index1 += 1def Output():    print(‘-‘*20)    print(‘Result:‘)    for key,value in Result.items():        print(‘The original line is :\n{0}‘.format(AllLines[key]))        print(‘Its line number is {0}‘.format(key+1))        print(‘The duplicated line numbers are:‘)        for i in value:            print(‘    Start:‘,i[0],‘     Span:‘,i[1])        print(‘-‘*20)    print(‘-‘*20)if isfile(FileName):    start =time()    PreOperate()    MainCheck()    Output()    print(‘Time used:‘,time() - start)

使用python对py文件程序代码复用度检查

评论关闭