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 18
Bottom

The slice positions are wrongly calculated. Existing in Book: record = '....................100 .......513.25 ..........' cost = int(record[20:32]) * float(record[40:48]) SHARES = slice(20,32) PRICE = slice(40,48) Corrected One: record = '....................100 .......513.25 ..........' cost = int(record[20:23]) * float(record[31:38]) SHARES = slice(20,23) PRICE = slice(31,38)

Nehemiah Jacob  Apr 17, 2014 
PDF Page 24
Second code sample in Discussion

Missing right brackets at the end of the lines

Bill  Dec 11, 2014 
Printed Page 28
in the middle of the page

in the book, it shows: >>> list(compress(addresses, more5)) ['5800 E 58TH', '4801 N BROADWAY','1039 W GRANVILLE'] the correct result should be: >>> list(compress(addresses, more5)) ['5800 E 58TH', '1060 W ADDISON', '4801 N BROADWAY']

Tony Tang  Jul 19, 2016 
PDF Page 28
2nd code fragment

You should probably put a comma after '2122 N CLARK', because the output is quite confusing as it is now. If you do, make sure to adjust the following code fragment as well: >>> list(compress(addresses, more5)) ['5800 E 58TH', '4801 N BROADWAY', '1039 W GRANVILLE'] should be: >>> list(compress(addresses, more5)) ['5800 E 58TH', '1060 W ADDISON', '4801 N BROADWAY']

Anonymous  Jul 20, 2016 
PDF Page 83
4th paragraph == 2nd paragraph in "Solution"

The paragraph states: "When a value is exactly halfway between two choices, the behavior of round is to round to the nearest even digit. That is, values such as 1.5 or 2.5 both get rounded to 2.", but round(2.5) evaluates to 3.0 . (Using Python 2.6.9) The Python documentation for the round() function states: "... if two multiples are equally close, rounding is done away from 0 (so. for example, round(0.5) is 1.0 and round(-0.5) is -1.0)." (https://docs.python.org/2/library/functions.html#round)

Dirk Sonnemann  Jul 17, 2014 
Printed Page 108
3rd line of discussion

simply set the //days// attribute to 1 --> simply set the //day// attribute to 1

Anonymous  Aug 23, 2016 
Printed Page 134
5th paragraph of discussion

It is true that "itertools.chain()" would not work here, but "itertools.chain.from_iterable()" would do the trick!

Marco Paolieri  Jun 26, 2014 
PDF Page 137
second code example

The second code example shows: with open('sorted_file_1', 'rt') as file1, \ open('sorted_file_2') 'rt' as file2, \ open('merged_file', 'wt') as outf: The second open statement should read as follows: open('sorted_file_2', 'rt') as file2, \

MacBent  Jan 23, 2016 
Printed Page 152
the 8th and 10th line of second in-line example

In 8th and 10th line, b'Hello' misspells as b'Hallo'.

Fei Chen  Dec 17, 2016 
Printed Page 182
at bottom of the page

In the book, the function is implemented as: def unserialize_object(d): clsname = d.pop('__classname__', None) if clsname: cls = classes[clsname] obj = cls.__new__(cls) for key, value in d.items(): setattr(obj, key, value) return obj else: return d The above code actually will error out. It should be defined as: def unserialize_object(d): clsname = d.pop('__classname__', None) if clsname: cls = classes[clsname] obj = cls.__new__(cls) for key, value in d.items(): setattr(obj, key, value) return obj else: return d The downloaded code is implemented correctly

tony tang  Aug 04, 2016 
Printed Page 196
5th par. beginning 'To perform a query.'

The following code ... >>> for row in db.execute('select * from portfolio'): ... the word 'db' should be replaced by 'c'. ('execute' is a method of class cursor, not the class connect). The rest of the page uses 'db' for a connect and 'c' for a cursor. The same error occurs again in the next paragraph.

Charles Jardine  Feb 06, 2017 
Printed Page 251
last paragraph of Discussion

You say: (i.e., the use of a leading underscore could be interpreted as a way to avoid name collision rather than as an indication that the value is private). In the context of the example I think what you wanted to say was the opposite: (i.e, the use of a leading underscore could be interpreted as an indication that the value is private rather than as a way to avoid name collision).

Thomas Geppert  May 07, 2014 
Printed Page 252
1st snippet

In the class Person constructor definition, the self.first_name attribute affectation should contain an underscrore as first_name parameter is setting an underlaying private attribute of the class.

eric regnier  Aug 16, 2017 
Printed Page 335
Function decorate

Should be logname = name if name is not None else func.__module__ ... log msg = message if message is not None else func.__name__ In an earlier chapter you discuss how important it is to check for None using, for example, "if x is None" instead of "if not x"

Siva Kumar  Oct 31, 2017 
Printed Page 382
Solution

class Person: def __init__(self, name, age): self.name = name self.age = age should be class Person: def __init__(self, name, age): self._name = name self._age = age the underscores for the private class variables went missing (as the @property @name/age.setter use _name and _age).

Anonymous  Apr 25, 2014 
PDF Page 476
method EventHandler.fileno

The method does: raise NotImplemented('must implement') But it should instead: raise NotImplementedError('must implement') because NotImplemented is not an exception, but a special value used when overloading operators, and NotImplemented is not callable, so the method as implemented actually raises a different exception: TypeError: 'NotImplementedType' object is not callable

Luciano Ramalho  Oct 25, 2014 
Printed Page 487
CountdownThread class snippet

In the CountdownThread class initialization, self.n is assigned to 0, instead of passing n argument: class CountdownThread(Thread): def __init__(self, n): super().__init__() self.n = n # instead of self.n = 0

Jesus ALkorta  Dec 13, 2014 
PDF Page 506
second code fragment

For this to become a proper server and an exact functional equivalent of the first example, the code in echo_client(q) needs to be wrapped in 'while True:' Otherwise, the server will only process the first 'nworkers' clients and then just hang there doing nothing, because all the worker threads will have exited by then.

Anonymous  Jul 20, 2016 
ePub Page 32801
Chapter 6, Data Encoding and Processing - final example in 'discussion' of CSV data (location above is from the Kindle version)

Text states: Alternatively, here is an example of converting selected fields of dictionaries: print('Reading as dicts with type conversion') field_types = [ ('Price', float), ('Change', float), ('Volume', int) ] with open('stocks.csv') as f: for row in csv.DictReader(f): row.update((key, conversion(row[key])) for key, conversion in field_types) print(row) This produces a 'cannot convert string to float' error when I attempt it in either Python 2.X or 3.X

Anonymous  Dec 28, 2014