Book Review: CherryPy Essentials

Originally published on my blog, 10 May 2007

A little over a week ago I received a review copy of Sylvain
Hellegouarch’s new book, CherryPy Essentials, published through
Packt Publishing. The timing couldn’t have been better, since we
have begun investigating Python web application frameworks at work for
a new project. From previous work I have done with TurboGears, I knew
that CherryPy was a contender, so I was definitely interested to see
what version 3 had to offer. Sylvain’s book is is a good starting
point for the information I wanted.

Review

CherryPy Essentials is a fairly short book (251 pages), especially
given the breadth of topics it covers. It could easily have been 2-3
times as long, if the author was wordy or repetitive, but the concise
writing style means that there is a lot of good information packed
into this short volume.

The outline is fairly typical for tech books:

  • What is this thing and why do I care? – chapter 1
  • How do I get it? – chapter 2
  • What can it do? – chapters 3-4
  • Build an example app – chapters 5-7
  • Advanced topics – chapters 8-10

The real substance of the book begins with chapter 3, which gives an
overview of CherryPy. It includes a moderately sized introductory
application which lets different authors post notes on a page. The
sample code includes embedded comments and is used as a basis for a
brief description of how CherryPy decides what to do when an HTTP
request is received.

CherryPy comes with a host of modules to make building your
application easier. The coverage given these modules in the “Library”
section of chapter 3 probably does not do them justice. The author
clearly states that his intent is not to create a reference guide, but
I would have liked to see this section pulled out into its own chapter
and expanded, possibly combined with the Tools list in chapter 4.

The more in-depth discussion of CherryPy in chapter 4 includes
instructions for running multiple HTTP servers; various mechanisms for
dispatching URLs to Python functions; serving static content; hooking
into the core to add your own middleware (or “Tools” in
CherryPy-parlance); and WSGI support. The same chapter also includes a
description and concise example of how to use each Tool provided in
the CherryPy core distribution, following a format not unlike the one
I use for my Python Module of the Week series.

Chapters 5-7 discuss the design and development of a photo blog
application, which is small enough for the reader to follow but large
enough to delve into the details of how to build a real application
with CherryPy. The presentation begins with the data model, then
covers web services and user interface topics.

Some of chapter 5, which discusses working with databases, is
unfocused and includes sections on topics such as backgrounds on
database types and object-relational mapping libraries not actually
used in the example applications. This material could have been
eliminated without serious loss. It is interesting, but it detracts a
bit from the overall coherence of this book. Once he selects the
Dejavu ORM, the discussion refocuses and covers the mechanics of using
that library to store and retrieve data.

Chapter 6 provides an excellent discussion of web services, REST, URL
design, and the Atom publishing protocol. These were perhaps the most
interesting sections in the book. The author clearly has a great deal
of experience to share on these topics. I hope there is another book
coming soon with a greater examination of these topics.

The coverage of presentation layer topics in chapter 7 begins with a
brief history of HTML, up to the development of DHTML. Then the Kid
templating language is introduced (thankfully without a comprehensive
survey of all available Python templating languages :-). The UI for
the example application is fairly simple, so only basic Kid features
are really covered, but that is enough. The more complex DHTML work is
handled by Mochikit, introduced here and used more extensively in
chapter 8, which is devoted to Ajax.

The last 2 chapters of the book cover topics too frequently left out
of other books: Testing and Deployment. The chapter on testing
presents several tools which integrate well with CherryPy for
automated testing of different aspects of your application, including
webtest for unit testing, FunkLoad for load testing, and Selenium for
UI testing. I had never seen these tools before, and the descriptions
and examples were enough to make me add them to my “to be researched”
list.

The final chapter covers deployment options for moving a CherryPy app
into production. Options for using Apache, lighthttpd, WSGI, and SSL
are covered, but no definitive “best practice” is suggested. I suppose
the final choice should be made on more variables than could really be
covered, but I would have liked to see clear guidelines for making a
decision about which configuration to use in different situations.

Third-party Tools

Any good, modern, open source project does not stand alone, and
CherryPy is no exception. “CherryPy Essentials” makes it clear that
integrating with third-party tools is an important part of the design
for CherryPy. Tools covered include:

Other tools are also mentioned, but covered in less detail.

Conclusion

CherryPy Essentials packs a surprising amount of information into a
small space. The coverage is not exceptionally deep in any one area,
but is fairly complete. The sample code is consistent and easy to
read. The book is full of useful and interesting information and,
while it occasionally suffers from disjoint flow, I can definitely
recommend it to any Python programmer interested in the future of
CherryPy development, and web technologies in general.

Special thanks to Ms. PyMOTW for proof reading this post for me.

PyMOTW: linecache

The linecache module is used extensively throughout the Python
standard library when dealing with Python source files. The
implementation of the cache simply holds the contents of files, parsed
into separate lines, in a dictionary in memory. The API returns the
requested line(s) by indexing into a list. The time savings is from
(repeatedly) reading the file and parsing lines to find the one
desired. This is especially useful when looking for multiple lines
from the same file, such as when producing a traceback for an error
report.

Read more at pymotw.com: linecache

codehosting now supports feedburner

I just posted a new version of my codehosting project for django
which supports passing the Atom feeds for release updates through
feedburner.com. There isn’t anything tying the implementation to
FeedBurner, of course, but since that’s why I wanted the feature that’s
how I am describing it.

One tricky bit was I wanted all of the existing subscribers to my
feed(s) to be redirected to the FeedBurner URL. I couldn’t just add a
redirect rule in Apache, since not all of the feeds are set up with
FeedBurner yet. So I opted for letting the django code handle the
redirection. If a project has an external_feed property that is not
null, that value is used as the URL for feeds for the project. So when
someone accesses the old URL for the codehosting release feed
(http://www.doughellmann.com/projects/feed/atom/codehosting/) they are
redirected to http://feeds.feedburner.com/DougHellmann-codehosting
instead. And FeedBurner looks at
http://www.doughellmann.com/projects/local_feed/atom/codehosting/,
which always produces the Atom content locally.

The “local_feed” URL is never included in any templates, so no web
crawlers should ever find it by themselves.

This is one of those cases where I had thought to include this feature
from the beginning, since migrating the existing readers of the feed(s)
required this hackish change. But, it looks like it is working. I would
be interested in any feedback anyone else might have on other ways I
could have handled the redirects.

PyMOTW: StringIO and cStringIO

The StringIO class provides a convenient means of working with text
in-memory using the file API (read, write. etc.). There are 2 separate
implementations. The cStringIO module is written in C for speed, while
the StringIO module is written in Python for portability. Using
cStringIO to build large strings can offer performance savings over some
other string conctatenation techniques.

Read more at pymotw.com: StringIO

PyMOTW: Queue

The Queue module provides a FIFO implementation suitable for
multi-threaded programming. It can be used to pass messages or other
data between producer and consumer threads safely. Locking is handled
for the caller, so it is simple to have as many threads as you want
working with the same Queue instance. A Queue’s size (number of
elements) may be restricted to throttle memory usage or processing.

Read more at pymotw.com: Queue