使用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 ...
随机推荐
- Linux下安装vmtools的语句
sudo apt-get upgrade sudo apt-get install open-vm-tools-desktop -y sudo reboot 出现提示信息不确定就默认就好,一路y或ye ...
- JavaScript第一回-来龙去脉
简写:JavaScript-JS ECMAScript-ES 写在前面的话:啃文字大多时间不是件愉快的事情,但是我们必须过这个坎,让自己习惯,让自己不讨厌,至于喜欢不喜欢,我们等时间给出答案. J ...
- 网络协议 13 - HTTPS 协议:加密路上无尽头
系列文章传送门: 网络协议 1 - 概述 网络协议 2 - IP 是怎么来,又是怎么没的? 网络协议 3 - 从物理层到 MAC 层 网络协议 4 - 交换机与 VLAN:办公室太复杂,我要回学校 网 ...
- springboot~Mongodb的集成与使用
说说springboot与大叔lind.ddd的渊源 Mongodb在Lind.DDD中被二次封装过(大叔的.net和.net core),将它当成是一种仓储来使用,对于开发人员来说只公开curd几个 ...
- KnockOut绑定之Click绑定
example(click绑定) Click绑定对DOM元素添加一个函数,当DOM元素被点击的时候调用.在button,input 或者a标签中常用,但其实他适用于任何可见的DOM元素. 每当你点击b ...
- docker-compose使用备忘(转)
Docker-Compose简介 Docker-Compose项目是Docker官方的开源项目,负责实现对Docker容器集群的快速编排. Docker-Compose将所管理的容器分为三层,分别是工 ...
- 使用Atlas进行元数据管理之容错和高可用
1. 介绍 Apache Atlas使用各种系统并与之交互,为数据管理员提供元数据管理和数据血缘信息.通过适当地选择和配置这些依赖关系,可以使用Atlas实现高度的服务可用性.本文档介绍了Atlas中 ...
- HTTP协议及其相关
URI.URL.URN URL,统一资源定位符,用来标识某一处资源的地址,必须包含协议和域名,协议指的是HTTP.FTP.WS.file等协议,域名就是常说的网址,比如www.baidu.com ,其 ...
- sql字符串分割扩展方法
可编程性—表值函数 SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE FUNCTION [dbo].[Split] ( @RowData ...
- Javascript中双等号(==)隐性转换机制
在Javascript中判断相等关系有双等号(==)和三等号(===)两种.其中双等号(==)是值相等,而三等号(===)是严格相等(值及类型是否完全相等). 因此有几个常识知识: 1.对于strin ...