The “Python Cookbook” is a book that brings the Python scripting language to O’Reilly’s popular “Cookbook” format. Each Cookbook provides a series of “Recipes” that teach users common techniques that can be used to become productive quickly and as a reference to those who might’ve forgotten how to do something.
I reviewed this book in the Mobi e-book format. Reading it on Kindle for PC, the Table of Contents only shows the major sections rather than the individual recipes and this made it harder to find what I was looking for. This is apparently a limitation of Kindle for PC, since my Kindle 3 and Kindle for Android had no such issue.
When I use an O’Reilly “Cookbook”, I judge it according to its’ usefulness: Can I become productive quickly? Is it easy to find what I need? Does it provide helpful tips? Does it teach me where to find the answers to my questions? Yes to all of the above.
This book is not targeted at new Python programmers, but that’s where I’m at. The best way for me to learn a new scripting language is to dive right in and try to write something useful, and that was my goal for the “Python Cookbook”. I also had “Learning Python” handy to cover any of the basics.
My first Python script was written to read in lists of subnets from two separate files and check that every subnet in list B was also in list A.
I used Recipe 13.3 to parse the command line options. Recipe 5.1 showed me how to read and write files. Recipe 2.11 taught me how to strip carriage returns out of my lines. Recipe 1.10, “Removing Duplicates from a Sequence while Maintaining Order”, was very helpful and I was able to reuse the code in my own script. Recipe 2.14, “Combining and Concatenating Strings”, helped me with my print statements. Considering this was the first Python script I ever wrote and that it ran, I consider both it and the “Python Cookbook” a success.
I had a bit more trouble with my second script. I was trying to write a script to find the subnet address given an interface address in CIDR notation. Recipe 11.4 introduced the ipaddress module, but this module refused to accept a string variable containing the interface in CIDR notation. I ended up installing another module (netaddr) I found via Google and things went better after that. I suspect the problem was that I was using ActivePython 126.96.36.199 [64 bit] and this book was written for Python 3.
As a DNS professional I was disappointed that there were no DNS-related recipes in the Network and Web Programming section, but Web-related topics were well-represented in the book.
The “Python Cookbook” doesn’t seem to have quite the depth and organization of the “Perl Cookbook” but I’m sure I will rely on it heavily as I learn to use Python. It did allow me to be productive very quickly and it passes the “Cookbook” standard with flying colors. Any book that can get me to the point of writing a working, useful script in less than a day is worth using. I recommend this book to anyone who has a basic understanding of Python and wants to get past “Hello, World” and “Eat Spam” as fast as possible.