Dev360.com - Web Development and Beyond

In a split-second, customers will determine if your site is worthy of their time. Can you afford to lose them?

Blog

Categories

Archive for May, 2009

Rendering a RSS feed in Django

Sunday, May 24th, 2009

I have been working to get the new version of DEV360.com out the door and found myself in need of displaying RSS items from my Wordpress-powered blog on the DEV360 site which is developed in Django.

There were two libraries that I found for parsing RSS; RSS.py by Mark Nottingham which unfortunately didn’t support RSS 2.0, and Mark Pilgrim’s feedparser.py. I settled on the latter since it contains more features, has easier syntax and has better unit-test coverage.

For what I’m doing, a template tag works out best for rendering the RSS items; let’s fast-forward to the code:

foo.html:

{% load rss_feed %}
{% rss_feed %}

[application]/templatetags.py:

import feedparser
from datetime import datetime
from django.template import Library, Node
register = Library()

@register.simple_tag
def rss_feed(itemCount=5):
	channel = feedparser.parse('http://blog.dev360.com/feed/')

	html = []
	html.append('<div id="rss-feed">')
	html.append('	<h1>Latest blog posts</h1>')
	html.append('	<ul>')
	for entry in channel.entries[:itemCount]:
		url = entry.id
		title = entry.title
		date = datetime.strptime(entry.date, "%a, %d %b %Y %H:%M:%S +0000");
		html.append('<li><p><strong><a href="%s">%s</a></strong>' % (url, title));
		html.append('%s</p></li>' % (date.strftime("%B %d, %Y")))

	html.append('	</ul>')
	html.append('</div>')
	return "\n".join(html)

Non-obtrusive JS rollovers with jQuery

Friday, May 22nd, 2009

I thought I’d share a technique that I have grown very fond of over the last year - all made possible by jQuery’s syntactic bliss.

On many occassions in the past, I have found myself writing roll-over code that looks something like this:

<ul class="navigation">
  <li onmouseover="this.className='selected';" onmouseout="this.className='';" class="selected"><a href="#">Link 1</a></li>
  <li onmouseover="this.className='selected';" onmouseout="this.className='';"><a href="#">Link 2</a></li>
  <li onmouseover="this.className='selected';" onmouseout="this.className='';"><a href="#">Link 3</a></li>
</ul>

As you can see, the onmouseover/onmouseout makes the code less readable and takes up a lot of unnecessary space. One way to get rid of it is obviously to take advantage of the :hover pseudo class on the a element but if you want the hovered item to be the only item marked with the selected class then obviously that solution doesn’t cut it.

jQuery’s event wiring, along with its selector syntax allows you to remove the onmouse**** tags and earn some well deserved HTML karma.

JS

$lastActiveItem = null;
jQuery(document).ready(function(){
	$('.navigation li').mouseover(function(){
		$lastActiveItem = $('.navigation li.selected');
		$('.navigation li').removeClass('selected');
		$(this).addClass('selected');
	});

	$('.navigation li').mouseout(function(){
		$('.navigation li').removeClass('selected')
		$lastActiveItem.addClass('selected')
	});
});

Ignoring deprecation warnings in Twill/Python

Tuesday, May 19th, 2009

I wrote a small command-line utility with Twill and Python that I use to scrape prices from our booking engine at work. The Python script worked fine when I ran it on my Mac/Python2.5 but Twill (or mechanize, to be exact) was giving deprecation warnings when I tried to run it on Windows/Python2.6.1.

C:\Users\Administrator\twill\other_packages\_mechanize_dist\_auth.py:14: DeprecationWarning: the md5 module is deprecated; use hashlib instead import re, base64, urlparse, posixpath, md5, sha, sys, copy
C:\Users\Administrator\twill\other_packages\_mechanize_dist\_auth.py:14: DeprecationWarning: the sha module is deprecated; use the hashlib module instead import re, base64, urlparse, posixpath, md5, sha, sys, copy

The solution is trivial; to supress deprecation warnings in python, just add an ingore filter before the line where you import Twill (or any other offending library). The alert reader may notice that the second part of the filterwarnings argument (’.*’) is a regular expression; this would allow you to tailor exactly which deprecation warnings you wish to propagate to the command-line.

import warnings
warnings.filterwarnings('ignore', '.*')

from twill.commands import *
import twill

Wolfram|Alpha - A New Kind of Search Engine?

Monday, May 4th, 2009

I just read about a new search AI dubbed Wolfram Alpha (WolframAlpha.com) in an article that was published on Sunday May 3 in The Independent. It talks about this new search engine AI invented by Dr. Stephen Wolfram which supposedly “understands and responds to ordinary language in the same way a person does”. Skimming through the article, I can’t remember getting this excited about a site since learning about the MetaWeb/Freebase API, but after scratching the surface a little bit, the excitement faded away very quickly.

Let me start by saying that I can’t say that I’m very knowledgeable or up to date when it comes to the academic research on the subject - apart from following a few AI and Semantic Web blogs. Now, the thing that immediately raised my eyebrows was a little time line of the history of the Internet which listed a handful of milestones - with the most recent item reading “2009 Dr Stephen Wolfram launches Wolfram Alpha.”, listed right after the traditional stuff that you would expect to see (ARPANET, first PC etc). For a site that is not even public (as of Monday May 4, 2009), it seems awfully rash to have it on a list like that.

Reading the fine print confirmed more and more of my skepticism. For instance, Dr. Wolfram admits that information in the engine is first “curated” or assessed by experts AND it also requires approximately 1,000 people for maintaining the site’s databases and keeping current with new information. All this begs to question how exactly is this an AI or anything revolutionary? And to top that off, the end of the article mentions that Google already is working on something similar which is already live. Wow! - Revolutionary - let’s credit Dr. Wolfram and say a loud Amen.

A person’s past is perhaps the best way to look for clues and Dr. Wolfram is no exception. This is where Dr. Dubya appears to have a solid track record as a self-indulgent hype machine. Consider for instance the overwhelmingly negative feedback and concerns about originality for his supposedly epic ‘10 years in the making’ book ironically titled “A New Kind of Science”. A New Kind of Search-Engine? - No, I doubt so.