Tag Archives: python

1 Comment

I have been trying out Cloud to Device Messaging (C2DM). It is a technology from Google that makes it possible to send small messages to an Android Devices running Froyo, the latest Android 2.2 release with the Google API's enabled. The application is a very simple prof of concept that illustrates how the technology can be used. It is however not a full implementation and it is missing some required functions.

The PoC is composed by two different sub-systems, an Android application and a web application server. The servers is written in Python and is leveraging Tornado.

The system supports the message sequences depicted in the diagrams below:

Register a device for C2DM:
Register a device for C2DM

Sending a message to device:
Sending a message to device

In the two coming posts I will walk through how each of the sub-systems (device application and web server) works. If you are aching to get your hands on the code you will find it on github: Android application, Web server.

You will find the device side introduction here and the server side introduction here.

A little while back I wrote about test driving Tornado development. This is all good and well, but how about getting instant feedback? If you are developing on a Mac or a Linux machine this is certainly possible. I found this great blog post on how to set up a change monitor on a directory that will alert you on test failures using Growl or Notify. Thanks Rodrigo Pimentel for the write up.


The past week I have had the pleasure to write a small web based system in python. Being a devout TDD practitioner the first step in learning python was of cause to understand the unittest module. Next thing was to see what mocking frameworks are available. To my delight I found that mockito is available for python as well (here).

The application I'm working on is a web service API to access our application using Cloud to device messaging (c2dm) from Google. To provide the web API I am using the Tornado framework.

As web frameworks goes they are usually tricky to test drive. Not so with Tornado. It ships with it's own testing framework that allows the developer to set up a round trip request/response cycle within a unit test using the tornado.testing module. Given clear module boundaries and mockito to stub out dependencies it's an easy thing to test drive Tornado web application development.

The tornado.testing module comes with a couple of very useful classes. I used AsyncHTTPTestCase to test the RequestHandler classes. To make sure that only failing tests writes to the log I also inherited LogTrapTestCase.

Below is some sample code written to allow device registration with our application server. First is the test in it's entirety and then the implementation.

The tests are in the module c2dm_registration_service_test.py:

The production code is in the module c2dm_registration_service.py:

Added to this one more class in the mondule c2dm.py. So far it is just a stub:

With this knowledge I can't find any excuses not to test drive web development in Python. It is such an easy task and takes no time at all. It is well worth the effort even when creating pilots, which most likely will end up becoming production code in the not to distant future.