Slashdot Mirror


The Python Cookbook

Nice2Cats writes "Python is something of a programmer's dream and an author's nightmare. What started life as a scripting tool for the Amoeba operating system has matured into a full-blown programming language with such speed that every book seems to be outdated in a year or two. To make matters worse for publishers, the crew around Python's creator Guido van Rossum keeps adding higher-level constructs such as iterators with every new release, reducing reams of code to single-line idioms at half-year intervals. Because not everybody has been able to keep up -- RedHat 7.3 infamously still ships with version 1.5.2 as the default, while SuSE 8.0 is hanging in there with version 2.2 -- authors are forced to cover stone age variants as well as modern forms. Python is cross-platform (Unix/Linux, Mac, Microsoft), has two underlying languages (C for Python, Java for Jython) and works with various GUIs (Tkinter, wxWindows, Qt, GTK, curses, Swing). Given this breadth of material, the idea of writing that most fragmented form of a programming book, a 'Cookbook,' seems as crazy as, say, nailing a dead parrot to its perch." Read Nice2Cats's review below of The Python Cookbook to see how well O'Reilly deals with dead parrots. The Python Cookbook author Alex Martelli and David Ascher pages 574 publisher O'Reilly rating 8 reviewer Nice2Cats ISBN 0596001673 summary A recommended book for the language with no Slashdot icon.

Beautiful plumage. O'Reilly, fortunately, has all kinds of experience with animals.

The Python Cookbook consists of seventeen chapters that contain between eight and twenty-six individual recipes. Chapters and recipes are roughly ordered by increasing complexity, length, and required background knowledge, starting with the simple "Swapping Values Without Using a Temporary Variable" and ending with the complete module "Parsing a String into a Date/Time Object Portably." The chapters are mostly organized by subject -- "Text," "Files," "Object-Orientated Programming," "User Interfaces" -- but also include "Python Shortcuts" and "System Administration." The background required varies: Whereas the chapter on "Text" starts off with Fred L. Drake reviewing the most basic string operations such as slicing and concatenation, Paul F. Dubois can only sketch the core concepts of lexing and parsing in "Programs About Programs."

This of course is a hallmark of all cookbooks, programming- or food-wise: Nobody will like everything, but everybody will like something. The worst fragmentation occurs, as expected, between examples of Python 1.5.2 and Python 2.2. Most recipes give preference to one version, and then point out how the problem could have been solved in the other version. This is more useful than the code that was written for all versions, because it gives a deeper insight into the changes that Python has gone through. The result is that after a few chapters, you start wondering why anybody in their right mind would keep using Python 1.5.2 instead of 2.2.* with its iterators, list comprehensions, new classes, and expanded module library.

Martelli and Ascher have done a good job balancing the different forms. Only one chapter struck me as lopsided: "System Administration", where ten of the sixteen recipes are Windows-only. Even though there is a good reason for this -- Microsoft's native administration tools just aren't like those provided with Unix -- the editors might want to rethink the selection of recipes in this chapter for future editions.

Generally helpful. The "Python Cookbook" has helped me in three ways. First, I found quite a lot of the examples themselves, especially those in the chapters "Python Shortcuts" and "Object-Orientated Programming" useful for everyday work. Second, reading more than 500 pages of peer-reviewed and well-commented code gave me a greater feeling for common idioms and constructs that are rare in this clarity in wild-type code. However, the book is strongest when more general principles of "Pythonic" programming are discussed, for example when Martelli demonstrates the merits of the "Look Before You Leap," "Easier to Ask Forgiveness than Permission," and "Homogenize Different Cases" methods.

My favorite recipe is Sebastien Keim's "Implementing a Ring Buffer," where an object carries a class deep in its bowels, and changes into this class in a rather cool Dr.-Jekyll-to-Mr.-Hyde transformation on the fly. The one recipe I found downright evil was "Sending HTML Mail," which should have been implemented as "Turning HTML Mail into Plain Text" with a note on how people who send HTML mail are going to be the first against the wall when the revolution comes. The best quote in the book comes from Tim Peters: "We read Knuth so you don't have to" -- Python's promise of programming power for the people, expressed in (dare I say it) a nutshell.

Conclusion:

I can recommend the "Python Cookbook" wholeheartedly to anyone who has passed into the advanced stage of language learning and is willing to actually sit down and work through the code. Anybody who is looking for a deeper understanding of Python, solutions to common coding problems, or starting points for their own projects will also profit. This book should have RedHat customers hammering at the gates of Raleigh, demanding the power of iterators and list comprehensions that their SuSE counterparts already enjoy by default; it demonstrates the superiority of Python 2.2.* over 1.5.2 in great detail.

Because of this, however, my guess is that 2.2.* will quickly replace 1.5.2, turning large parts of this book into historical footnotes in two years at the latest. This is no fault of O'Reilly's, but rather a current fact of Python life. The editors have done a good job of nailing the parrot, and until this Pythonic Norwegian Blue does the inevitable backflip, it should give its owner much pleasure.

You can purchase The Python Cookbook from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.

