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. If the error was corrected in a later version or reprint the date of the correction will be displayed in the column titled "Date Corrected".

The following errata were submitted by our customers and approved as valid errors by the author or editor.

Color Key: Serious Technical Mistake Minor Technical Mistake Language or formatting error Typo Question Note Update



Version Location Description Submitted By Date Submitted Date Corrected
Printed
Page xxvi
third line from page bottom

http://www.oreilly.com/cgi-bin/errata.form/python_cook NOW READS: http://www.oreilly.com/cgi-bin/errata.form/9780596001674

Anonymous    Oct 01, 2004
Printed
Page 3
first paragraph

def method(self, argument, *args, **kw): # do something with argument apply(callable, args, kw) NOW READS: def method(self, argument, *args, **kw): # do something with argument callable(*args, **kw) Also - def method(self, argument, *args, **kw): # do something with argument apply(callable_object, args, kw) NOW READS: def method(self, argument, *args, **kw): # do something with argument callable_object(*args, **kw)

Anonymous    Mar 01, 2004
Printed
Page 5
3rd paragraph

tada = dodict(*data.items(), yellow=2, green=4) NOW READS: tada = dodict(yellow=2, green=4, *data.items())

Anonymous    Oct 01, 2003
Printed
Page 7
sect 1.4

example provided: theIndex={} def addWord(word, pageNumber): try: theIndex[word].append(pageNumber) except AttributeError: theIndex[word]=[pageNumber] NOW READS: theIndex={} def addWord(word, pageNumber): try: theIndex[word].append(pageNumber) except KeyError: theIndex[word]=[pageNumber]

Anonymous    Oct 01, 2003
Printed
Page 50
in byItem and byAttribute

The code in the list comp definition of aux in byItem and byAttribute in the Sorter class had a bug. byItem was: aux = [(d[i][itemindex], i) for i in range(len(data))] NOW READS: aux = [(data[i][itemindex], i) for i in range(len(data))] Likewise byAttribute was: aux = [(getattr(d[i],attributename),i) for i in range(len(data))] NOW READS: aux = [(getattr(data[i],attributename),i) for i inange(len(data))]

Anonymous    Oct 01, 2003
Printed
Page 66
3rd paragraph

htmilib NOW READS: htmllib

Anonymous    Mar 01, 2004
Printed
Page 98
1st paragraph

In the Commands list of Recipe 3.19: r'X (?P<num>d),(?P<name>.*)$' NOW READS: r'X (?P<num>d+),(?P<name>.*)$'

Anonymous    Oct 01, 2003
Printed
Page 98
1st paragrap, seciond half of code block

# Prepare the Commands list for exeution by compiling each re for cmd in Commands: NOW READS: # Prepare the Commands list for exeution by compiling each re for cmd in Commands: Also, the try/except block that follows HAS BEEN INDENTED by 4 spaces.

Anonymous    Mar 01, 2004
Printed
Page 145
2nd paragraph

The second paragraph reads: The file patterns are case-insensitive The is only true if your os handles files this way. See the docs on fnmatch: http://www.python.org/doc/current/lib/module-fnmatch.html To get case-insensitivity on UNIX, you need the pattern '*.html' to be '*.[hH][tT][mM][lL]' Python 2.2.2/Solaris

Anonymous   
Printed
Page 169
all 5 recipes

for base in self__class__.__bases__: NOW READS: for base in self.__class__.__bases__: (The pages affected by this repeated change are 169 to 171, all examples).

Anonymous    Mar 01, 2004
Printed
Page 200
The "def union" code example, the following error was discovered

Section 5.17 gave a Set class that used an internal dictionary, _dict, to store the set. At the end of the section was: def union(s1, s2): import copy result = copy.copy(s1) for item in s2: result.add(item) return result The idea behind this union function is great, but the implementation of the Set class on the prior pages has no special support for __copy__, and thus the _dict attribute is copied shallowly to the new object. Because of this the "result.add(item)" line ended up changing both the copy and the original instead of just the copy. To correct this problem, the following lines of code were ADDED to the Set class on page 199: def __copy__(self): return Set(self)

Anonymous    Oct 01, 2003
Printed
Page 201
In definition of RingBuffer class

self.__class__ = __Full NOW READS: self.__class__ = self.__Full

Anonymous    Mar 01, 2004
Printed
Page 257
7.13 Impersonating Principles on Windows

