May 23

Introduction

I’ve recently started work on a Rails project after working on many Django apps in the past couple of years and I’d like to share my experiences and gripes from my transition. Rather than attempting to analyze all the pros and cons of each framework, I’m going to try to focus on the transition pain points.

Python vs. Ruby (Perl’s Ugly Sister)

Python and Ruby are quite similar languages and I’m not going to compare them in any serious depth. Both have their relative merits and I find both languages easy to work in. The one complaint I have about Ruby is that it borrows far too much from Perl. I’ve done a fair bit of scripting in Perl, and most people agree with me that Perl scripts of any substantial size quickly become very difficult to maintain.

Ruby is a great language but it tries to provide similar syntax and constructs to Perl, while duct taping all of Ruby’s own powerful features on too. This makes the Ruby syntax look like someone emptied all the symbols from the keyboard into a shotgun and shot them into your face.

Another irritating habit Ruby borrowed from Perl is having multiple ways of doing the same thing when simple having one way would be fine.

Say what you will about Python’s whitespace rules, but Python code is easy to read and consistent, and generally there is a single correct way of doing things.

As I mentioned earlier though, I do like Ruby, and one of the awesome features is Ruby’s blocks. Blocks are basically Ruby’s way of implementing closures and they are extremely flexible and powerful. In Python, to do something similar you’d use lambdas or list comprehensions.

Views Versus Templates

In the Rails world, the layer which generates the final markup sent to the browser is called the View layer. In a Rails View, you can chuck as much rails code as you like in amongst your markup.

Some of the early sites I worked on many years ago were written in PHP, which had the same idea. Rails views and PHP both allow the developer to mix markup and code together in the same file. When making PHP sites, you quickly learn that this is a terrible idea and quickly becomes a maintenance nightmare. One of the best things I ever did in my PHP days was to start using a PHP templating library, such as Smarty.

By restricting the template layer to simple constructs, like Smarty and Django both do, the developer is forced to keep business logic and complex constructs separate from the markup, which makes maintaining and changing the markup in future a piece of cake.

When it comes to markup reuse, the two frameworks are quite evenly matched. Django has template inheritance, which I think is a very elegant solution to the age old problem of sharing common markup, whereas Rails solves this problem using “Layouts”. Both frameworks also support template inclusion and “partials”.

Multiple Apps in a Project

When developing web projects, there are many situations where you need to have 2 very different applications which may need to access some of the same data. In the django world, you create a project and a project may contain multiple applications. Application B can use the ORM classes defined in application A’s model layer if it so chooses.

In rails, there is no such distinction between project and application. If you want to share model layer objects between code, your best bet is to have all the code in the same application. If you wish to logically seperate your applications, you can do so by creating 2 seperate rails projects, but it is not easy (or recommended) to share the models between the 2 projects. Another option is to have a controller per logical application, but this quickly leads to huge amounts of code in each controller.

URL Routing

In Django, you design the URLs for your application using regular expressions, a skill in every programmer’s toolbox (or at least it should be). In Rails you do these mappings using Ruby. Anyone who knows how to write regular expressions can instantly write url matching/capture expressions in Django.

Schema Migration

One thing Rails used to be much better at than Django was database schema migration. This is a feature built into Rails, but Django developers used to have to write manual schema migration scripts to transition their databases. These days though, there are numerous choices for easy schema migration for Django projects, my favourite being South. This is an area where Django really had to play catch-up though, and the various stable projects which have emerged to supporting migration are indicative of the maturity of Django as a whole.

Conclusion

Django and Rails both have their strong points and are similar in many ways, but certain fundamental philosophies differ, especially the Convention vs Configuration side. They are both great frameworks, and transition issues aside I look forward to working more with Rails in the future. Let me know what you think in the comments section below!

May 6

Overview

TheTVDB.com is user contributed TV show and episode database which is used by many popular home theatre applications, such as XBMC to look up details of TV shows.

I’ve written a simple Python interface to their Programmer’s API which makes it possible for developers to extract the TV data they need from TheTVDB with minimal effort.

Let me know if you find this useful, or you use it in any real projects!

Dependencies

This module uses cElementTree (available in Python 2.5+) to traverse XML, so you’ll need Python 2.5 available. Alternatively if you have an older version of Python you can install cElementTree or ElementTree yourself, and modify the import line at the top of the file.

Usage

I’m not going to go into great detail about the interface because the module is fairly self explanatory, but here are a few of the main functions:

Classes

