zoukankan      html  css  js  c++  java
  • 使用python对py文件程序代码复用度检查

    #!/user/bin/env python
    # @Time     :2018/6/5 14:58
    # @Author   :PGIDYSQ
    #@File      :PyCheck.py
    from os.path import isfile as isfile
    from time import time as time
    
    Result ={}
    AllLines =[]
    FileName = r'C:UsersPGIDYSQDesktopfibo.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 False
    def IsInSpan(Index2):
        for item in Result.values():
            for i in item:
                if i[0] <=Index2<i[0]+i[1]:
                    return True
        return False
    def 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 += 1
    def Output():
        print('-'*20)
        print('Result:')
        for key,value in Result.items():
            print('The original line is :
    {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)
  • 相关阅读:
    css知识点
    javascript 中闭包
    javascript 继承方法总结
    css滚动滚轮事件
    关于闭包的总结
    xpth xslt
    好的js函数
    自动化测试实施(4)
    自动化测试实施(5)
    自动化测试实施(3)
  • 原文地址:https://www.cnblogs.com/ysq0908/p/9157390.html
Copyright © 2011-2022 走看看