The Occasional Occurence

Book Review: CherryPy Essentials

May 11, 2007 at 06:03 AM | categories: Python, cherrypy, General

`CherryPy Essentials <http://www.cherrypyessentials.com/>`_ Author: Sylvain Hellegouarch Publisher: Packt Publishing Pages: 257

Introduction The title CherryPy Essentials is a bit of a misnomer, as this book covers far more than the bare essentials of CherryPy 3. Admittedly, a book on simply the essentials would be little more than a leaflet, as CherryPy is very easy to understand and be productive with. In this book, which has two main sections, the author covers CherryPy specifics in the first four chapters and then delves into a host of other web-related Python libraries in the remaining chapters. The use of those libraries is tied together with CherryPy through the construction of an example "photo blog" application. Here follows a more in depth look at these two main sections and their strengths and weaknesses.

Chapters 1-4: Pure CherryPy From how to acquire and install CherryPy to creating custom "Tools" to extend it, the first four chapters cover various topics. Some of the content is geared at beginners and some is geared at the seasoned CherryPy developer. There's something for everyone.

The first chapter covers the history and rationale behind the project. While it is interesting, it does not delve into anything that you need to know to get up and running with CherryPy. Newcomers to CherryPy might want to give it a glance, but people who have been using it for a while don't really need to bother.

The second chapter covers installation. Those of you who have installed Python packages won't find any surprises here. For the beginners, the content is excellent. Three methods of installation (source tarball, easy_install, Subversion) are covered in nice detail. Definitely go over this chapter if you are new to Python in general or if you would like to learn more about any of those installation methods.

Things really get going once you hit chapter three. After a brief intro, it starts off with source code for a demo "note taking" application. The code is well documented and should be fairly easy to digest. It makes use of a number of basic features of CherryPy and the author refers back to the code throughout the chapter to highlight specific features. Highlights include configuring your application and the builtin HTTP server, how the object publishing system works, handling exceptions and customizing errors, and overviews of the various utility modules that CherryPy is built upon (but also exposes to the developer!).

One thing that could be slightly confusing in this and subsequent chapters is the use of the term "CherryPy engine". The author uses it in reference to CherryPy itself, but CherryPy has a distinct "engine" (also introduced in the chapter) with specific functionality (process and signal control, generating Request objects, etc). The use of the term in the context of CherryPy-at-large could lead to some confusion - but now you know, and knowing is half the battle.

Chapter four goes even deeper into the inner workings of CherryPy and the power that is available to the developer. The various URL dispatchers (HTTP method, Routes and virtual hosting) included with the framework are introduced with usage examples. Although it is mentioned that there is a "simple way" to create your own dispatchers, there is no content related to actually creating one.

Hooking into the core request/response processing system is covered in this chapter. The various hook points and how to attach callbacks to them is described. The "Tool" interface which makes working with the various hook points much friendlier is also introduced. All of the builtin Tools are covered, including expected parameters, example code and a description of what the Tool accomplishes. This section is reminiscent of the Python Cookbook in its approach and is one of the really useful references in the book. The coverage of Tools winds up with a section on creating your own custom Tool.

One slight quirk about the section on Tools is that the Tools for serving static content are discussed separately from the other tools. They are introduced after the section on creating a custom Tool. Not a huge deal, but an interesting decision. Perhaps it was done that way because serving static content is covered in greater detail than the other Tools.

Chapter four closes with a section on the WSGI support that CherryPy provides. Examples of hosting WSGI applications using CherryPy as well as hosting a CherryPy application within a third-party WSGI server are presented. The diagram of WSGI leaves a little to be desired, but the content of this section is very good otherwise.

I think that the first four chapters are what most people have been looking for in a CherryPy book. These are the Essentials as referenced in the title, in my opinion. But the book is just getting started...

Chapters 5-10: Developing a Web Application with CherryPy Chapter five kicks off the rest of the book, which is a sort of case-study covering the creation of a "photoblog application". The "model" of the photoblog is introduced in this chapter, as well as three different Python object-relational mappers (ORMs): SQLObject, SQLAlchemy and Dejavu. Excellent usage examples are presented for each ORM, but oddly enough the examples are not geared to the photoblog but to an imaginary music cataloging system or something.

Though the three ORMs are introduced, only one is chosen to be used with the photoblog. The lucky ORM is Dejavu. Some might question this choice, as it is not one of the more popular ORMs (at least compared to SQLObject and SQLAlchemy), but I think it was a good decision. Dejavu has a solid design and compares well with other ORMs. Whichever ORM you prefer, the Dejavu code should be easy enough to follow and you could follow along with your favorite ORM if you like.

After the ORM is settled on, the topic of the application as a web service is covered in chapter six. Two methods of exposing the application's API are described: REpresentational State Transfer (REST) and the Atom Publishing Protocol (APP). The description of the principles of REST is good and the photoblog API implementation using REST and the MethodDispatcher helps to solidify the concepts. APP is also covered with good introductory detail.

The user interface for the photoblog is covered in chapter seven. Like the database/ORM layer, CherryPy can work with any templating language, but the decision was made to use Kid in the book. A custom Tool for rendering Kid templates is introduced in the chapter and it is a nice example of a simple but worthwhile Tool. The MochiKit Javascript library is also introduced in this chapter. While I am personally a fan of MochiKit, the only problem that I have with its use in the book is that the latest stable release is quite dated. Developers wanting to follow along with the book will have to fetch the latest revision from the MochiKit subversion repository.

Ajax, the darling of the web, is presented in chapter eight. The chapter includes some history and a description of Ajax in general. MochiKit handles the client-side while CherryPy (of course) and the simplejson library are used to generate JSON output on the server-side. Some knowledge of Javascript is expected in this chapter, but the decisions that are made in the photoblog application are covered in great detail. One particularly interesting topic that is covered in the chapter is Basic/Digest authentication with XMLHttpRequest objects. A form-based login page that does Basic/Digest auth "under the covers" is presented complete with code.

Chapter 9 offers excellent coverage of testing your CherryPy applications. Unit, functional and load testing are all covered in this chapter, with copious examples and descriptions. The unittest and doctest standard library modules are introduced, as well as the webtest extension to unittest provided by CherryPy specifically for testing web applications. For functional testing, Selenium is demonstrated. It is a top-notch testing tool that is invaluable for testing Ajax applications. I usually do crude load testing with ab, but the FunkLoad tool introduced in this chapter is very interesting.

The final chapter in the book delves into deploying your CherryPy applications. Configuration, some of the various deployment methods (mod_rewrite, mod_python, etc) and targets (Apache, lighttpd) are covered in good detail. Surprisingly (to me at least) deployment using mod_proxy was not covered. I find this to be the best deployment option for a small site. Thankfully, there are good online docs for that deployment method.

Conclusions As I mentioned in the introduction, the title CherryPy Essentials is misleading - this book is much more than that. It gives a good picture of the power and flexibility of CherryPy and introduces the reader to a number of good libraries and tools that are useful for web development.

A well done index follows-up the meat of the book, which is always important in this type of book which you are likely to refer back to for various topics.

The only fairly large disappointment to me was the poor quality of the copy editing. The editors let too many awkward phrases slip through. Thankfully, the content of the book more than makes up for that shortcoming. Kudos to the author.

If you are looking to expand your knowledge of CherryPy in particular or web development using Python in general, I think that you would do well reading CherryPy Essentials.

cw

Disclaimer: Packt sent me a copy of the book in exchange for doing a review. I already had a copy, so I gave it away. I am also involved with the CherryPy project, so I am somewhat biased.