#!/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:\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文件程序代码复用度检查的更多相关文章

  1. python将py文件转换为pyc

    python -m py_compile lib/ylpy.py python -m py_compile lib/ylpy.py python 一个.py文件如何调用另一个.py文件中的类和函数 A ...

  2. Python(一) 快速配置Python编译环境与第一个py文件程序

    1. Python基本语法在此不熬述. 2. 到管网下载Python 3.6.x 版本,与本机匹配的版本,如本机是 win7 64 python-3.6.5-amd64 3. 下载IDE:Python ...

  3. Python的.py文件打包成exe可执行文件

    前几天做了几个简单的爬虫python程序,于是就想做个窗口看看效果. 首先是,窗口的话,以前没怎么接触过,就先考虑用Qt制作简单的ui.这里用前面sinanews的爬虫脚本为例,制作一个获取当天sin ...

  4. Python __init__.py 文件使用

    __init__.py的主要作用是: 1. Python中package的标识,不能删除 2. 定义__all__用来模糊导入 3. 编写Python代码(不建议在__init__中写python模块 ...

  5. python的py文件打包成exe

    一.首先需要安装Pyinstaller-- 使用pip来安装模块 (我电脑上装的是python的一个编译环境Anaconda,如果电脑上装的是python自带的IDE的话,就直接进入python的安装 ...

  6. Python __init__.py文件的作用

    我们经常在python的模块目录中会看到 "__init__.py"  这个文件,那么它到底有什么作用呢? 1. 模块包(module package)标识 如果你是使用pytho ...

  7. python的py文件命名注意事项

    最近,在学习python爬虫时,用到各种库特性时,写小段代码,命名demo的py文件诸如:requests.py,json.py,csv.py.都会提示类似“module 'csv' has no a ...

  8. 将Python的.py文件转变为可执行文件.exe

    python是个很强大的工具,但我们生成的.py文件在没有Python环境下运行就需要转为.exe文件,我使用的是PyInstaller 1.安装PyInstaller: pip install Py ...

  9. uncompyle2反编译python的.py文件

    前几天学用github,一不小心把a.py文件给删除了,由于1天没有提交,也无法找回.突然发现同a.py文件生成的编译文件a.pyc还在,逐去搜索一番反编译的方法. 查询得知python比较好的工具u ...

随机推荐

  1. IDEA设置热部署

    1.点击File找到Settings打开. 1.2:找到Compiler将 Build project automatically   勾选 2.使用快捷键 Ctrl+Shift+A    搜索 re ...

  2. 一句话,讲清楚java泛型的本质(非类型擦除)

    背景 昨天,在逛论坛时遇到个这么个问题,上代码: public class GenericTest { //方法一 public static <T extends Comparable< ...

  3. 微信小程序开发04-打造自己的UI库

    前言 github地址:https://github.com/yexiaochai/wxdemo 接上文继续,我们前面学习了小程序的生命周期.小程序的标签.小程序的样式,后面我们写了一个简单的load ...

  4. SmartSql Zookeeper分布式配置

    安装 SmartSql.ZooKeeperConfig Install-Package SmartSql.ZooKeeperConfig Demo string connStr = "192 ...

  5. Python编程从入门到实践笔记——用户输入和while循环

    Python编程从入门到实践笔记——用户输入和while循环 #coding=utf-8 #函数input()让程序暂停运行,等待用户输入一些文本.得到用户的输入以后将其存储在一个变量中,方便后续使用 ...

  6. 包、继承以及 LeetCode 27、28题

    1 package.import 和 import static 1.1 Package Java 引入了包(Package)机制,提供了类的多层命名空间,用于解决类的命名冲突.类文件管理问题.Jav ...

  7. PHP进程信号处理

    PHP进程信号处理 php有一组进程控制函数PCNTL,使得php能在*nix系统中实现跟c一样的创建子进程.使用exec函数执行程序.处理信号等功能. 注意:pcntl这个扩展仅在cli/cgi模式 ...

  8. 判断值是否为undefined

    可以使用 Ext.isDefined( value ) 这个函数, 也可以使用下面代码来进行实现: /** 判断传入的值是否 为undefined */ function isUndefined(va ...

  9. SAP HUM 事务代码HUMO为整托做Scrap

    SAP HUM 事务代码HUMO为整托做Scrap HUMO事务代码查询结果里,选择某个HU, 回车,过账成功, 2019-04-10 写于苏州市.  

  10. 生鲜配送管理系统_升鲜宝V2.0 价格组功能 操作说明_15382353715

    价格组功能是B端供应链系统,必不可少的一个功能,其主要实现不同的客户不同的价格,B端系统有一个最大的不同就是,有些商品后台下单人员能看到的.有些商品在销售的那一瞬间,还不知道价格.所以这些商品只有后台 ...