Python Cookbook Edited by Alex Martelli, David Ascher The following corrections were made to the 10/03 reprint: Here's a key to the markup: [page-number]: serious technical mistake {page-number}: minor technical mistake : important language/formatting problem (page-number): language change or minor formatting problem [5] 3rd paragraph; tada = dodict(*data.items(), yellow=2, green=4) NOW READS: tada = dodict(yellow=2, green=4, *data.items()) [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] [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))] [98] 1st paragraph; In the Commands list of Recipe 3.19: r'X (?P\d),(?P.*)$' NOW READS: r'X (?P\d+),(?P.*)$' [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) [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 [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("<", "<") s = s.replace(">", ">") Line 37 previously read: s = 's' NOW READS: s = str(s) [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