There are 2 classes, the Show class and the Episode class. Most functions in this API return either a Show object, an Episode object, or a list of IDs. I’m not going to document all of the properties in each object as these are visible in the module itself, but you’d usually access the public variables directly, e.g. episode.overview.

Functions

  • get_matching_shows(show_name)
    Returns a list of tuples (id, name) of all matching shows in thetvdb
  • get_show(show_id)
    Get the show object matching this show_id.
  • get_episode(episode_id)
    Get the episode object matching this episode_id.
  • get_show_and_episodes(show_id)
    Get the show object and all matching episode objects for this show_id.
  • get_updated_shows(period)
    Get a list of show ids which have been updated within this period (day/week/month).
  • get_updated_episodes(period)
    Get a list of episode ids which have been updated within this period (day/week/month).
  • get_show_image_choices(show_id)
    Get a list of image urls and types relating to this show.

License

I’m releasing this under the GPL2 license. If you reuse it or improve it, please let me know!

Download

http://loopj.com/thetvdbapi/thetvdbapi.py

Apr 25

Update!

Thanks for all the great feedback on the plugin! I forgot to explain in the original article text that I intended to release this as GPL2. Take the code and modify it however you like, just make sure to give me credit for the original code ;)

Overview

This is a jQuery plugin to allow users to select multiple items from a predefined list, using autocompletion as they type to find each item. You may have seen a similar type of text entry when filling in the recipients field sending messages on facebook.

Features

  • Intuitive UI for selecting multiple items from a large list
  • Layout controlled fully in CSS, easily customisable
  • Result caching reduces server load
  • No images required, just the plugin’s .js file and some CSS
  • Handles json search data for autocompletion
  • Smooth animations when results load
  • Select items using the mouse or keyboard

Screenshots

Vertical list style item selection
Vertical list style item selection

Facebook style item selection
Facebook style item selection

Demo

A live demo of the token input is available here.

Usage

  • Make sure you have jquery script included on your page
  • Include jquery.tokeninput.js on your page
  • Include one of the provided stylesheets, or make your own
  • Create a server-side script (php/rails/django anything goes) to generate the search results.
    The script itself can fetch data from wherever you like, for example a database or a hardcoded list, but it must do the following:

    • Take exactly one GET parameter named “q” which will contain the query string. E.g. http://www.example.com/myscript?q=query
    • Output JSON search results in the following format:
      [{"id":"856","name":"House"},
       {"id":"1035","name":"Desperate Housewives"},
       {"id":"1048","name":"Dollhouse"},
       {"id":"1113","name":"Full House"}
      ]
  • Turn text inputs into tokeninputs using jQuery and point them to your results script:
    <script type="text/javascript"><!--mce:0--></script>
  • A list of selected item ids is created inside the original text entry, process them as usual when the form is submitted.

Download

Please don’t use these links directly in your scripts, I might change their location at any time. Instead, download them and use your own local copy.

Jun 16

CSS veterans will probably already know this, but here’s a fix to a problem which has been bugging me for ages.

Problem:

If you have something like:

<div>
    <img src=... />
</div>

You may see a gap below the image on some browsers, a gap which cannot be removed by setting the padding/margins to 0:

Solution:

<img> tags are rendered using display: inline by default, which means they act and flow like text does on a page. In order to stop spaces being added (caused by spaces next to the img tag in your html), you should set your img to use display: block.

<img style="display: block" src="..." alt="" />

This means your image will no longer act like flowing text and will no longer have a gap below it!

Caveats:

Changing from display: inline means your img tag will ignore things like text-align: center, and other alignments specific to displaying elements inline.

Image credit: buhsnarf on flickr

Jun 13
The Original Microblogger
icon1 James Smith | icon2 Blogging | icon4 June 13th, 2008| icon3Comments

I’ve just been reading an article about the diarist Robert Shields, who sadly passed away last year, describing his amazing “condition”.

Robert was thought to have hypergraphia, an overwhelming urge to write, and detailed every action of his life meticulously in a collection of diaries. The entries chronicled every 5 minutes of his life.

Every mundane, boring detail, such as the following gem:

July 25, 1993
7 am: I cleaned out the tub and scraped my feet with my fingernails to remove layers of dead skin.
7.05 am: Passed a large, firm stool, and a pint of urine. Used five sheets of paper.

It struck me how similar this was to the actions of heavy twitter users (I will refrain from mentioning specific names!), tweeting every insignificant detail of their lives.

Twitter’s growth problems aren’t a new phenomenon either, Robert too suffered from service scaling issues as his diaries piled up to fill 94 cartons.

