Errata

Python Cookbook

Errata for Python Cookbook

Submit your own errata for this product.

The errata list is a list of errors and their corrections that were found after the product was released.

The following errata were submitted by our customers and have not yet been approved or disproved by the author or editor. They solely represent the opinion of the customer.

Color Key: Serious technical mistake Minor technical mistake Language or formatting error Typo Question Note Update

Version Location Description Submitted by Date submitted
Printed Page 87
Section 3.13 Expanding and Compressing Tabs

Section 3.13 Expanding and Compressing Tabs, fails to take newline (
) into account.
Python's expandtabs function expands tabs to spaces on a *per-line* basis. The
unexpand function in 3.14.2 and expand_with_re in 3.14.3 fail to take newline into
account. They treat newline (
) as any other character.

Anonymous   
Printed Page 92
middle of page (function: anytolw(), et al)

The suite of functions beginning with anytolw() do not reproduce the functionality of
the solution presented on the preceding page and illustrated by the examples at the
top of the page. E.g.:

>>> s='PrintHTML'
>>> cw2us(s)
'print_html'
>>> cwtous(s)
'print_h_t_m_l'

Anonymous   
Printed Page 275
3rd paragraph

The example is not wrong, but I think the emphasis is misplaced. It is not so much
that "we were working with a temporary object", but that she['p']['p'] = 42 is not a
write operation. The example implies that we are trying to modify a nested
dictionary. What we actually want to do is modify a dictionary that is accessed via a
shelf key. The relevant phrase in the language reference is 'A ``shelf'' is a
persistent, dictionary-like object.' In other words, it looks like a duck, it walks
with a duck-like gait, but is not in fact your actual duck. I suggest that Recipe 8.4
should say something like "The shelve module provides a wonderful way of reading and
writing objects from and to files, without having to worry about how or where these
objects are stored. All we need is a key to access them. The assignment operator has
been overloaded to handle this file access. However, this convenient notation should
not blind us to the fact that file reads and writes are still going on. !
To write to the file: key=object. Therefore we have to write she['p'] = {'p' : 42}.
But we cannot combine the file key and the dictionary key: she['p']['p'] = 42
(DOESN'T WORK)." It has nothing to do with binding a name to a temporary object. It
is just the syntax for the shelf write.

Anonymous   
Printed Page 285
code

for dd in d:
l = dd[1]
.
.
.

#the third item of the cursor.description is the display_size, so:

l = dd[2]

#is more appropriate

Anonymous   
Printed Page 426
4th paragraph

The fifth line of code reads:
for x not in init_modules:
This should be:
for m not in init_modules:

Anonymous   
Printed Page 539
routine _isPointInPolygon

Essentially routine does not check last line segment of the polygon. For a polygon with few vertices, such as a triangle, it is fairly easy to generate an example in which this routine identifies points outside the polygon as inside. The correction is to add a one more check in the routine after the loop with p=P[-1] and q=P[0] so that all connected pairs of polygon vertices are tested.

That is
"def _isPointInPolygon(r, P):
"Is point r inside a given polygon P?"

# We assume the polygon is a list of points, listed clockwise!
for i in xrange(len(P[:-1])):
p, q = P[i], P[i+1]
if not _isRightTurn((p, q, r)):
return 0 # Out!

return 1 # It's within!"

becomes

"def _isPointInPolygon(r, P):
"Is point r inside a given polygon P?"

# We assume the polygon is a list of points, listed clockwise!
for i in xrange(len(P[:-1])):
p, q = P[i], P[i+1]
if not _isRightTurn((p, q, r)):
return 0 # Out!
p = q
q = P[0]
if not _isRightTurn((p, q, r)):
return 0 # Out!

return 1 # It's within!"

Anonymous  Aug 15, 2014