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才是王道,工具只是用来实现自己想法的,别舍本逐末~~~
Python——极限编程的更多相关文章
- Python演讲笔记1
参考: 1. The Clean Architecture in Python (Brandon Rhodes) 2. Python Best Practice Patterns (Vladimir ...
- [转] Python自动单元测试框架
一.软件测试 大型软件系统的开发是一个很复杂的过程,其中因为人的因素而所产生的错误非常多,因此软件在开发过程必须要有相应的质量保证活动,而软件测试则是保证质量的关键措施.正像软件熵(software ...
- PYTHON单元测试
PYTHON开发入门与实战11-单元测试 1. 单元测试 本章节我们来讲讲django工程中如何实现单元测试,单元测试如何编写以及在可持续项目中单元测试的重要性. 下面是单元测试的定义: 单元测试是开 ...
- Python单元测试框架
目录 概况 系统要求 使用PyUnit构建自己的测试 安装 测试用例介绍 创建一个简单测试用例 复用设置代码:创建固件 包含多个测试方法的测试用例类 将测试用例聚合成测试套件 嵌套测试用例 测试代码的 ...
- Python PEP-8编码风格指南中文版
#PEP 8 – Python编码风格指南 PEP: 8 Title: Style Guide for Python Code Author: Guido van Rossum , Barry War ...
- 面试问题整理之python测试
1.下列哪个语句在Python中是非法的? A.x = y = z =1 B.x = (y = z + 1) C.x, y = y, x D.x += y 答案:B 2.关于Python内存管理,下列 ...
- IEEEXtreme 极限编程大赛题解
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 IEEEXtreme全球极限编程挑战赛,是由IEEE主办,IEEE学生分会组织承办.IEEE会员参与指导和监督的.IEEE学生会员以团队 ...
- Python中的多进程与多线程(一)
一.背景 最近在Azkaban的测试工作中,需要在测试环境下模拟线上的调度场景进行稳定性测试.故而重操python旧业,通过python编写脚本来构造类似线上的调度场景.在脚本编写过程中,碰到这样一个 ...
- Python高手之路【六】python基础之字符串格式化
Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...
随机推荐
- sql:将字符类型字段转换成数字并排序
使用cast 函数可以把字符类型字段(数学形式)转换为数字 比如 AND m.nfrc_meeting_no=? ORDER BY cast(m.BOOTH AS INT) ASC "; 结 ...
- [BZOJ4591][SHOI2015]超能粒子炮·改(Lucas定理+数位DP)
大组合数取模可以想到Lucas,考虑Lucas的意义,实际上是把数看成P进制计算. 于是问题变成求1~k的所有2333进制数上每一位数的组合数之积. 数位DP,f[i][0/1]表示从高到低第i位,这 ...
- [BZOJ2427][HAOI2010]软件安装(Tarjan+DP)
2427: [HAOI2010]软件安装 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1987 Solved: 791[Submit][Statu ...
- JZYZOJ1998 [bzoj3223] 文艺平衡树 splay 平衡树
http://172.20.6.3/Problem_Show.asp?id=1998 平衡树区间翻转的板子,重新写一遍,给自己码一个板子. #include<iostream> #incl ...
- NOI1999 JZYZOJ1289 棋盘分割 dp 方差的数学结论
http://172.20.6.3/Problem_Show.asp?id=1289 除了下标一坨一坨屎一样挺恶心其他都还挺容易的dp,这道题才发现scanf保留小数位是四舍五入的,惊了. f[k][ ...
- Eclipse下使用Stanford CoreNLP的方法
源码下载地址:CoreNLP官网. 目前release的CoreNLP version 3.5.0版本仅支持java-1.8及以上版本,因此有时需要为Eclipse添加jdk-1.8配置,配置方法如下 ...
- PHP -- 函数基础入门
FROM : http://www.cnblogs.com/kym/archive/2010/02/14/1668300.html, http://www.cnblogs.com/kym/archiv ...
- Codeforces Beta Round #10 A. Power Consumption Calculation 水题
A. Power Consumption Calculation 题目连接: http://www.codeforces.com/contest/10/problem/A Description To ...
- CROC 2016 - Elimination Round (Rated Unofficial Edition) D. Robot Rapping Results Report 二分+拓扑排序
D. Robot Rapping Results Report 题目连接: http://www.codeforces.com/contest/655/problem/D Description Wh ...
- 一个iframe注入漏洞,也是微软的 Application["error"] 漏洞
最近学校进行安全等级评估,有人给我打电话,说我之前写的一个网站存在iframe注入漏洞,页面是error页面.我于是用netsparker扫描了自己的网站,果然发现error页面存在漏洞,我写网站的时 ...