The sched module implements a generic event scheduler for running
tasks at specific times.
PyMOTW: timeit
The timeit module provides a simple interface for determining the
execution time of small bits of Python code. It uses a platform-specific
time function to provide the most accurate time calculation possible.
PyATL Book Club on O’Reilly
Marsee Henon from O’Reilly recently interviewed a couple of us
about the Atlanta-area Python Users’ Group Book Club. She asked some
good questions, and although I’m not entirely comfortable with the group
being characterized as an “O’Reilly” book club, O’Reilly does offer us a
lot of support (esp. free books) so I guess I shouldn’t complain.
If you’re interested in participating in a technical book club, you
don’t have to live in or even near, Atlanta to join ours. Head over to
http://pyatl.org/bookclub and join the Google Group we have set up for
our discussions.
PyMOTW: hmac
The HMAC algorithm can be used to verify the integrity of information
passed between applications or stored in a potentially vulnerable
location. The basic idea is to generate a cryptographic hash of the
actual data combined with a shared secret key. The resulting hash can
then be used to check the transmitted or stored message to determine a
level of trust, without transmitting the secret key.
Book Review: RESTful Web Services
As part of the Atlanta Python Users’ Group Book Club, I received a
copy of RESTful Web Services written by Leonard Richardson and Sam
Ruby, and published by O’Reilly Media. When we started the book club,
this was the first book I suggested we read. I had previously studied
some material on various blogs discussing REST, but I wanted a clear
description and more specific examples. The book provided both, and I
highly recommend reading it before planning your next web development
project.
Overview
Unlike many such books, RESTful does not depend on a single
programming language for examples. Much of the code is Ruby, but Python
and Java make up a respectable proportion of the material as well. Since
I was primarily interested in the design principles and “theory”, I did
not try to run any of the sample code myself. Others in the book club
have, so check the forum for more details if you are interested in
that aspect of the book.
The outline of the book follows a well thought-out progression of
topics from basic “programmable web” concepts to in-depth discussion of
Roy Fielding’s Representational State Transfer (REST) ideas and then
Resource Oriented Architecture (ROA), a natural extension of REST.
Intermediate chapters include discussions of best-of-breed tools for web
development and copious example code.
Outline
Chapter 1 is a foundation chapter for the remainder of the book. It
describes how the HTTP protocol works and breaks down the different
architectural styles discussed in the remaining chapters (REST, RPC, and
REST-RPC hybrid). The theme of this chapter, and perhaps the entire
book, is that “the human web is on the programmable web”. If something
is on the web, it is a service.
Chapter 2 introduces the concepts necessary to implement clients using
web services. The easily digestible example code (in several languages)
implements a client for the del.icio.us bookmarking service.
Bookmarks are an excellent choice for an example program, since the
information being managed is straightforward and everyone understands
the concept, even if they have never used del.icio.us directly. Chapter
2 also includes recommendations for client tools and libraries for
common languages. Basic HTTP access, JSON parsers, XML parsers
(including details about DOM, SAX, and pull-style parsers and when each
is appropriate), and WADL libraries are discussed, with best-of-breed
options presented for each language.
In chapter 3, the authors use Amazon’s S3 service design to point out
features of the REST architecture which make it different from RPC-style
APIs. The complexity of the examples increases to match the requirements
of the service, including advanced authentication techniques.
Resource Oriented Architecture, introduced in Chapter 4 and discussed
in an extended design example used through chapters 5 and 6, is perhaps
the most interesting part of the book. ROA is a set of design principles
which encourage you to think about your service in a specific way to
enable REST APIs. The principles are:
- Descriptive URIs
- URIs should convey meaning
- Addressibility
- Expose all information via URLs
- Statelessness
- The client maintains the application state so the server does not
have to. - Representations
- Resources can have multiple representations, based on format, level
of detail, language, and other criteria - Connectedness
- Link between related resources explicitly within the
representations, so the client does not have to know how to build
URLs - Uniform Interface
- Use the HTTP methods (GET, PUT, POST, DELETE) as designed
To illustrate these principles, in chapters 5 and 6 the authors build
a web mapping service, similar to Google Maps. This detailed example
also serves as a way to introduce their ROA development process.
- Identify the data set to be managed by the service.
- Split up that data into resources.
- Name each resource with a URI.
- Expose a subset of the uniform interface for each resource,
depending on what makes sense and what features are to be supported. - Design representations to be passed from client to server.
- Design representations to be passed from server to client.
- Include links to other resources.
- Consider a typical course of events, to ensure completeness.
- Consider error conditions, to identify the HTTP response codes to be
used.
Chapter 7 includes the implementation of a bookmarking service similar
to del.icio.us. The sample code uses Ruby extensively, and it was a
little more advanced than what I was prepared to absorb without a Ruby
primer. One important point made in the prose of the chapter is that
code frameworks may constrain your design by making certain choices for
implementation easier or harder.
Chapter 8 is a summary of the REST and ROA principles discussed in the
earlier chapters, and is an excellent reference once you’ve finished
reading the whole book. It is also suitable as a “Cliff’s Notes” version
of the material, if you don’t have time to read everything. If you want
to review the book before reading it, go to the book store and take a
look at this chapter.
While chapter 2 covered client implementation techniques, chapter 9 is
a survey of tools and aspects of web service implementations in
different languages. It covers topics such as XHTML, HTML5,
micro-formats, Atom, JSON, RDF, control flow patterns, and WSDL.
In chapter 10, the authors give an extensive comparison of ROA and
“Big Web Services” to argue that ROA is simpler, requires fewer tools,
and can even be more interoperable.
Chapter 11 is the requisite “How to use this with AJAX” chapter.
And the book wraps up in chapter 12 with a discussion of frameworks
for doing RESTful development in multiple languages. The coverage of
Django includes a dispatcher that decides how to handle the request
based on the HTTP method invoked.
Conclusion
Before reading “RESTful Web Services”, I had a somewhat cloudy notion
of REST and how to apply it. The book clarified what REST is and how to
apply it. It also offered an invaluable concrete process to follow when
implementing a web service using REST and ROA principles. I expect my
copy to see a lot of use and become dog-eared as I refer back to it
frequently.
PyATL Book Club
The Atlanta Python Users’ Group runs an online book club. We encourage
all Atlanta area Python developers to check the schedule on
PyATL.org and come down to a meeting. Anyone is free to join and
participate. For more reviews by members of the book club, check out
the forums or our Reviews List.
PyMOTW: unittest
Python’s unittest module, sometimes referred to as PyUnit, is
based on the XUnit framework design by Kent Beck and Erich Gamma.
The same pattern is repeated in many other languages, including C, perl,
Java, and Smalltalk. The framework implemented by unittest supports
fixtures, test suites, and a test runner to enable automated testing for
your code.
django-links
I spent some time yesterday afternoon working on my personal site. I
wanted a way to add “related links” for my projects, without
hand-editing individual lists in HTML. That’s boring, after all.
Surprisingly, I didn’t find an existing django app that did anything
like that, and had to build one myself.
My requirements were pretty simple: My site uses codehosting to
host project home pages, including file release management, and RSS
feeds for updates. I wanted to include related links on the project
description pages, so they could link to discussions of the projects on
my blog (or other sites).
Rejected Solutions
My first attempt at solving the problem was to modify codehosting to
manage the links directly. That worked OK, but felt wrong. I knew I was
going to want to have links for other purposes on another, as yet
undeveloped, part of the site, and having to manage the links separately
would be a pain.
I prototyped a version using JavaScript to render an RSS feed from
del.icio.us, but didn’t like the results. I wanted to embed the links
instead of requiring a separate iframe. Next, I briefly considered
building something combining del.icio.us feeds and feedcache, which
would fetch the feeds when the project page was hit, but in-line the
results as the template was being rendered. It seemed like (potentially)
loading the feed each time anyone hit my projects pages would hurt site
response time too much. Storing the links in the database seemed like
the best way to go.
Fruitless Search
Even though it was a simple problem, I went looking for an existing
project that did something similar already. I was surprised that I
couldn’t find one. I didn’t spend more than 30-45 minutes searching, but
I looked at the list of Django-powered sites, which besides full
sites includes links to various django-related packages. I also looked
around on a few of the blogs I know cover django programming, and of
course did a google search. When I found nothing, I assumed that meant
the problem was so simple that no one thought they needed to release
their code.
django-links
It is a simple problem, so I built something simple to solve it
(and not being shy about releasing code, am making it available for the
next person who comes along). The django-links app defines a trivial
model to hold the links, with title, description, and publication date.
It provides no UI or syndication features since I didn’t need them, yet.
All of the data is entered through the admin interface. It uses
django-tagging to define tags for the links, so it is easy to search
for links to be associated with a particular codehosting project.
The next step was to update the project details page for codehosting
to show the links. That turned out to be fairly simple as well, using a
little bit of code like this:
{% tagged_objects project_tag in djangolinks.Link as related_links %}
Related Links
{% endifequal %}
{% for link in related_links %}
{{ link.title }} ({{link.release_date}})
{% if link.description %}
{{link.description}}
{% endif %}
{% endfor %}
I also updated codehosting to include a TagsField, so the
details page can show related projects as well as links. I found
instructions for how to build a list of items tagged with all of a
set of tags, but there didn’t seem to be an easy way to build a list of
objects tagged with at least one of a set of tags using only
template language. I suppose that’s the sort of thing that really should
be done in a view, so I’ll work that out in Python.
What next?
The next thing I want to do is build a small CLI app to read a
del.icio.us feed and add the links to the django-links database. That
way I can use del.icio.us instead of the django admin UI to manage the
links.
And, of course, I should add at least a minimal view to show all of
the available links and a tag cloud. django-tagging includes tools to
build those sorts of UI features, so it should be fairly
straightforward.
PyMOTW: optparse
The optparse module is a modern alternative for command line
option parsing that offers several features not available in getopt,
including type conversion, option callbacks, and automatic help
generation. There are many more features for to optparse than can be
covered here, but hopefully this introduction will get you started if
you are writing a command line program soon.
feedcache 1.1
There is a new release of feedcache available tonight, based on a
patch from Thomas Perl. The update includes Unicode support for URLs, a
“force” flag to always download data, and an “offline” mode flag to
never download data.
Thanks, Thomas!
Python Developer Networking
Jesse Noller is leading a campaign to have Python developers form a
network via LinkedIn.com. He talks about it over on his blog, so
check it out for the details.
According to Doug Napoleone‘s comment on Jesse’s post, there is a
more formal effort to set up a PyCon08 group and tie it in with the web
site for the convention. I didn’t realize that LinkedIn supported
groups other than “employers”. It looks like the right way to go for
the community is a “networking group” (there are only a few types, and
the others seem to imply a more formal organization than what we would
have). Unfortunately, the groups feature is closed for right now.
For now, following Jesse’s lead, I set up a position with the “Python
community” organization and job title “None”. I had to guess at the
start date. :-)
I noticed several Python-oriented groups over on Facebook, so that
might be an alternative if LinkedIn doesn’t come through. Somehow
LinkedIn feels more professional; maybe I just have a historical bias
based on Facebook’s origins, though.