diff --git a/advanced-iterators.html b/advanced-iterators.html index ee15c6d..d780ce6 100755 --- a/advanced-iterators.html +++ b/advanced-iterators.html @@ -71,7 +71,7 @@ if __name__ == '__main__':

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 76cc4a9..71a3cd6 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 02aeaa2..018b87c 100644
--- a/files.html
+++ b/files.html
@@ -251,7 +251,7 @@ ValueError: I/O operation on closed file.
 
  • Using the 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 @@ AttributeError: '_io.BufferedReader' object has no attribute 'encoding'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 @@ print('C')

    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 435d631..b612b3c 100755
    --- a/http-web-services.html
    +++ b/http-web-services.html
    @@ -351,7 +351,7 @@ Writing c:\python31\Lib\site-packages\httplib2-python3_0.5.0-py3.1.egg-infoOn Mac OS X, run the Terminal.app application in your /Applications/Utilities/ folder. On Linux, run the Terminal application, which is usually in your Applications menu under Accessories or System.
     
    -
    +
     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 01aa1e0..8ed3ca7 100644
    --- a/index.html
    +++ b/index.html
    @@ -5,10 +5,11 @@
     
     
     
     
    @@ -48,6 +49,7 @@ h1:before,h2:before{content:''}
     
  • 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 c2c45e6..a0b1a7b 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 af177e3..5856381 100755 --- a/refactoring.html +++ b/refactoring.html @@ -41,7 +41,7 @@ body{counter-reset:h1 10}

    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 @@ FAILED (failures=1)
  • 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 the format() 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 @@ class RoundtripCheck(unittest.TestCase):
     
     

    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 @@ def from_roman(s):
     
     

    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 @@ def from_roman(s):
     
     

    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 ee2faf4..6ceaa24 100755
    --- a/unit-testing.html
    +++ b/unit-testing.html
    @@ -144,7 +144,7 @@ def to_roman(n):
     
  • 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 word pass, 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 @@ subtracting 10 from input, adding X to output
     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 @@ OK

    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 a try...except block), the assertRaises 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). The assertRaises method takes care of calling to_roman() and checking that it raises roman2.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 @@ FAILED (errors=1)
  • 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 @@ FAILED (failures=1)
  • This is straightforward: if the given input (n) is greater than 3999, raise an OutOfRangeError 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 @@ OK

    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 @@ FAILED (failures=2)

    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 @@ class OutOfRangeError(ValueError): pass
     
     

    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 @@ FAILED (failures=1)

    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 @@ OK

    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 @@ def from_roman(s):
     
     

    Now the test cases will actually fail. -

    +
     you@localhost:~/diveintopython3/examples$ python3 romantest5.py
     F.F....
     ======================================================================
    @@ -689,7 +689,7 @@ found I of length 1, adding 1
     
     

    Time to re-run the tests. -

    +
     you@localhost:~/diveintopython3/examples$ python3 romantest5.py
     .......
     ----------------------------------------------------------------------
    @@ -745,7 +745,7 @@ class InvalidRomanNumeralError(ValueError): pass

    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 @@ def from_roman(s):
     
     

    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 e8ded06..acb034d 100644
    --- a/where-to-go-from-here.html
    +++ b/where-to-go-from-here.html
    @@ -79,7 +79,7 @@
     
  • 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 3c726ae..5059051 100755 --- a/your-first-python-program.html +++ b/your-first-python-program.html @@ -58,7 +58,7 @@ if __name__ == '__main__': 1.0 TB 931.3 GiB

  • On Mac OS X or Linux, it would look something like this: -

    +
     you@localhost:~/diveintopython3/examples$ python3 humansize.py
     1.0 TB
     931.3 GiB