Stefan Arentz

Finding out if an object is a string

Today I was working on an function that takes a variable number of string values. Something like this:

def transmogrify(*values):
   if not all(isinstance(v, str) or isinstance(v, unicode) for v in values):
      raise TypeError("transmogrify() takes a variable number of strings")

There are two things that I learned when trying to improve this. First of all, isinstance can actually take a tuple of types to match against. So a better version would be:

def transmogrify(*values):
   if not all(isinstance(v, (str, unicode)) for v in values):
      raise TypeError("transmogrify() takes a variable number of strings")

But then I searched the documentation and found a hint about the basestring type, which is an abstract superclass for both str and unicode. So to test if an object is a string you can simply do this:

def transmogrify(*values):
   if not all(isinstance(v, basestring) for v in values):
      raise TypeError("transmogrify() takes a variable number of strings")

This is documented in The Python Standard Library - 2. Build in Functions.

Finding your way home with Clojure

Learning a new technology is much more fun when you actually have a mission. So I decided to make a little web app that can show the departure times of streetcars in Toronto.

This is possible because the City of Toronto has made the NextBus API available as part of its Open Data initiative, which provides info about routes, stop locations and arrival time predictions.

You can see the end result in the screenshot below, or try the app yourself on my test server. (If it is down, clone the code from Github, it is very simple to run the web app)

Many thanks to Sean Martell for his superhero CSS skills to make it look so good.

Like my previous Clojure experiment, this app also uses the Angular.js framework on the client side and Ring, Compojure, Chesire and clj-http on the server side.

There is a good chunk of XML parsing in the app using clj-xpath and I also implemented a very basic geo-spatial database that currently has a naive find-objects function, but is plenty fast. (Less than a millisecond to find stops in a database with more than 10000 entries. Yay for JVM speed.)

The app has two modes of operation:

  1. Location based - where it will ask your browser for your current location and then show departure times for streetcars and buses within a 250 meter radius. This is the default. See Demo (Only finds stops if you are in Toronto)
  2. Preconfigured stops - this uses a list of stops that you can provide. The current app has two, one for where I work and one for where I sometimes play. You can change the list of stops in index.js and reference a configuration by putting it's name in the location hash. See Demo

The project is open source and you can find the code at https://github.com/st3fan/departures-board

Clojure & Angular.JS Recipes Demo

I'm getting back into Clojure and web app development and so I am hacking on a bunch of small projects to learn the technology.

The first app that I am making available is a simple web application project that uses the Angular.js framework and Twitter Bootstrap on the client side and Ring, Compojure, Chesire and clj-http on the server side.

It's functionality is pretty minimal: it just grabs a list of recipes from Recipe Puppy and then displays those in a table. You can change the main ingredient to load different recipes.

I think it is a decent example of how to mix together Clojure technologies to get an app that exposes JSON APIs for an Angular.js front end.

Although most of my (Mozilla) work is still in Python, I a really liking, and prefering, Clojure at this point. The language is beautiful and packaging and deployment are so much better than in the Python world.

Code is at https://github.com/st3fan/clj-angularjs-recipes