结对编程

合作者学号:201631062206,201631062231

代码地址:https://gitee.com/ronanly/wordcountExtend/tree/master

作业地址:https://edu.cnblogs.com/campus/xnsy/2018softwaretest2398/homework/2187

1.代码互审:我们用python语言,两个人分工,最后我们整合了两个人的代码,基本实现了多批量处理目录以及子目录下所有的符合条件的文件,输出他们的代码行/空行/注释行信息,以及单词的剔除。

2.模块分工:我负责代码运行和测试,她主要负责代码的运行部分。

3.在运行过程中,有报错

后经修改,发现是写代码途中,大意忘装wxpython这个第三方库了。解决方案如下:

这问题后发现代码中存在非法字符,认真检查后已无碍。

4.实现扩展功能

(1)在实现原来基础功能的代码上增加了扩展功能:

wc.exe -s            //递归处理目录下符合条件的文件

wc.exe -a file.c     //返回更复杂的数据(代码行 / 空行 / 注释行)

wc.exe -e stopList.txt  // 停用词表,统计文件单词总数时,不统计该表中的单词

[file_name]: 文件或目录名,可以处理一般通配符。

 

 

(2)代码中分为3个模块:FileInfo.py (基本扩展功能模块), DirInfo.py (创建列表模块) main.py(主函数模块)

 

 

 (3)扩展功能(代码行,注释行,空行)代码:

 

 1  def line_detail(self):

 2         # open the file with the name 'filename'

 3         f = open(self.filename, 'r', encoding='utf-8')

 4         # get all line string into a list

 5         lines = f.readlines()

 6         f.close()

 7         # distinguish different lines

 8         codelines, emptylines, commentlines = [], [], []

 9         for line in lines:

10             tmpline = line.replace(' ', '')

11             tmpline = tmpline.replace('\t', '')

12             tmpline = tmpline.replace('\n', '')

13             if len(tmpline) == 1 or len(tmpline) == 0:

14                 emptylines.append(line)

15             elif tmpline.startswith('//'):

16                 commentlines.append(line)

17             else:

18                 codelines.append(line)

19         return self.fname + ', 代码行/空行/注释行:' + str(len(codelines)) + '/'\

20                 + str(len(emptylines)) + '/' + str(len(commentlines))

 

(4)递归处理文件代码:

 

 1 # determine the type needed

 2 tmplist = desfile.split('.')

 3 type = '.' + tmplist[-1]

 4 # get the required list

 5 localinfo = directory.build_infolist(type)

 6 # build up the information list from the file list

 7 def build_infolist(type=''):

 8     filenames = build_filelist(type)

 9     dirnames = build_dirlist()

10     # get information by creating a FileInfo object

11     infos = []

12     for fname in filenames:

13         info = FileInfo(path, fname)

14         infos.append(info)

15     # deal with the inner directory

16     for dirname in dirnames:

17         new_path = join(path, dirname)

18         newdir = DirInfo(new_path)

19         new_infos = newdir.build_infolist(type)

20         infos = infos + new_infos

21     return infos

 

 

(4)主函数模块:

 主函数模块

 

 5.静态代码检查

 1.在这用的静态代码工具是:pyflakes

   安装后(pip install pyflakes)对代码进行测试如下:

 

经对代码仔细检查后,发现是变量thisstr写错

其他错误:

 

 

多次修改后检查三个模块:

 

静态代码检查正确。

6.性能测试和优化

性能优化工具:cProfile

cProfile:基于lsprof的用C语言实现的扩展应用,运行开销比较合理,适合分析运行时间较长的程序,推荐使用这个模块; 

使用cProfile分析的结果可以输出到指定的文件中,但是文件内容是以二进制的方式保存的,用文本编辑器打开时乱码。所以,Python提供了一个pstats模块,用来分析cProfile输出的文件内容。

 如下图所示:

 

 

 7.参考文献

python.py文件打包为exe文件:https://www.jb51.net/article/140067.htm

python性能优化cProfile:https://blog.csdn.net/asukasmallriver/article/details/74356771

python静态代码测试:https://blog.csdn.net/fan_hai_ping/article/details/41733817

8.总结

  第一次经历这个结对编程,感觉马马虎虎吧,两个人思想会多一点,考虑问题比较全面,在这方面,我的队友小罗洋还是帮了我挺多的,彼此还是出现了一些分歧,但经历过讨论,还是解决了,结对方式还是比较好的,在这个过程,我还是学到了比较多东西。

 

 

 

 

posted @ 2018-10-21 18:40  胡帅帅  阅读(119)  评论(0编辑  收藏  举报