一、理论学习
1.《软件工程》慕课
《软件工程》第三章慕课的学习记录。
2.《构建之法》-“两人合作”
合作的最小单位就是两个人,我们编写的代码不仅给自己看,也要给别人看。如何让其他人快速了解代码中体现的思路,需要规范的代码,代码的规范包括:代码风格规范和代码设计规范。代码风格的原则是需要简明、易懂、无二义性。代码风格规范包括缩进,行宽,括号,命名,下划线等方面。代码的设计规范。
结对编程在慕课视频中也有一定的介绍,并进行了实际演示,两个人交替扮演领航员和驾驶员的角色,提高编程的效率。
3.测试驱动开发
由于对软件开发了解不太多,这还是一个新名词。测试驱动开发,简称TDD,不同于传统软件开发流程的新型的开发方法。它要求在编写某个功能的代码之前先编写测试代码,然后只编写使测试通过的功能代码,通过测试来推动整个开发的进行。
开发的基本过程如下:
① 快速新增一个测试
② 运行所有的测试,发现新增的测试不能通过
③ 做一些小小的改动,尽快地让测试程序可运行,为此可以在程序中使用一些不合情理的方法
④ 运行所有测试并全部通过
⑤ 重构代码,优化结构。
感觉测试驱动开发像是机械制图。在绘图之前先确定好零件图的基准线,从基准出发,绘制图纸。而直接编写代码则像是直接开始画零件图,不容易控制零件图在图纸中的位置,不美观,甚至有可能走偏。
二、实践学习
生命游戏单元测试见
from itertools import chain from itertools import cycle from unittest import TestCase from unittest import mock from game_map import GameMap class TestGameMap(TestCase): def setUp(self): self.game_map = GameMap(4,3) def test_init(self): self.assertRaises(TypeError, GameMap, ('a', 3)) self.assertRaises(TypeError, GameMap, (4, 'b')) def test_rows(self): self.assertEqual(4,self.game_map.rows,"Should get correct rows") def test_cols(self): self.assertEqual(3, self.game_map.cols, "Should get correct rows") @mock.patch('random.random',new=mock.Mock(side_effect=chain(cycle([0.3, 0.6, 0.9])))) def test_reset(self): self.game_map.reset() for i in range(0, 4): self.assertEqual(1, self.game_map.get(i, 0)) for j in range(1, 3): self.assertEqual(0, self.game_map.get(i, j)) self.assertRaises(TypeError, self.game_map.reset, possibility='ab') def test_get_set(self): self.assertEqual(0, self.game_map.get(0,0),"Cells init to 0") self.game_map.set(0, 0, 1) self.assertEqual(1, self.game_map.get(0, 0), "Should get value set by set") self.assertRaises(TypeError, self.game_map.get, ("d3d3f", 0)) self.assertRaises(TypeError, self.game_map.get, (0, 'b')) self.assertRaises(TypeError, self.game_map.set, ('a', 0, 1)) self.assertRaises(TypeError, self.game_map.set, (0, 'b', 1)) self.assertRaises(TypeError, self.game_map.set, (0, 0, 'c')) def test_get_neighbor_count(self): expected_value = [[8]*3]*4 self.game_map.cells = [[1]*3]*4 for i in range(0,4): for j in range(0,3): self.assertEqual(expected_value[i][j],self.game_map.get_neighbor_count(i, j), '(%d %d)' % (i, j)) self.assertRaises(TypeError, self.game_map.get_neighbor_count, ('a', 0)) self.assertRaises(TypeError, self.game_map.get_neighbor_count, (0, 'b')) @mock.patch('game_map.GameMap.get_neighbor_count', new=mock.Mock(return_value=8)) # game_map.GameMap.get_neighbor_count def test_get_neighbor_count_map(self): expected_value = [[8] * 3] * 4 self.assertEqual(expected_value, self.game_map.get_neighbor_count_map()) def test_set_map(self): self.assertRaises(TypeError,self.game_map.set_map,{(0,0):1}) self.assertRaises(AssertionError,self.game_map.set_map,[[1]*3]*3) self.assertRaises(TypeError,self.game_map.set_map,[['1']*3]*4) self.assertRaises(AssertionError,self.game_map.set_map,[[2]*3]*4) self.game_map.set_map([[1]*3]*4) self.assertEqual([[1]*3]*4,self.game_map.cells) def test_print_map(self): self.game_map.cells = [ [0, 1, 1], [0, 0, 1], [1, 1, 1], [0, 0, 0] ] with mock.patch('builtins.print') as mock1: self.game_map.print_map() mock1.assert_has_calls([ mock.call('0 1 1'), mock.call('0 0 1'), mock.call('1 1 1'), mock.call('0 0 0'), ])
对单词搜索程序的补充如下需要进一步改进和性能测试:Github仓库 https://github.com/YuxuanCao666/localgithub/blob/master/literature_query.py
import os import re f_d = [] f_q = [] with open("document.txt","r") as sent_list: f_d.append(sent_list.read()) with open("query.txt","r") as query_list: f_q.append(query_list.read()) print(f_d_l = [str(f_d).lower()])#忽略大小写 f_q_l = [str(f_q).lower()] f_d_l_s = [str(f_d_l).split("." or "!" or "?")] # 将查询的文献进行分割 f_q_l_s = [str(f_q_l).split(" ") ]# 将查询的单词进行分割 line_num = 1 #记录文献的行数 word_num = 1 #记录查询的单词数量 #先查询单词出现的行号,再提取出每一行。 new_list = [] #将单词出现的行组成新的列表 for word_num in range(1,len(f_q_l_s)): for line_num in range(1,len(f_d_l_s)): if f_q_l_s[word_num] in f_d_l_s[line_num]: new_list.append(f_d_l_s[line_num]) line_num += 1 word_num += 1 else: print("None") for word_num in range(len(new_list)): pos = 1 # 记录单词在句子中的具体位置 if f_q_l_s[word_num] in new_list: pos = new_list.index(f_q_l_s[word_num]) new_list[pos] = '$' pos += 1 print("{}/{}".format(line_num,pos))