码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-06-08 21:57:20      阅读:307      评论:0      收藏:0      [点我收藏+]

标签:IV   sed   number   orm   pen   append   main   time()   get   

#!/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 = rC:\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 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 :\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文件程序代码复用度检查

标签:IV   sed   number   orm   pen   append   main   time()   get   

原文地址:https://www.cnblogs.com/ysq0908/p/9157390.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!