Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add cmdline classes and auto-links to new troubleshooting appendix
  • Loading branch information
Mark Pilgrim committed Jul 26, 2010
1 parent b26807f commit 7516423
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 28 deletions.
2 changes: 1 addition & 1 deletion advanced-iterators.html
Expand Up @@ -71,7 +71,7 @@ <h2 id=divingin>Diving In</h2>

<p>You can run the program from the command line. On Linux, it would look like this. (These may take some time, depending on the speed of your computer, and there is no progress bar. Just be patient!)

<pre class='nd screen'>
<pre class='nd screen cmdline'>
<samp class=p>you@localhost:~/diveintopython3/examples$ </samp><kbd>python3 alphametics.py "HAWAII + IDAHO + IOWA + OHIO == STATES"</kbd>
<samp>HAWAII + IDAHO + IOWA + OHIO = STATES
510199 + 98153 + 9301 + 3593 == 621246</samp>
Expand Down
12 changes: 12 additions & 0 deletions dip3.css
Expand Up @@ -353,6 +353,18 @@ h3:before {
counter-increment: h3;
content: 'C.' counter(h2) '.' counter(h3) '. '
}
#appd h1:before {
counter-increment: h1;
content: 'Appendix D. '
}
#appd h2:before {
counter-increment: h2;
content: 'D.' counter(h2) '. '
}
#appd h3:before {
counter-increment: h3;
content: 'D.' counter(h2) '.' counter(h3) '. '
}
aside {
display: block;
float: right;
Expand Down
6 changes: 3 additions & 3 deletions files.html
Expand Up @@ -251,7 +251,7 @@ <h3 id=for>Reading Data One Line At A Time</h3>
<li>Using <a href=strings.html#formatting-strings>the <code>format()</code> string method</a>, you can print out the line number and the line itself. The format specifier <code>{:>4}</code> means &#8220;print this argument right-justified within 4 spaces.&#8221; The <var>a_line</var> variable contains the complete line, carriage returns and all. The <code>rstrip()</code> string method removes the trailing whitespace, including the carriage return characters.
</ol>

<pre class=screen>
<pre class='screen cmdline'>
<samp class=p>you@localhost:~/diveintopython3$ </samp><kbd class=pp>python3 examples/oneline.py</kbd>
<samp> 1 Dora
2 Ethan
Expand Down Expand Up @@ -418,7 +418,7 @@ <h3 id=gzip>Handling Compressed Files</h3>

<p>As an added bonus, it supports the <code>with</code> statement too, so you can let Python automatically close your gzip-compressed file when you&#8217;re done with it.

<pre class='nd screen'>
<pre class='nd screen cmdline'>
<samp class=p>you@localhost:~$ </samp><kbd>python3</kbd>

<samp class=p>>>> </samp><kbd class=pp>import gzip</kbd>
Expand Down Expand Up @@ -515,7 +515,7 @@ <h3 id=redirect>Redirecting Standard Output</h3>

<p>Check this out:

<pre class='nd screen'>
<pre class='nd screen cmdline'>
<samp class=p>you@localhost:~/diveintopython3/examples$ </samp><kbd>python3 stdout.py</kbd>
<samp>A
C</samp>
Expand Down
2 changes: 1 addition & 1 deletion http-web-services.html
Expand Up @@ -351,7 +351,7 @@ <h2 id=introducing-httplib2>Introducing <code>httplib2</code></h2>

<p>On Mac OS X, run the <code>Terminal.app</code> application in your <code>/Applications/Utilities/</code> folder. On Linux, run the <code>Terminal</code> application, which is usually in your <code>Applications</code> menu under <code>Accessories</code> or <code>System</code>.

<pre class=screen>
<pre class='screen cmdline'>
<samp class=p>you@localhost:~/Desktop$ </samp><kbd><mark>unzip httplib2-python3-0.5.0.zip</mark></kbd>
<samp>Archive: httplib2-python3-0.5.0.zip
inflating: httplib2-python3-0.5.0/README
Expand Down
4 changes: 3 additions & 1 deletion index.html
Expand Up @@ -5,10 +5,11 @@
<link rel=stylesheet href=dip3.css>
<style>
h1:before,h2:before{content:''}
#a,#b,#c{list-style:none;margin:0 0 0 -1.7em}
#a,#b,#c,#d{list-style:none;margin:0 0 0 -1.7em}
#a:before{content:'A. \00a0 \00a0'}
#b:before{content:'B. \00a0 \00a0'}
#c:before{content:'C. \00a0 \00a0'}
#d:before{content:'D. \00a0 \00a0'}
</style>
<link rel=stylesheet media='only screen and (max-device-width: 480px)' href=mobile.css>
<link rel=stylesheet media=print href=print.css>
Expand Down Expand Up @@ -48,6 +49,7 @@ <h2>Table of Contents (<a href=table-of-contents.html>expand</a>)</h2>
<li id=a><a href=porting-code-to-python-3-with-2to3.html>Porting Code to Python 3 with <code>2to3</code></a>
<li id=b><a href=special-method-names.html>Special Method Names</a>
<li id=c><a href=where-to-go-from-here.html>Where to Go From Here</a>
<li id=d><a href=troubleshooting.html>Troubleshooting</a>
</ol>
<!-- /toc -->

Expand Down
12 changes: 11 additions & 1 deletion j/dip3.js
Expand Up @@ -141,7 +141,12 @@ $(document).ready(function() {

/* wrap code block in a div and insert widget block */
$(this).wrapInner('<div class=b></div>');
$(this).prepend('<div class=w>[<a class=toggle href="javascript:toggleCodeBlock(\'' + this.id + '\')">' + HS.visible + '</a>] [<a href="javascript:plainTextOnClick(\'' + this.id + '\')">open in new window</a>]</div>');
var widgetHTML = '<div class=w>[<a class=toggle href="javascript:toggleCodeBlock(\'' + this.id + '\')">' + HS.visible + '</a>] [<a href="javascript:plainTextOnClick(\'' + this.id + '\')">open in new window</a>]';
if ($(this).hasClass('cmdline')) {
widgetHTML += ' [<a href="troubleshooting.html#running-python-on-the-command-line">command line help</a>]';
}
widgetHTML += '</div>';
$(this).prepend(widgetHTML);

/* move download link into widget block */
$(this).prev("p.d").each(function(i) {
Expand All @@ -155,6 +160,11 @@ $(document).ready(function() {
postelm.id = postid;
$(this).before('<p class=skip><a href=#' + postid + '>skip over this code listing</a>');
});

$("pre.screen.cmdline:not(.nd)").each(function(i) {
/* add link to command-line help */
this.id = "autopre" + i;
});

/* make skip links disappear until you tab to them */
$(".skip a").blur(function() {
Expand Down
10 changes: 5 additions & 5 deletions refactoring.html
Expand Up @@ -41,7 +41,7 @@ <h2 id=divingin>Diving In</h2>
</ol>

<p>Since your code has a bug, and you now have a test case that tests this bug, the test case will fail:
<pre class='nd screen'>
<pre class='nd screen cmdline'>
<samp class=p>you@localhost:~/diveintopython3/examples$ </samp><kbd>python3 romantest8.py -v</kbd>
<samp>from_roman should fail with blank string ... FAIL
from_roman should fail with malformed antecedents ... ok
Expand Down Expand Up @@ -89,7 +89,7 @@ <h2 id=divingin>Diving In</h2>
<li>I don&#8217;t think I&#8217;ve mentioned this yet anywhere in this book, so let this serve as your final lesson in <a href=strings.html#formatting-strings>string formatting</a>. Starting in Python 3.1, you can skip the numbers when using positional indexes in a format specifier. That is, instead of using the format specifier <code>{0}</code> to refer to the first parameter to the <code>format()</code> method, you can simply use <code>{}</code> and Python will fill in the proper positional index for you. This works for any number of arguments; the first <code>{}</code> is <code>{0}</code>, the second <code>{}</code> is <code>{1}</code>, and so forth.
</ol>

<pre class=screen>
<pre class='screen cmdline'>
<samp class=p>you@localhost:~/diveintopython3/examples$ </samp><kbd>python3 romantest8.py -v</kbd>
<a><samp>from_roman should fail with blank string ... ok</samp> <span class=u>&#x2460;</span></a>
<samp>from_roman should fail with malformed antecedents ... ok
Expand Down Expand Up @@ -168,7 +168,7 @@ <h2 id=changing-requirements>Handling Changing Requirements</h2>

<p>Now your test cases are up to date with the new requirements, but your code is not, so you expect several of the test cases to fail.

<pre class=screen>
<pre class='screen cmdline'>
<samp class=p>you@localhost:~/diveintopython3/examples$ </samp><kbd>python3 romantest9.py -v</kbd>
<samp>from_roman should fail with blank string ... ok
from_roman should fail with malformed antecedents ... ok
Expand Down Expand Up @@ -263,7 +263,7 @@ <h2 id=changing-requirements>Handling Changing Requirements</h2>

<p>You may be skeptical that these two small changes are all that you need. Hey, don&#8217;t take my word for it; see for yourself.

<pre class='nd screen'>
<pre class='nd screen cmdline'>
<samp class=p>you@localhost:~/diveintopython3/examples$ </samp><kbd>python3 romantest9.py -v</kbd>
<samp>from_roman should fail with blank string ... ok
from_roman should fail with malformed antecedents ... ok
Expand Down Expand Up @@ -422,7 +422,7 @@ <h2 id=refactoring>Refactoring</h2>

<p>But does it work? Why yes, yes it does. And I can prove it.

<pre class=screen>
<pre class='screen cmdline'>
<samp class=p>you@localhost:~/diveintopython3/examples$ </samp><kbd>python3 romantest10.py -v</kbd>
<samp>from_roman should fail with blank string ... ok
from_roman should fail with malformed antecedents ... ok
Expand Down
28 changes: 14 additions & 14 deletions unit-testing.html
Expand Up @@ -144,7 +144,7 @@ <h2 id=romantest1>A Single Question</h2>
<li>At this stage, you want to define the <abbr>API</abbr> of the <code>to_roman()</code> function, but you don&#8217;t want to code it yet. (Your test needs to fail first.) To stub it out, use the Python reserved word <code>pass</code>, which does precisely nothing.
</ol>
<p>Execute <code>romantest1.py</code> on the command line to run the test. If you call it with the <code>-v</code> command-line option, it will give more verbose output so you can see exactly what&#8217;s going on as each test case runs. With any luck, your output should look like this:
<pre class=screen>
<pre class='screen cmdline'>
<samp class=p>you@localhost:~/diveintopython3/examples$ </samp><kbd>python3 romantest1.py -v</kbd>
<a><samp>test_to_roman_known_values (__main__.KnownValues)</samp> <span class=u>&#x2460;</span></a>
<a><samp>to_roman should give known result with known input ... FAIL</samp> <span class=u>&#x2461;</span></a>
Expand Down Expand Up @@ -213,7 +213,7 @@ <h2 id=romantest1>A Single Question</h2>
subtracting 4 from input, adding IV to output
'MCDXXIV'</samp></pre>
<p>So the <code>to_roman()</code> function appears to work, at least in this manual spot check. But will it pass the test case you wrote?
<pre class='nd screen'>
<pre class='nd screen cmdline'>
<samp class=p>you@localhost:~/diveintopython3/examples$ </samp><kbd>python3 romantest1.py -v</kbd>
<samp>test_to_roman_known_values (__main__.KnownValues)
<a>to_roman should give known result with known input ... ok <span class=u>&#x2460;</span></a>
Expand Down Expand Up @@ -260,7 +260,7 @@ <h2 id=romantest2>&#8220;Halt And Catch Fire&#8221;</h2>
<p>Pay close attention to this last line of code. Instead of calling <code>to_roman()</code> directly and manually checking that it raises a particular exception (by wrapping it in <a href=your-first-python-program.html#exceptions>a <code>try...except</code> block</a>), the <code>assertRaises</code> method has encapsulated all of that for us. All you do is tell it what exception you&#8217;re expecting (<code>roman2.OutOfRangeError</code>), the function (<code>to_roman()</code>), and the function&#8217;s arguments (<code>4000</code>). The <code>assertRaises</code> method takes care of calling <code>to_roman()</code> and checking that it raises <code>roman2.OutOfRangeError</code>.
<p>Also note that you&#8217;re passing the <code>to_roman()</code> function itself as an argument; you&#8217;re not calling it, and you&#8217;re not passing the name of it as a string. Have I mentioned recently how handy it is that <a href=your-first-python-program.html#everythingisanobject>everything in Python is an object</a>?
<p>So what happens when you run the test suite with this new test?
<pre class=screen>
<pre class='screen cmdline'>
<samp class=p>you@localhost:~/diveintopython3/examples$ </samp><kbd>python3 romantest2.py -v</kbd>
<samp>test_to_roman_known_values (__main__.KnownValues)
to_roman should give known result with known input ... ok
Expand Down Expand Up @@ -291,7 +291,7 @@ <h2 id=romantest2>&#8220;Halt And Catch Fire&#8221;</h2>
<li>Exceptions don&#8217;t actually do anything, but you need at least one line of code to make a class. Calling <code>pass</code> does precisely nothing, but it&#8217;s a line of Python code, so that makes it a class.
</ol>
<p>Now run the test suite again.
<pre class=screen>
<pre class='screen cmdline'>
<samp class=p>you@localhost:~/diveintopython3/examples$ </samp><kbd>python3 romantest2.py -v</kbd>
<samp>test_to_roman_known_values (__main__.KnownValues)
to_roman should give known result with known input ... ok
Expand Down Expand Up @@ -331,7 +331,7 @@ <h2 id=romantest2>&#8220;Halt And Catch Fire&#8221;</h2>
<li>This is straightforward: if the given input (<var>n</var>) is greater than <code>3999</code>, raise an <code>OutOfRangeError</code> exception. The unit test does not check the human-readable string that accompanies the exception, although you could write another test that did check it (but watch out for internationalization issues for strings that vary by the user&#8217;s language or environment).
</ol>
<p>Does this make the test pass? Let&#8217;s find out.
<pre class=screen>
<pre class='screen cmdline'>
<samp class=p>you@localhost:~/diveintopython3/examples$ </samp><kbd>python3 romantest2.py -v</kbd>
<samp>test_to_roman_known_values (__main__.KnownValues)
to_roman should give known result with known input ... ok
Expand Down Expand Up @@ -382,7 +382,7 @@ <h2 id=romantest3>More Halting, More Fire</h2>

<p>Now check that the tests fail:

<pre class='nd screen'>
<pre class='nd screen cmdline'>
<samp class=p>you@localhost:~/diveintopython3/examples$ </samp><kbd>python3 romantest3.py -v</kbd>
<samp>test_to_roman_known_values (__main__.KnownValues)
to_roman should give known result with known input ... ok
Expand Down Expand Up @@ -435,7 +435,7 @@ <h2 id=romantest3>More Halting, More Fire</h2>

<p>I could show you a whole series of unrelated examples to show that the multiple-comparisons-at-once shortcut works, but instead I&#8217;ll just run the unit tests and prove it.

<pre class='nd screen'>
<pre class='nd screen cmdline'>
<samp class=p>you@localhost:~/diveintopython3/examples$ </samp><kbd>python3 romantest3.py -v</kbd>
<samp>test_to_roman_known_values (__main__.KnownValues)
to_roman should give known result with known input ... ok
Expand Down Expand Up @@ -486,7 +486,7 @@ <h2 id=romantest4>And One More Thing&hellip;</h2>

<p>Now check that the test fails properly.

<pre class='nd screen'>
<pre class='nd screen cmdline'>
<samp class=p>you@localhost:~/diveintopython3/examples$ </samp><kbd>python3 romantest4.py -v</kbd>
<samp>test_to_roman_known_values (__main__.KnownValues)
to_roman should give known result with known input ... ok
Expand Down Expand Up @@ -534,7 +534,7 @@ <h2 id=romantest4>And One More Thing&hellip;</h2>

<p>Finally, check that the code does indeed make the test pass.

<pre class='nd screen'>
<pre class='nd screen cmdline'>
<samp class=p>you@localhost:~/diveintopython3/examples$ </samp><kbd>python3 romantest4.py -v</kbd>
<samp>test_to_roman_known_values (__main__.KnownValues)
to_roman should give known result with known input ... ok
Expand Down Expand Up @@ -586,7 +586,7 @@ <h2 id=romantest5>A Pleasing Symmetry</h2>

<p>These new tests won&#8217;t even fail yet. We haven&#8217;t defined a <code>from_roman()</code> function at all, so they&#8217;ll just raise errors.

<pre class='nd screen'>
<pre class='nd screen cmdline'>
<samp class=p>you@localhost:~/diveintopython3/examples$ </samp><kbd>python3 romantest5.py</kbd>
<samp>E.E....
======================================================================
Expand Down Expand Up @@ -622,7 +622,7 @@ <h2 id=romantest5>A Pleasing Symmetry</h2>

<p>Now the test cases will actually fail.

<pre class='nd screen'>
<pre class='nd screen cmdline'>
<samp class=p>you@localhost:~/diveintopython3/examples$ </samp><kbd>python3 romantest5.py</kbd>
<samp>F.F....
======================================================================
Expand Down Expand Up @@ -689,7 +689,7 @@ <h2 id=romantest5>A Pleasing Symmetry</h2>

<p>Time to re-run the tests.

<pre class='nd screen'>
<pre class='nd screen cmdline'>
<samp class=p>you@localhost:~/diveintopython3/examples$ </samp><kbd>python3 romantest5.py</kbd>
<samp>.......
----------------------------------------------------------------------
Expand Down Expand Up @@ -745,7 +745,7 @@ <h2 id=romantest6>More Bad Input</h2>

<p>All three of these tests should fail, since the <code>from_roman()</code> function doesn&#8217;t currently have any validity checking. (If they don&#8217;t fail now, then what the heck are they testing?)

<pre class='nd screen'>
<pre class='nd screen cmdline'>
<samp class=p>you@localhost:~/diveintopython3/examples$ </samp><kbd>python3 romantest6.py</kbd>
<samp>FFF.......
======================================================================
Expand Down Expand Up @@ -809,7 +809,7 @@ <h2 id=romantest6>More Bad Input</h2>

<p>And re-run the tests&hellip;

<pre class='nd screen'>
<pre class='nd screen cmdline'>
<samp class=p>you@localhost:~/diveintopython3/examples$ </samp><kbd>python3 romantest7.py</kbd>
<samp>..........
----------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion where-to-go-from-here.html
Expand Up @@ -79,7 +79,7 @@ <h2 id=code>Where To Look For Python 3-Compatible Code</h2>
<li><a href='http://bitbucket.org/repo/all/?name=python3'>BitBucket: list of projects matching &#8220;python3&#8221;</a> (and <a href='http://bitbucket.org/repo/all/?name=python+3'>those matching &#8220;python 3&#8221;</a>)
</ul>

<p class=v><a rel=prev href=special-method-names.html title='back to &#8220;Special Method Names&#8221;'><span class=u>&#x261C;</span></a> <a href=blank.html rel=next><span class=u>&#x261E;</span></a>
<p class=v><a rel=prev href=special-method-names.html title='back to &#8220;Special Method Names&#8221;'><span class=u>&#x261C;</span></a> <a href=troubleshooting.html rel=next title='onward to &#8220;Troubleshooting&#8221;'><span class=u>&#x261E;</span></a>

<p class=c>&copy; 2001&ndash;10 <a href=about.html>Mark Pilgrim</a>
<script src=j/jquery.js></script>
Expand Down
2 changes: 1 addition & 1 deletion your-first-python-program.html
Expand Up @@ -58,7 +58,7 @@ <h2 id=divingin>Diving In</h2>
<samp>1.0 TB
931.3 GiB</samp></pre>
<p>On Mac OS X or Linux, it would look something like this:
<pre class='nd screen'>
<pre class='nd screen cmdline'>
<samp class=p>you@localhost:~/diveintopython3/examples$ </samp><kbd>python3 humansize.py</kbd>
<samp>1.0 TB
931.3 GiB</samp></pre>
Expand Down

0 comments on commit 7516423

Please sign in to comment.