博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用来删除菊花公司代码中大量无用的函数头部注释的python剃刀
阅读量:4514 次
发布时间:2019-06-08

本文共 3979 字,大约阅读时间需要 13 分钟。

删除前很多函数都有不少基本无用注释,整个注释中除了 函数说明这一项有用外,其他像函数名称,输入参数,输出参数,返回值等等,对于你阅读代码基本无用。完全可以删去,换个眼前干净 

/**************************************functionName: ddddddddTTTT:  ttttttttttttttt aaaaaaaaaaaa aaaaaaaaaaaa useful notes of the functionsinput:ddddddddddauthor:ddddddddddddddd************************************/int main(int argc, char const *argv[]){    /***********/    int d   = 0;    char a[] = {
'1','b','d'}; /***********/ return 0;}

删除之后,仅保留头部注释中 有用的 函数说明一项,看着舒服多了。

/**************************************TTTT:  ttttttttttttttt aaaaaaaaaaaa aaaaaaaaaaaa useful notes of the functions************************************/int main(int argc, char const *argv[]){    /***********/    int d   = 0;    char a[] = {
'1','b','d'}; /***********/ return 0;}

 

下面是实现这个小功能的python代码

1 ''' 2 function:  clean these nonsense function notes 3 author: fpj @ 2013.7.5 4 ''' 5  6 import os, re 7  8 ''' 9 walk all .c files in the [root] directory10 '''11 def GetAllFiles(root):12     for  rootpath, dirList, fileList in os.walk(root):13         print("display all files in: %s \r directory: %s \r file:%s \r" % (rootpath, dirList, fileList) )14         for file in fileList:15             if re.match(r'\w*.c', file):  16                 cCleaner(os.path.join(root,file))17          18         for directory in dirList:19             GetAllFiles(os.path.join(root,directory))20                 21 '''22 clean the nonsense function notes in [cleanedFile]23 '''24 def cCleaner(toCleanedFileName):25     if not os.path.exists(toCleanedFileName):26         print("ERROR! the file[%s] is not exist.\r" % toCleanedFileName)27         return28     29     tmpFileName = toCleanedFileName + "_tmp"30     cleanedFile = open(tmpFileName, 'a+', encoding='utf-8')31     32     file = open(toCleanedFileName, 'r+', encoding='utf-8')33     inNonsenseScope = False # judge if the line is in nonsense documents34     isUsefulNotes = False   # judge if the line is useful notes when the line is in nonsense documents35     for line in file:36         if re.match(r'^\s?\/\*{1,8}', line) and not(re.search(r'\*{1,}/', line)): # match the /**********, if there are any SPACE ahead, it will be matched.37             inNonsenseScope = True38             print("%s" % line, end='', file = cleanedFile)39             continue40         41         if re.search(r'\*{1,}/', line): # search the **********/, if there are any SPACE ahead, it will be matched.42             inNonsenseScope = False43             print("%s" % line, end='', file = cleanedFile)44             continue45         46         if inNonsenseScope:  # lines of Nonsense Function documents47             if re.search('^\s?TTTT', line):  # TTTT means these useful notes of functions48                 isUsefulNotes = True49                 print("%s" % line, end='', file = cleanedFile)50                 continue51 52             if re.search('^\s?NNNN', line):  # NNNN means these nonsense notse of functions53                 isUsefulNotes = False54                 continue55 56             if isUsefulNotes: # useful notes of function maybe more than one line57                 print("%s" % line, end='', file = cleanedFile)58                 continue59         60         if not inNonsenseScope: # lines of Not function documents61             print("%s" % line, end='', file = cleanedFile)62             63     # close the file and delete the old one then rename it64     file.close()65     cleanedFile.flush()66     cleanedFile.close()67     68     os.remove(toCleanedFileName)    69     os.renames(tmpFileName, toCleanedFileName)70     71 # # #     main script   # # #72 # input the directory these .c files included.73 root = input('Enter directory:')74 if not os.path.isdir(root):75     root = os.getcwd()76     print("Error! you can't give a valid file path;so we will start from current directory[%s]!\r" % root)77 78 # walk all .c files in the [root] directory79 GetAllFiles(root)

 

 

 
 

 

转载于:https://www.cnblogs.com/fanopi/p/3174739.html

你可能感兴趣的文章
Android 常用命令集的使用
查看>>
Android ADT,SDK的安装,让人烦恼的在线方式!
查看>>
mac显示隐藏文件
查看>>
RobotFramework下的http接口自动化Follow Response关键字的使用
查看>>
this&super两个关键字的意义和用法
查看>>
Spring基础内容一
查看>>
鹅厂欧阳大神给年轻人的一些分享
查看>>
RocketMQ服务搭建_1
查看>>
CentOS7 安装 Docker
查看>>
Redis常见问题
查看>>
Android自带样式
查看>>
iSCSI 原理和基础使用
查看>>
Gym101350 J Lazy Physics Cat
查看>>
Java读取文件方法大全
查看>>
解决mysql无法显示中文/MySQL中文乱码问号等问题
查看>>
CentOS 7.2 配置mysql5.7
查看>>
第一次写博客用来记录自己的工程师生涯。
查看>>
python输出转义字符
查看>>
java基础43 IO流技术(输入字节流/缓冲输入字节流)
查看>>
ASP.NET那点不为人知的事(四)
查看>>