diff --git a/advanced-iterators.html b/advanced-iterators.html index ee15c6d4..d780ce66 100755 --- a/advanced-iterators.html +++ b/advanced-iterators.html @@ -71,7 +71,7 @@
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!) -
+you@localhost:~/diveintopython3/examples$ python3 alphametics.py "HAWAII + IDAHO + IOWA + OHIO == STATES" HAWAII + IDAHO + IOWA + OHIO = STATES 510199 + 98153 + 9301 + 3593 == 621246 diff --git a/dip3.css b/dip3.css index 76cc4a9c..71a3cd69 100755 --- a/dip3.css +++ b/dip3.css @@ -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; diff --git a/files.html b/files.html index 02aeaa2e..018b87c4 100644 --- a/files.html +++ b/files.html @@ -251,7 +251,7 @@Reading Data One Line At A Time
format()
string method, you can print out the line number and the line itself. The format specifier {:>4}
means “print this argument right-justified within 4 spaces.” The a_line variable contains the complete line, carriage returns and all. The rstrip()
string method removes the trailing whitespace, including the carriage return characters.
-+you@localhost:~/diveintopython3$ python3 examples/oneline.py 1 Dora 2 Ethan @@ -418,7 +418,7 @@Handling Compressed Files
As an added bonus, it supports the
with
statement too, so you can let Python automatically close your gzip-compressed file when you’re done with it. -+you@localhost:~$ python3 >>> import gzip @@ -515,7 +515,7 @@Redirecting Standard Output
Check this out: -
+you@localhost:~/diveintopython3/examples$ python3 stdout.py A C diff --git a/http-web-services.html b/http-web-services.html index 435d6319..b612b3c1 100755 --- a/http-web-services.html +++ b/http-web-services.html @@ -351,7 +351,7 @@Introducing
httplib2
On Mac OS X, run the
Terminal.app
application in your/Applications/Utilities/
folder. On Linux, run theTerminal
application, which is usually in yourApplications
menu underAccessories
orSystem
. -+you@localhost:~/Desktop$ unzip httplib2-python3-0.5.0.zip Archive: httplib2-python3-0.5.0.zip inflating: httplib2-python3-0.5.0/README diff --git a/index.html b/index.html index 01aa1e09..8ed3ca78 100644 --- a/index.html +++ b/index.html @@ -5,10 +5,11 @@ @@ -48,6 +49,7 @@Table of Contents (expand)
Porting Code to Python 3 with 2to3
Special Method Names Where to Go From Here + Troubleshooting diff --git a/j/dip3.js b/j/dip3.js index c2c45e6b..a0b1a7bc 100644 --- a/j/dip3.js +++ b/j/dip3.js @@ -141,7 +141,12 @@ $(document).ready(function() { /* wrap code block in a div and insert widget block */ $(this).wrapInner(''); - $(this).prepend(''); + var widgetHTML = ' [' + HS.visible + '] [open in new window]'; + if ($(this).hasClass('cmdline')) { + widgetHTML += ' [command line help]'; + } + widgetHTML += ''; + $(this).prepend(widgetHTML); /* move download link into widget block */ $(this).prev("p.d").each(function(i) { @@ -155,6 +160,11 @@ $(document).ready(function() { postelm.id = postid; $(this).before('skip over this code listing'); }); + + $("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() { diff --git a/refactoring.html b/refactoring.html index af177e39..5856381e 100755 --- a/refactoring.html +++ b/refactoring.html @@ -41,7 +41,7 @@
Diving In
Since your code has a bug, and you now have a test case that tests this bug, the test case will fail: -
+you@localhost:~/diveintopython3/examples$ python3 romantest8.py -v from_roman should fail with blank string ... FAIL from_roman should fail with malformed antecedents ... ok @@ -89,7 +89,7 @@Diving In
I don’t think I’ve mentioned this yet anywhere in this book, so let this serve as your final lesson in string formatting. 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 {0}
to refer to the first parameter to theformat()
method, you can simply use{}
and Python will fill in the proper positional index for you. This works for any number of arguments; the first{}
is{0}
, the second{}
is{1}
, and so forth. -+you@localhost:~/diveintopython3/examples$ python3 romantest8.py -v from_roman should fail with blank string ... ok ① from_roman should fail with malformed antecedents ... ok @@ -168,7 +168,7 @@Handling Changing Requirements
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. -
+you@localhost:~/diveintopython3/examples$ python3 romantest9.py -v from_roman should fail with blank string ... ok from_roman should fail with malformed antecedents ... ok @@ -263,7 +263,7 @@Handling Changing Requirements
You may be skeptical that these two small changes are all that you need. Hey, don’t take my word for it; see for yourself. -
+you@localhost:~/diveintopython3/examples$ python3 romantest9.py -v from_roman should fail with blank string ... ok from_roman should fail with malformed antecedents ... ok @@ -422,7 +422,7 @@Refactoring
But does it work? Why yes, yes it does. And I can prove it. -
+you@localhost:~/diveintopython3/examples$ python3 romantest10.py -v from_roman should fail with blank string ... ok from_roman should fail with malformed antecedents ... ok diff --git a/unit-testing.html b/unit-testing.html index ee2faf48..6ceaa247 100755 --- a/unit-testing.html +++ b/unit-testing.html @@ -144,7 +144,7 @@A Single Question
At this stage, you want to define the API of the to_roman()
function, but you don’t want to code it yet. (Your test needs to fail first.) To stub it out, use the Python reserved wordpass
, which does precisely nothing.Execute
romantest1.py
on the command line to run the test. If you call it with the-v
command-line option, it will give more verbose output so you can see exactly what’s going on as each test case runs. With any luck, your output should look like this: -+you@localhost:~/diveintopython3/examples$ python3 romantest1.py -v test_to_roman_known_values (__main__.KnownValues) ① to_roman should give known result with known input ... FAIL ② @@ -213,7 +213,7 @@A Single Question
subtracting 4 from input, adding IV to output 'MCDXXIV'So the
to_roman()
function appears to work, at least in this manual spot check. But will it pass the test case you wrote? -+you@localhost:~/diveintopython3/examples$ python3 romantest1.py -v test_to_roman_known_values (__main__.KnownValues) to_roman should give known result with known input ... ok ① @@ -260,7 +260,7 @@“Halt And Catch Fire”
Pay close attention to this last line of code. Instead of calling
to_roman()
directly and manually checking that it raises a particular exception (by wrapping it in atry...except
block), theassertRaises
method has encapsulated all of that for us. All you do is tell it what exception you’re expecting (roman2.OutOfRangeError
), the function (to_roman()
), and the function’s arguments (4000
). TheassertRaises
method takes care of callingto_roman()
and checking that it raisesroman2.OutOfRangeError
.Also note that you’re passing the
to_roman()
function itself as an argument; you’re not calling it, and you’re not passing the name of it as a string. Have I mentioned recently how handy it is that everything in Python is an object?So what happens when you run the test suite with this new test? -
+you@localhost:~/diveintopython3/examples$ python3 romantest2.py -v test_to_roman_known_values (__main__.KnownValues) to_roman should give known result with known input ... ok @@ -291,7 +291,7 @@“Halt And Catch Fire”
Exceptions don’t actually do anything, but you need at least one line of code to make a class. Calling pass
does precisely nothing, but it’s a line of Python code, so that makes it a class.Now run the test suite again. -
+you@localhost:~/diveintopython3/examples$ python3 romantest2.py -v test_to_roman_known_values (__main__.KnownValues) to_roman should give known result with known input ... ok @@ -331,7 +331,7 @@“Halt And Catch Fire”
This is straightforward: if the given input (n) is greater than 3999
, raise anOutOfRangeError
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’s language or environment).Does this make the test pass? Let’s find out. -
+you@localhost:~/diveintopython3/examples$ python3 romantest2.py -v test_to_roman_known_values (__main__.KnownValues) to_roman should give known result with known input ... ok @@ -382,7 +382,7 @@More Halting, More Fire
Now check that the tests fail: -
+you@localhost:~/diveintopython3/examples$ python3 romantest3.py -v test_to_roman_known_values (__main__.KnownValues) to_roman should give known result with known input ... ok @@ -435,7 +435,7 @@More Halting, More Fire
I could show you a whole series of unrelated examples to show that the multiple-comparisons-at-once shortcut works, but instead I’ll just run the unit tests and prove it. -
+you@localhost:~/diveintopython3/examples$ python3 romantest3.py -v test_to_roman_known_values (__main__.KnownValues) to_roman should give known result with known input ... ok @@ -486,7 +486,7 @@And One More Thing…
Now check that the test fails properly. -
+you@localhost:~/diveintopython3/examples$ python3 romantest4.py -v test_to_roman_known_values (__main__.KnownValues) to_roman should give known result with known input ... ok @@ -534,7 +534,7 @@And One More Thing…
Finally, check that the code does indeed make the test pass. -
+you@localhost:~/diveintopython3/examples$ python3 romantest4.py -v test_to_roman_known_values (__main__.KnownValues) to_roman should give known result with known input ... ok @@ -586,7 +586,7 @@A Pleasing Symmetry
These new tests won’t even fail yet. We haven’t defined a
from_roman()
function at all, so they’ll just raise errors. -+you@localhost:~/diveintopython3/examples$ python3 romantest5.py E.E.... ====================================================================== @@ -622,7 +622,7 @@A Pleasing Symmetry
Now the test cases will actually fail. -
+you@localhost:~/diveintopython3/examples$ python3 romantest5.py F.F.... ====================================================================== @@ -689,7 +689,7 @@A Pleasing Symmetry
Time to re-run the tests. -
+you@localhost:~/diveintopython3/examples$ python3 romantest5.py ....... ---------------------------------------------------------------------- @@ -745,7 +745,7 @@More Bad Input
All three of these tests should fail, since the
from_roman()
function doesn’t currently have any validity checking. (If they don’t fail now, then what the heck are they testing?) -+you@localhost:~/diveintopython3/examples$ python3 romantest6.py FFF....... ====================================================================== @@ -809,7 +809,7 @@More Bad Input
And re-run the tests… -
+you@localhost:~/diveintopython3/examples$ python3 romantest7.py .......... ---------------------------------------------------------------------- diff --git a/where-to-go-from-here.html b/where-to-go-from-here.html index e8ded06e..acb034d1 100644 --- a/where-to-go-from-here.html +++ b/where-to-go-from-here.html @@ -79,7 +79,7 @@Where To Look For Python 3-Compatible Code
BitBucket: list of projects matching “python3” (and those matching “python 3”) - © 2001–10 Mark Pilgrim diff --git a/your-first-python-program.html b/your-first-python-program.html index 3c726ae0..50590510 100755 --- a/your-first-python-program.html +++ b/your-first-python-program.html @@ -58,7 +58,7 @@
Diving In
1.0 TB 931.3 GiBOn Mac OS X or Linux, it would look something like this: -
+you@localhost:~/diveintopython3/examples$ python3 humansize.py 1.0 TB 931.3 GiB