forked from joshgachnang/diveintopython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
198 lines (188 loc) · 14.7 KB
/
index.html
File metadata and controls
198 lines (188 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<!DOCTYPE html
PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Chapter 16. Functional Programming</title>
<link rel="stylesheet" href="/css/diveintopython.css" type="text/css" />
<link rev="made" href="josh@servercobra.com" />
<meta name="generator" content="DocBook XSL Stylesheets V1.52.2" />
<meta name="keywords" content="Python, Dive Into Python, tutorial, object-oriented, programming, documentation, book, free" />
<meta name="description" content="Python from novice to pro" />
<link rel="home" href="http://www.diveintopython.net/" title="Dive Into Python" />
<link rel="up" href="http://www.diveintopython.net/" title="Dive Into Python" />
<link rel="previous" href="http://www.diveintopython.net/" title="15.5. Summary" />
<link rel="next" href="http://www.diveintopython.net/" title="16.2. Finding the path" />
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-9740779-18']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script></head>
<body>
<style type="text/css">body{margin-top:0!important;padding-top:0!important;min-width:800px!important;}#wm-ipp a:hover{text-decoration:underline!important;}</style>
<table id="Header" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
<tr>
<td id="breadcrumb" colspan="5" align="left" valign="top">You are here: <a href="http://www.diveintopython.net/index.html">Home</a> > <a href="http://www.diveintopython.net/toc/index.html">Dive Into Python</a> > <span class="thispage">Functional Programming</span></td>
<td id="navigation" align="right" valign="top"> <a href="http://www.diveintopython.net/refactoring/summary.html" title="Prev: “Summary”"><<</a> <a href="http://www.diveintopython.net/functional_programming/finding_the_path.html" title="Next: “Finding the path”">>></a></td>
</tr>
<tr>
<td colspan="3" id="logocontainer">
<h1 id="logo"><a href="http://www.diveintopython.net/index.html" accesskey="1">Dive Into Python</a></h1>
<p id="tagline">Python from novice to pro</p>
</td>
<td colspan="3" align="right">
<form id="search" method="GET" action="http://www.google.com/custom">
<p><label for="q" accesskey="4">Find: </label><input type="text" id="q" name="q" size="20" maxlength="255" value=" " /> <input type="submit" value="Search" /><input type="hidden" name="cof" value="LW:752;L:http://diveintopython.org/images/diveintopython.png;LH:42;AH:left;GL:0;AWFID:3ced2bb1f7f1b212;" /><input type="hidden" name="domains" value="diveintopython.org" /><input type="hidden" name="sitesearch" value="diveintopython.org" /></p>
</form>
</td>
</tr>
</table>
<div class="chapter" lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title"><a name="regression"></a>Chapter 16. Functional Programming
</h2>
</div>
</div>
<div></div>
</div>
<div class="toc">
<ul>
<li><span class="section"><a href="http://www.diveintopython.net/functional_programming/index.html#regression.divein">16.1. Diving in</a></span></li>
<li><span class="section"><a href="http://www.diveintopython.net/functional_programming/finding_the_path.html">16.2. Finding the path</a></span></li>
<li><span class="section"><a href="http://www.diveintopython.net/functional_programming/filtering_lists.html">16.3. Filtering lists revisited</a></span></li>
<li><span class="section"><a href="http://www.diveintopython.net/functional_programming/mapping_lists.html">16.4. Mapping lists revisited</a></span></li>
<li><span class="section"><a href="http://www.diveintopython.net/functional_programming/data_centric.html">16.5. Data-centric programming</a></span></li>
<li><span class="section"><a href="http://www.diveintopython.net/functional_programming/dynamic_import.html">16.6. Dynamically importing modules</a></span></li>
<li><span class="section"><a href="http://www.diveintopython.net/functional_programming/all_together.html">16.7. Putting it all together</a></span></li>
<li><span class="section"><a href="http://www.diveintopython.net/functional_programming/summary.html">16.8. Summary</a></span></li>
</ul>
</div>
<div class="section" lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title"><a name="regression.divein"></a>16.1. Diving in
</h2>
</div>
</div>
<div></div>
</div>
<div class="abstract">
<p>In <a href="http://www.diveintopython.net/unit_testing/index.html" title="Chapter 13. Unit Testing">Chapter 13, <i>Unit Testing</i></a>, you learned about the philosophy of unit testing. In <a href="http://www.diveintopython.net/unit_testing/stage_1.html" title="Chapter 14. Test-First Programming">Chapter 14, <i>Test-First Programming</i></a>, you stepped through the implementation of basic unit tests in <span class="application">Python</span>. In <a href="http://www.diveintopython.net/refactoring/index.html" title="Chapter 15. Refactoring">Chapter 15, <i>Refactoring</i></a>, you saw how unit testing makes large-scale refactoring easier. This chapter will build on those sample programs, but here
we will focus more on advanced <span class="application">Python</span>-specific techniques, rather than on unit testing itself.
</p>
</div>
<p>The following is a complete <span class="application">Python</span> program that acts as a cheap and simple regression testing framework. It takes unit tests that you've written for individual
modules, collects them all into one big test suite, and runs them all at once. I actually use this script as part of the
build process for this book; I have unit tests for several of the example programs (not just the <tt class="filename">roman.py</tt> module featured in <a href="http://www.diveintopython.net/unit_testing/index.html" title="Chapter 13. Unit Testing">Chapter 13, <i>Unit Testing</i></a>), and the first thing my automated build script does is run this program to make sure all my examples still work. If this
regression test fails, the build immediately stops. I don't want to release non-working examples any more than you want to
download them and sit around scratching your head and yelling at your monitor and wondering why they don't work.
</p>
<div class="example"><a name="d0e35275"></a><h3 class="title">Example 16.1. <tt class="filename">regression.py</tt></h3>
<p>If you have not already done so, you can <a href="http://www.diveintopython.net/download/diveintopython-examples-5.4.zip" title="Download example scripts">download this and other examples</a> used in this book.
</p><pre class="programlisting">
<span class="pystring">"""Regression testing framework
This module will search for scripts in the same directory named
XYZtest.py. Each such script should be a test suite that tests a
module through PyUnit. (As of Python 2.1, PyUnit is included in
the standard library as "unittest".) This script will aggregate all
found test suites into one big test suite and run them all at once.
"""</span>
<span class="pykeyword">import</span> sys, os, re, unittest
<span class="pykeyword">def</span><span class="pyclass"> regressionTest</span>():
path = os.path.abspath(os.path.dirname(sys.argv[0]))
files = os.listdir(path)
test = re.compile(<span class="pystring">"test\.py$"</span>, re.IGNORECASE)
files = filter(test.search, files)
filenameToModuleName = <span class="pykeyword">lambda</span> f: os.path.splitext(f)[0]
moduleNames = map(filenameToModuleName, files)
modules = map(__import__, moduleNames)
load = unittest.defaultTestLoader.loadTestsFromModule
<span class="pykeyword">return</span> unittest.TestSuite(map(load, modules))
<span class="pykeyword">if</span> __name__ == <span class="pystring">"__main__"</span>:
unittest.main(defaultTest=<span class="pystring">"regressionTest"</span>)
</pre></div>
<p>Running this script in the same directory as the rest of the example scripts that come with this book will find all the unit
tests, named <tt class="filename"><i class="replaceable"></i></tt><tt>module</tt>test.py, run them as a single test, and pass or fail them all at once.
</p>
<div class="example"><a name="d0e35293"></a><h3 class="title">Example 16.2. Sample output of <tt class="filename">regression.py</tt></h3><pre class="screen">
<tt class="prompt">[you@localhost py]$ </tt><span class="userinput">python regression.py -v</span>
<span class="computeroutput">help should fail with no object ... ok </span><a name="regression.divein.1.1"></a><img src="http://www.diveintopython.net/images/callouts/1.png" alt="1" border="0" width="12" height="12" /><span class="computeroutput">
help should return known result for apihelper ... ok
help should honor collapse argument ... ok
help should honor spacing argument ... ok
buildConnectionString should fail with list input ... ok </span><a name="regression.divein.1.2"></a><img src="http://www.diveintopython.net/images/callouts/2.png" alt="2" border="0" width="12" height="12" /><span class="computeroutput">
buildConnectionString should fail with string input ... ok
buildConnectionString should fail with tuple input ... ok
buildConnectionString handles empty dictionary ... ok
buildConnectionString returns known result with known input ... ok
fromRoman should only accept uppercase input ... ok </span><a name="regression.divein.1.3"></a><img src="http://www.diveintopython.net/images/callouts/3.png" alt="3" border="0" width="12" height="12" /><span class="computeroutput">
toRoman should always return uppercase ... ok
fromRoman should fail with blank string ... ok
fromRoman should fail with malformed antecedents ... ok
fromRoman should fail with repeated pairs of numerals ... ok
fromRoman should fail with too many repeated numerals ... ok
fromRoman should give known result with known input ... ok
toRoman should give known result with known input ... ok
fromRoman(toRoman(n))==n for all n ... ok
toRoman should fail with non-integer input ... ok
toRoman should fail with negative input ... ok
toRoman should fail with large input ... ok
toRoman should fail with 0 input ... ok
kgp a ref test ... ok
kgp b ref test ... ok
kgp c ref test ... ok
kgp d ref test ... ok
kgp e ref test ... ok
kgp f ref test ... ok
kgp g ref test ... ok
----------------------------------------------------------------------
Ran 29 tests in 2.799s
OK</span></pre><div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td width="12" valign="top" align="left"><a href="http://www.diveintopython.net/functional_programming/index.html#regression.divein.1.1"><img src="http://www.diveintopython.net/images/callouts/1.png" alt="1" border="0" width="12" height="12" /></a>
</td>
<td valign="top" align="left">The first 5 tests are from <tt class="filename">apihelpertest.py</tt>, which tests the example script from <a href="http://www.diveintopython.net/power_of_introspection/index.html" title="Chapter 4. The Power Of Introspection">Chapter 4, <i>The Power Of Introspection</i></a>.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href="http://www.diveintopython.net/functional_programming/index.html#regression.divein.1.2"><img src="http://www.diveintopython.net/images/callouts/2.png" alt="2" border="0" width="12" height="12" /></a>
</td>
<td valign="top" align="left">The next 5 tests are from <tt class="filename">odbchelpertest.py</tt>, which tests the example script from <a href="http://www.diveintopython.net/getting_to_know_python/index.html" title="Chapter 2. Your First Python Program">Chapter 2, <i>Your First Python Program</i></a>.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href="http://www.diveintopython.net/functional_programming/index.html#regression.divein.1.3"><img src="http://www.diveintopython.net/images/callouts/3.png" alt="3" border="0" width="12" height="12" /></a>
</td>
<td valign="top" align="left">The rest are from <tt class="filename">romantest.py</tt>, which you studied in depth in <a href="http://www.diveintopython.net/unit_testing/index.html" title="Chapter 13. Unit Testing">Chapter 13, <i>Unit Testing</i></a>.
</td>
</tr>
</table>
</div>
</div>
</div>
</div>
<table class="Footer" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
<tr>
<td width="35%" align="left"><br /><a class="NavigationArrow" href="http://www.diveintopython.net/refactoring/summary.html"><< Summary</a></td>
<td width="30%" align="center"><br /> <span class="divider">|</span> <span class="thispage">1</span> <span class="divider">|</span> <a href="http://www.diveintopython.net/functional_programming/finding_the_path.html" title="16.2. Finding the path">2</a> <span class="divider">|</span> <a href="http://www.diveintopython.net/functional_programming/filtering_lists.html" title="16.3. Filtering lists revisited">3</a> <span class="divider">|</span> <a href="http://www.diveintopython.net/functional_programming/mapping_lists.html" title="16.4. Mapping lists revisited">4</a> <span class="divider">|</span> <a href="http://www.diveintopython.net/functional_programming/data_centric.html" title="16.5. Data-centric programming">5</a> <span class="divider">|</span> <a href="http://www.diveintopython.net/functional_programming/dynamic_import.html" title="16.6. Dynamically importing modules">6</a> <span class="divider">|</span> <a href="http://www.diveintopython.net/functional_programming/all_together.html" title="16.7. Putting it all together">7</a> <span class="divider">|</span> <a href="http://www.diveintopython.net/functional_programming/summary.html" title="16.8. Summary">8</a> <span class="divider">|</span>
</td>
<td width="35%" align="right"><br /><a class="NavigationArrow" href="http://www.diveintopython.net/functional_programming/finding_the_path.html">Finding the path >></a></td>
</tr>
<tr>
<td colspan="3"><br /></td>
</tr>
</table>
<div class="Footer">
<p class="copyright">Copyright © 2000, 2001, 2002, 2003, 2004 <a href="mailto:josh@servercobra.com">Mark Pilgrim</a></p>
</div>
</body>
</html>