13 of 221 comments (clear)

  1. They don't eat pythons, do they? by Darth+RadaR · · Score: 5, Funny

    The title "Python Cookbook" has gotta look weird to people bopping around Barnes & Noble who aren't in the know. :)

    --
    /*drunk.. fix later*/
    1. Re:They don't eat pythons, do they? by wirefarm · · Score: 5, Funny

      I was at a bookstore years ago and this old preacher-type guy came up to me and started saying how pleased he was to see a young guy like me interested in religion -

      I didn't have the heart to tell the guy exactly what "Linux Bible, the Gnu Testament" was about...

      (Then again, I probably do as much preaching about Linux as he does about God - maybe we should get it declared a religion and get tax-free status...)

      Cheers,
      Jim in Tokyo

      --
      -- My Weblog.
  2. Alternative Cookbooks by WIAKywbfatw · · Score: 4, Funny

    And for those of you that can't get your hands on a python, the adder, asp, boa, cobra, diamondback, etc cookbooks are just as well packed with tasty recipes.

    --

    "Accept that some days you are the pigeon, and some days you are the statue." - David Brent, Wernham Hogg
    1. Re:Alternative Cookbooks by sielwolf · · Score: 5, Funny

      Huh, I looked on O'Reilly's website and I couldn't find the ASP or Corba Cookbooks any- oh wait...

      --
      What is music when you despise all sound?
  3. Darn... by Kierthos · · Score: 2, Funny

    And here I was hoping for the recipe for crunchy frog.

    Kierthos

    --
    Mr. Hu is not a ninja.
  4. Python cookbook by gowen · · Score: 2, Funny
    As Mrs Beeton might have written: First catch your python

    Or, as Homer might add: "...mmm...python"

    --
    Athletic Scholarships to universities make as much sense as academic scholarships to sports teams.
  5. Not sure about cooking up a Python, but... by qurob · · Score: 5, Funny


    Cornmeal Crusted Rattle Snake with Cactus-Corn Succotash

    Recipe courtesy Joey Altman, Copyright 2001

    2 1/2 pounds rattle snake, dead
    1 cup buttermilk
    1 cup cornmeal
    1 cup flour
    1 tablespoon salt
    1 tablespoon chile powder
    1 tablespoon garlic powder
    1 tablespoon paprika
    1 teaspoon cayenne pepper
    1 teaspoon ground cumin
    1 cup vegetable oil
    Cactus-Corn Succotash, recipe follows
    Using a sharp boning knife remove the meat from the snake by cutting down the back, just slightly to 1 side of the spine from the head to the rattle. Using the tip of the knife peel the meat from the ?rib cage?. Once you removed the 2 long strips of meat, lightly pound them with the back of the knife to tenderize them. Cut the strips of meat into 1-inch pieces and place in a bowl with the buttermilk. Mix to coat well. In a large bowl combine the cornmeal with the flour and the spices. Heat the oil in a large skillet on medium high heat. Dredge the snake pieces in the flour mixture and fry for 2 minutes or until golden brown and then transfer to a paper towel lined plate. Repeat until all the snake pieces are cooked. Serve with Cactus-Corn Succotash.

    Cactus-corn succotash:

    2 tablespoons olive oil
    1 cactus pad, thorns scraped off, cut into small dice
    2 ears corn, shucked
    1 red onion, peeled, sliced in rings, grilled with olive oil and chopped in small dice
    1 bunch scallions, grilled and chopped
    1 chayote squash, sliced 1/4-inch thick, grilled with olive oil and chopped in small dice
    1 tablespoon minced garlic
    2 tablespoons minced jalape?o
    1/2 cup diced red bell pepper
    4 tablespoons butter
    1 cup chicken stock
    1 cup diced, peeled and seeded tomatoes
    1/2 cup chopped cilantro
    Salt and pepper

    Grilling the vegetables first gives another great layer of flavor, however, it is not absolutely necessary. Just omit that step and cook the vegetable right in the pan. In a skillet on high heat saute the vegetables except the tomatoes in the olive oil for 2 minutes. Add the stock and butter and cook until mixture reduces by half. Add tomatoes and seasoning and serve with the warm snake ?nuggets? on top.

    Yield: 4 servings
    Prep Time: 30 minutes
    Cook Time: 10 minutes
    Difficulty: Medium

    1. Re:Not sure about cooking up a Python, but... by BlackBolt · · Score: 2, Funny
      The best part of the recipe is of course
      2 1/2 pounds rattle snake, dead
      Dead? I was planning on having 2 1/2 pounds of LIVE rattlesnake roaming around my kitchen. Of course, if the snakes were still alive, I don't think they could honestly give this recipe a
      Difficulty: Medium
      I would hope for at least
      Difficulty: Fatal
      But for me, Kraft Dinner is almost fatal.

      BlackBolt

  6. Python Cookbook? by loconet · · Score: 2, Funny


    Pythons? I heard they taste like chicken!

    --
    [alk]
  7. This is a fine book.... by nob · · Score: 3, Funny

    The perfect companion piece to Bake a Snake.

    --
    daed si luap
  8. It had to be said by Torgo's+Pizza · · Score: 3, Funny
    Read Nice2Cats's review below of The Python Cookbook to see how well O'Reilly deals with dead parrots.

    No no he's not dead, he's, he's restin'! Remarkable bird, the Norwegian Blue, idn'it, ay? Beautiful plumage!

  9. Re:Python icon by TeknoHog · · Score: 5, Funny
    > If someone pulls it off, they might become famous. ;-)

    Yeah, like the guy who drew Tux, whatshisname..

    --
    Escher was the first MC and Giger invented the HR department.
  10. Re:perl/python phrasebook by steveha · · Score: 3, Funny

    Thanks! You just made me laugh out loud, and that's always fun.

    Having to use a debugger at all is a sign that the code doesn't clearly express what it's going to do.

    No, it's a sign that the code isn't doing what you want it to do. Sometimes that's due to a flaw in your code. Sometimes it might even be due to something else, like an API call that doesn't do what you expect, or even an API call that is downright buggy.

    It's as silly to say that one should never need a debugger, as to say that one cannot do without a debugger.

    a debugger that can't even tell you which expression is about to be evaluated

    So, when you have a project on a platform with primitive development tools, what do you do? Refuse to work on the project?

    Tell me, what development tools do you use? They must be wonderful. Maybe I should use them.

    steveha

    --
    lf(1): it's like ls(1) but sorts filenames by extension, tersely