These diaries are now in the safe hands of Washington State University, but won’t be released to read for another 49 years. I can barely contain myself.

Robert Shields was a true pioneer. I salute you!

May 1

At the end of March, Jude and I bumped into Vincent and Eugene from intruders.tv at Minibar London where I mentioned that it was a shame we couldn’t really get to any of the cool web conferences going on in Europe, at which point Vincent mentioned they were looking for someone to do some interviews at The Next Web 2008 conference in Amsterdam the following week. Go to Amsterdam, the party capital of Europe, and interview some of the hottest people in the tech world. Hell yes.

So following in the illustrious footsteps of Immad, we grabbed the camera, flew over to Amsterdam and tried to interview the best people we could find. Who knew it would be so easy!

The first major interview we got was with Kevin Rose of digg.com fame and talked to many more people throughout both days, it seems that if you have a camera and you ask politely for an interview, people are more than happy to oblige.

It was great fun, an awesome way to meet people and make great friends and contacts.

So check out intruders.tv if you haven’t been there before, and especially check out the interviews we did :)

Kevin Rose (digg.com, diggnation, revision3, pownce)

Jessica Mah (Entrepreneur, blogger)

Werner Vogels (Amazon.com)

Adeo Ressi (thefunded.com)

Stefan Fountain (soocial.com)

Khris Loux (js-kit.com)

David Prager (revision3.com)

Gary Cige (zilok.com)

Jun 4
Information Fallout
icon1 James Smith | icon2 Privacy | icon4 June 4th, 2007| icon3Comments

Online privacy is not something that has overly concerned me on my internet travels, but there are many people who are vocal about protecting information online. I have been of the opinion that there shouldn’t be a problem with this information being available unless people have something to hide. More recently though, there have been a large number of articles about information being used in new and interesting ways which have led me to think differently about what I say and do online.

I read an interesting article today about the CEO of a company called Plazes being caught out by his own location tracking product after skipping a conference. Just a few hours later, Mashable covered a story about interesting pictures captured by Google’s Street View cameras, including sunbathing girls, strip club patrons and a guy picking his nose on a bench. These articles got me thinking about all the information being constantly fed onto the web, and how things which are seemingly innocent, such as street photography and letting people know what you are up to could get you in trouble.

Mashable’s article raised an interesting point:

It won’t be long until people start identifying protected witnesses or spotting domestic violence and who knows what else.

I’m sure there are many more examples, and it isn’t just Street View and Plazes which are collating this sort of information.

As a fan of social networking and microblogging I’m feeding more and more information to services like Facebook and Twitter, meaning that more people have access to details of what I’m doing than ever before. At this time we make this sort of information available it might seem like a trivial thing to do, but often this information is archived and stored for the future, meaning, in theory, someone could build a profile of your habits and activities.

When a user chooses to release this information to friends and contacts, there are normally clear controls on who can see what. When it comes to information outside of the user’s control, such as the public domain images captured by Google’s Street View, there isn’t much that a user can do about it, and this capturing and hoarding of public domain information is surely to increase.

A couple of years ago when Facebook and MySpace were starting to make it big in my area, I began hearing stories about job interviewers who would mention to interviewees that they had read the interviewee’s Facebook profiles, there were even rumours of this being used as the basis for refusing employment. If you ask around I’m sure you will find similar stories. Again, the user can choose who to make this information available to, and what to release, but there are often ways to get around these protections. For example, it is simple enough to use a friend’s account on a social networking site, such as Facebook, to view users’ profiles on a network other than your own.

It is now commonplace to Google people to get an idea of their reputation and links (although with a name like James Smith you get a certain level of automatic anonymity!) and the idea of doing this wouldn’t really sound too shocking to most people. Are people adjusting their ideas of acceptable privacy due to the changing volumes of available information?

Another possibility which could arise from this growing information craze, is that of social misinformation. It would be quite easy to set up accounts with social networking and microblogging sites and feed them with whatever information you choose to, meaning you could present yourself online as a different person to offline. Of course, this idea has been around for a long time, with stories of men posing as women on chat rooms etc since the dawn of the internet, but would it be now credible to create a persona and backstory online to manipulate people, such as in the job interview situation mentioned above? Imagine a situation where someone’s Twitter or Facebook status was used as an “alibi” to show where someone was. Clearly this could never be used in a legal situation, but maybe it would convince the boss that you were ill rather than at that party.

What do you think about this? Does anyone have any other examples of where information such as this has been used in an interesting way? Let me know by leaving a comment.