Python——极限编程

很cool的名字,极限编程(Extreme Programming,简写XP)是编程的一种流行趋势:
(1)首先是对目标进行计划;
(2)然后将测试用例集合编写为一种框架;
(3)之后才编写实际的代码。
在完成实际代码后的任何时候,都可以运行测试用例以查看接近设计目标的程度,该测试套件具体体现了这个设计目标,同时也在调式和测试软件,非常的nice。


下面是一个实际的例子,完成一个和Unix中find和grep结合的工具
可以指定 文件名 文件内容 扩展名 搜索目录 进行搜索,返回合适的文件信息

第一步是需要分析,对结果做初步规划:
(1)返回形式:(path , filename , 扩展名 , 文件大小)的元组;
(2)输入参数:搜索文件名的regex 和 内容地regex;
(3)其它搜索项:文件大小,存在时间,最近一次修改信息等(介个以后再说)

第二步是极限编程的特色,按照设计好的接口,完成测试代码:
(这里是个作弊的行为,因为实践中不可能一次性的将测试代码全部完成)
1 #!/usr/bin/python
2 # -- coding: utf-8 --
3 #============================================
4 #FileName: test_find.py
5 #Date: 2013年 06月 04日 星期二 09:29:20 CST
6 #Author: chenhuan
7 #Usage: unittest test file of find.py
8 #============================================
9
10 import unittest
11 import find
12 import os , os.path
13 import shutil
14
15 #============================================================
16 class TestFind(unittest.TestCase) :
17 """
18 TestFind - test the find module. It has five parts to test
19 1. testSearchAll - search the .*
20 2. testSearchFile - search as the input filename
21 3. testSearchContent - search as the content
22 4. testSearchExtension - search as the extension name
23 5. testSearchLogic - search as the logic
24
25 find(fileName , content , start)
26 return ([path , fileName , extension , fileSize]...)
27 """
28
29 #--------------------------------------------------
30 def __getFileName(self , fileInfo) :
31 """
32 __getFileName - return the filename
33 fileInfo - the result return by find
34 """
35 return fileInfo[1]
36
37
38 #--------------------------------------------------
39 def setUp(self) :
40 """
41 setUp - build up the test files as follows
42 1. _test/file_1.txt "chenhuan"
43 1. _test/file_2.py "wuxiaoqin"
44 2. _test/dir_1/file_1.cpp "#include <stdio.h>"
45 3. _test/dir_2/file_1.py "wuxiaoqin"
46 """
47 try :
48 os.mkdir('./_test')
49 os.mkdir('./_test/dir_1')
50 os.mkdir('./_test/dir_2')
51 except :
52 pass
53
54 f = open('./_test/file_1.txt','w')
55 f.write('chenhuan')
56 f.close()
57
58 f = open('./_test/file_2.py','w')
59 f.write('wuxiaoqin')
60 f.close()
61
62 f = open('./_test/dir_1/file_1.cpp','w')
63 f.write('#include<stdio.h>')
64 f.close()
65
66 f = open('./_test/dir_1/file_1.py','w')
67 f.write('wuxiaoqin')
68 f.close()
69
70 #-------------------------------------------------
71 def tearDown(self) :
72 """
73 tearDown - remove all the test files
74 """
75 shutil.rmtree('./_test')
76
77 #-------------------------------------------------
78 def testSearchAll(self) :
79 """
80 testSearchAll - find all the files under pointed directory
81 """
82 filesInfo = find.find(fileName=r"." , start="_test")
83 self.failUnless(map(self.__getFileName , filesInfo).sort() ==
84 ['file_1.txt','file_2.py','file_1.cpp','file_1.py'].sort(),
85 'Error in testSearchAll')
86
87 #----------------------------------------------------
88 def testSearchFile(self) :
89 """
90 testSearchFIle - find the pointed file
91 """
92 filesInfo = find.find(fileName=r"file_1.
" , start='_test')
93 self.failUnless(map(self.__getFileName , filesInfo).sort() ==
94 ['file_1.txt' , 'file_1.cpp' , 'file_1.py'].sort() ,
95 'Error in testSearchFile')
96
97
98 filesInfo = find.find(fileName=r"file_1.txt" , start='_test')
99 self.failUnless(map(self.__getFileName , filesInfo) ==
100 ['file_1.txt'] , 'Error in testSearchFile')
101
102 filesInfo = find.find(fileName=r"file_2.txt" , start='_test')
103 self.failUnless(map(self.__getFileName , filesInfo) ==
104 [] , 'Error in testSearchFile')
105
106 #----------------------------------------------------
107 def testSearchContent(self) :
108 """
109 testSearchContent - find the files which contains pointed content
110 """
111 filesInfo = find.find(content = 'wuxiaoqin' , start='_test')
112 self.failUnless(map(self.__getFileName , filesInfo).sort() ==
113 ['file_2.py' , 'file_1.py'].sort() ,
114 'Error in testSearchContent')
115
116 filesInfo = find.find(content = r'include' , start='_test')
117 self.failUnless(map(self.__getFileName , filesInfo) ==
118 ['file_1.cpp'] , 'Error in testSearchContent')
119
120 filesInfo = find.find(content = r'wu.*qin' , start='_test')
121 self.failUnless(map(self.__getFileName , filesInfo).sort() ==
122 ['file_1.py' , 'file_2.py'].sort() ,
123 'Error in testSearchContent')
124
125 #-------------------------------------------------------
126 def testSearchExtension(self) :
127 """
128 testSearchExtension - find the files by the extension name
129 """
130 filesInfo = find.find(extension='txt' ,start='_test')
131 self.failUnless(map(self.__getFileName , filesInfo).sort() ==
132 ['file_1.txt','file_2.txt'].sort() ,
133 'Error in testSearchExtension')
134
135 filesInfo = find.find(content='chenhuan' , extension='txt' ,
136 start='_test')
137 self.failUnless(map(self.__getFileName , filesInfo) ==
138 ['file_1.txt'] , 'Error in testSearchExtension')
139
140 #=====================================================
141 if name == 'main' :
142 unittest.main()


第三步开始才是真正的需要实现功能的代码,实际上这一块和测试代码一道,是一个缓慢发展的过程:
1 #!/usr/bin/python
2 # -- coding: utf-8 --
3 #============================================
4 #FileName: find.py
5 #Date: 2013年 06月 04日 星期二 09:29:55 CST
6 #Author: chenhuan
7 #Usage: find to search the file as dir regex
8 #and content regex
9 #============================================
10
11 import os
12 import os.path
13 import re
14 from stat import *
15
16 #---------------------------------------------------------
17 def find(fileName=r'.*' , content=None , extension=None , start='.') :
18 """
19 find - find the file named 'fileName' which contains the
20 'content' under the 'start' path, 'extension' means the suffix
21 """
22 context = {}
23 context['fileName'] = fileName
24 context['content'] = content
25 context['extension'] = extension
26 context['return'] = []
27
28 os.path.walk(start , findFile , context)
29
30 return context['return']
31
32 #----------------------------------------------------------
33 def findFile(context , dirName , files) :
34 """
35 findFile - return the fileInfo which match the context
36 """
37 for file in files :
38 path = os.path.join(dirName,file)
39 path = os.path.normcase(path)
40
41 fileInfo = os.stat(path)
42 size = fileInfo.st_size
43
44 #ignore directorys
45 if S_ISDIR(fileInfo.st_mode) :
46 continue
47
48 try :
49 extension = os.path.splitext(file)[1][1:]
50 except :
51 extension = ''
52
53 #do filtration based on the fileName
54 fileNameP = re.compile(context['fileName'])
55 if not fileNameP.search(file):
56 continue
57
58 #do filtration based on extension
59 if context['extension'] :
60 extensionP = re.compile(context['extension'])
61 if not extensionP.search(extension) :
62 continue
63
64 #do filtration based on the contents
65 if context['content'] :
66 f = open(path , 'r')
67 fileContentP = re.compile(context['content'])
68 matchFlag = False
69 for line in f.readlines() :
70 if fileContentP.search(line) :
71 matchFlag = True
72 break
73 f.close()
74 if not matchFlag :
75 continue
76
77 #Build the return value
78 fileReturn = (path , file , extension ,size)
79 context['return'].append(fileReturn)


结果直接运行test_find.py就可以了,调式了半天,当然是没有啥错误:
Python:极限编程

极限编程的优点是在开始真正的coding之前,需要有一个清晰的需求分析和接口设计,这是我的大短板。感觉从学习Python开始,越来越习惯于接受知识,而不是去思考自己的方案,有点像Matlab,过分的依赖于各种库。thinking~~~thinking~~~thinking才是王道,工具只是用来实现自己想法的,别舍本逐末~~~

posted @ 2015-03-03 14:26  落叶落叶  阅读(674)  评论(0编辑  收藏  举报