使用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:\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文件程序代码复用度检查的更多相关文章
- python将py文件转换为pyc
python -m py_compile lib/ylpy.py python -m py_compile lib/ylpy.py python 一个.py文件如何调用另一个.py文件中的类和函数 A ...
- Python(一) 快速配置Python编译环境与第一个py文件程序
1. Python基本语法在此不熬述. 2. 到管网下载Python 3.6.x 版本,与本机匹配的版本,如本机是 win7 64 python-3.6.5-amd64 3. 下载IDE:Python ...
- Python的.py文件打包成exe可执行文件
前几天做了几个简单的爬虫python程序,于是就想做个窗口看看效果. 首先是,窗口的话,以前没怎么接触过,就先考虑用Qt制作简单的ui.这里用前面sinanews的爬虫脚本为例,制作一个获取当天sin ...
- Python __init__.py 文件使用
__init__.py的主要作用是: 1. Python中package的标识,不能删除 2. 定义__all__用来模糊导入 3. 编写Python代码(不建议在__init__中写python模块 ...
- python的py文件打包成exe
一.首先需要安装Pyinstaller-- 使用pip来安装模块 (我电脑上装的是python的一个编译环境Anaconda,如果电脑上装的是python自带的IDE的话,就直接进入python的安装 ...
- Python __init__.py文件的作用
我们经常在python的模块目录中会看到 "__init__.py" 这个文件,那么它到底有什么作用呢? 1. 模块包(module package)标识 如果你是使用pytho ...
- python的py文件命名注意事项
最近,在学习python爬虫时,用到各种库特性时,写小段代码,命名demo的py文件诸如:requests.py,json.py,csv.py.都会提示类似“module 'csv' has no a ...
- 将Python的.py文件转变为可执行文件.exe
python是个很强大的工具,但我们生成的.py文件在没有Python环境下运行就需要转为.exe文件,我使用的是PyInstaller 1.安装PyInstaller: pip install Py ...
- uncompyle2反编译python的.py文件
前几天学用github,一不小心把a.py文件给删除了,由于1天没有提交,也无法找回.突然发现同a.py文件生成的编译文件a.pyc还在,逐去搜索一番反编译的方法. 查询得知python比较好的工具u ...
随机推荐
- 推荐系统(Recommendation system )介绍
前言 随着电子商务的发展,网络购物成为一种趋势,当你打开某个购物网站比如淘宝.京东的时候,会看到很多给你推荐的产品,你是否觉得这些推荐的产品都是你似曾相识或者正好需要的呢.这个就是现在电子商务里面的推 ...
- Android状态栏着色
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 状态栏着色,也就是我们经常听到的沉浸式状态栏,关于沉浸式的称呼网上也有很多吐槽的,这里就不做过多讨论了,以下我们统称状态栏着色,这样 ...
- js数组遍历(for in ,for of ,map,foreach,filter)的区别
一.for in 和for of 的区别 1.for in 遍历数组时,索引实际上是字符串类型的数字,不能进行运算,我们来输出一下: let arr = [1,3,5,4] for (let inde ...
- Asp.Net Core 轻松学-HttpClient的演进和避坑
前言 在 Asp.Net Core 1.0 时代,由于设计上的问题, HttpClient 给开发者带来了无尽的困扰,用 Asp.Net Core 开发团队的话来说就是:我们注意到,HttpC ...
- PHP类和函数注释大全
每次要用PHP的某个功能的时候,都要去查一下,于是决定将PHP所有类和函数都整理出来,加上注释 大致实现 将php.jar文件解压,取出目录stubs 将stubs中的所有php文件中的注释去掉,并做 ...
- 【网摘】C#.NET 在 MVC 中动态绑定下拉菜单的方法
1. 已知下拉菜单列表项: 在 Controller 控制器类中输入已下代码 1 public class DemoController : Controller 2 { 3 public ...
- 客户端浏览器- UWP兼容版本WebView
WebView简介 在win10之前,浏览器控件有WPF版本webBrowser.Winform版本WebBrowser,浏览器内核为IE. win10之后,微软不再维护原有的WebBrowser,转 ...
- Python二级-----------程序冲刺3
1. 根据输入字符串 s,输出一个宽度为 15 字符,字符串 s 居中显示,以“=”填充的格式.如果输入字符串超过 15 个字符,则输出字符串前 15 个字符.提示代码如下: ...
- 时空地图TimeGIS.com生成正交曲线网格
数值模拟中对数学物理方程的求解过程中经常需要生成网格,这里提供了一种方便的方法,只需要简单地勾画出区域的轮廓, 就可以生成相应的正交曲线网格,详情请访问 www.TimeGIS.com
- java 线程方法 ---- wait()
class MyThread5 implements Runnable{ private int flag = 10; @Override public void run() { while (fla ...