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)
  • 相关阅读:
    有点忙啊
    什么是协程
    HDU 1110 Equipment Box (判断一个大矩形里面能不能放小矩形)
    HDU 1155 Bungee Jumping(物理题,动能公式,弹性势能公式,重力势能公式)
    HDU 1210 Eddy's 洗牌问题(找规律,数学)
    HDU1214 圆桌会议(找规律,数学)
    HDU1215 七夕节(模拟 数学)
    HDU 1216 Assistance Required(暴力打表)
    HDU 1220 Cube(数学,找规律)
    HDU 1221 Rectangle and Circle(判断圆和矩形是不是相交)
  • 原文地址:https://www.cnblogs.com/ysq0908/p/9157390.html
Copyright © 2011-2022 走看看