The code detailed within is problematic. It will work only if certain criteria is met first. This presents a problem if the end user wants to use it on locked down NT based system (Win2k and XP). I spent several days on the Python-List requesting help and from Mark Hammond. Finally after someone pointed out the possibility I needed the below right first, was I able to make this function work. I have sent this comment to Active-State on this code and to you all as well so someone will not have to pull their hair out as I have done trying to get it to work. It is a VERY useful routine for our remote locations to install printer drivers or vendor supplied programs and is now working great. Please add an addendum so others will not have to go through the same basic research again to discover how to get it to work on a locked down system. In order to use it on these systems, one base requirement MUST be met. The "Act as Operating System" privilege has to enabled in the system policy(run secpol.msc, select Local Policies, User Rights Assignments). Once this is set, then the programmer needs to add some additional code and call it prior to attempting to login as the impersonated user. Example: def AdjustPrivilege(priv, enable = 1): # Get the process token. print priv flags = TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY #flags= TOKEN_QUERY htoken = win32security.OpenProcessToken(win32api.GetCurrentProcess(), flags) # Get the ID for the privilege. id = win32security.LookupPrivilegeValue(None, priv) # Now obtain the privilege for this process. # Create a list of the privileges to be added. if enable: newPrivileges = [(id, SE_PRIVILEGE_ENABLED)] else: newPrivileges = [(id, 0)] # and make the adjustment. try: win32security.AdjustTokenPrivileges(htoken, 0, newPrivileges) except: fail.append(priv) # call routine to set privileges. This goes after the call to main. AdjustPrivilege(SE_CHANGE_NOTIFY_NAME) AdjustPrivilege(SE_TCB_NAME) AdjustPrivilege(SE_ASSIGNPRIMARYTOKEN_NAME) # Then the login try: a.login EDITOR: The reporter is correct.

Anonymous   
Printed
Page 272

1st code sample; The last two lines of code previously read: reloaded = cPickle.loads(save) assert saved.stuff == reloaded.stuff NOW READ: reloaded = cPickle.loads(saved) assert anInstance.stuff == reloaded.stuff

Anonymous    Oct 01, 2003
Printed
Page 276
Title of Section 8.5

"8.5 Accesssing a MySQL Database" NOW READS "8.5 Accessing a MySQL Database"

Anonymous    Oct 01, 2004
Printed
Page 309
4th paragraph: the example code that does open("icon.gif") etc.

print "icon='''\ " + base64.encodestring(open("icon.gif").read()) + "'''" NOW READS: print "icon='''\ " + base64.encodestring(open("icon.gif", "rb").read()) + "'''"

Anonymous    Mar 01, 2004
Printed
Page 319
2nd Paragraph, 3rd line

(fresh from the CSV... NOW READS: (fresh from the CVS...

Anonymous    Oct 01, 2004
Printed
Page 339

try: ftp.login(username, password) except ftp.all_errors, error: print 'Cannot login:', error The second line NOW READS: except ftplib.all_errors, error:

Anonymous    Mar 01, 2004
Printed
Page 362
code

"Content-Type: text/plain" NOW READS: "Content-Type: text/html"

Anonymous    Oct 01, 2004
Printed
Page 378

The Xerox PARC Map Viewer Service (http://pubweb.parc.xerox.com/) used in this recipe is no longer maintained.

Anonymous   
Printed
Page 383
1st paragraph

for tag in tags: print tag, handler.tags[tag] The second line HAS BEEN INDENTED.

Anonymous    Mar 01, 2004
Printed
Page 389
IN PRINT: Solution: 5th line of program

.replace(''', """) NOW READS: .replace('"', """)

Anonymous    Mar 01, 2004
Printed
Page 401
Example 12-2 (in Recipe 12.12)

Lines 30 and 31 previously read: s = s.replace("<", "<") s = s.replace(">", ">") NOW READ: s = s.replace("<", "&lt;") s = s.replace(">", "&gt;") Line 37 previously read: s = 's' NOW READS: s = str(s)

Anonymous    Oct 01, 2003
Printed
Page 411
3rd paragraph, last line

xmlprc_handler NOW READS: xmlrpc_handler

Anonymous    Oct 01, 2004
Printed
Page 426
4th paragraph, the fifth line of code

for x not in init_modules: NOW READS: for m not in init_modules:

Anonymous    Mar 01, 2004
Printed
Page 435
2nd paragraph of "discussion"

"sizecustomize.py file" NOW READS "sitecustomize.py"

Anonymous    Oct 01, 2004
Printed
Page 533
Lines 8, 10, 11

print a.pop(0) NOW READS: print a.pop()

Anonymous    Mar 01, 2004
Printed
Page 534
Solution

The code following the "if __name__ == '__main__':" has been REPLACED by the following: if __name__ == '__main__': a = PriorityQueue() a.insert('L', 5) a.insert('E', 4) a.insert('L', 5) a.insert('O', 8) a.insert('H', 1) for i in range(5): print a.pop(), print

Anonymous    Oct 01, 2003