John McCollum

Another reason to love Python

Last night, I was doing some code kata at codingbat.com when I was presented with the following problem:

Given a string and a non-negative int n,
we'll say that the front of the string is the
first 3 chars, or whatever is there
if the string is less than length 3.
Return n copies of the front;

front_times('Chocolate', 2) → 'ChoCho'
front_times('Chocolate', 3) → 'ChoChoCho'
front_times('Abc', 3) → 'AbcAbcAbc'

Pretty straightforward, right? The solution from the site was the following:
def front_times(str, n):
  front_len = 3
  if front_len > len(str):
    front_len = len(str)
  front = str[:front_len]

  result = ""
  for i in range(n):
    result = result + front
  return result

I went for a totally different solution though:
def front_times(str, n):
   return n*(''.join([x for x in list(str[:3])]))

Is it something I’d use in production? Nope. It’s less readable and less pythonic, in my opinion, than the longer answer. (Although some would argue that you don’t get more pythonic than list comprehensions.)

Did it make me smile? Hell yes. Although ultimately, the most pythonic is probably:

def front_times(str, n):
   return n*(str[:3])

Posted in Python, web development at March 25th, 2010. No Comments.

jQuery 1.4 Reference Guide – reviewed

Packt Publishing were kind enough to send me a copy of the jQuery 1.4 Reference Guide, by Jonathan Chaffer and Karl Swedberg. The book is intended to be a comprehensive reference for the library, aimed at intermediate to advanced readers.

The first point I should make about this book is that it is not intended for users who are new to the jQuery library – this is not a book full of tutorials. If that’s what you’re looking for, you should check out Learning jQuery 1.3 instead, by the same authors.

The book is organised into eleven chapters, covering (amongst other things) selectors, DOM traversal and manipulation, events, effects, and AJAX. The whole library is covered (as you would expect from a reference guide).

Each method in the library has its parameters documented. This is surprisingly helpful – many jQuery methods can be overloaded for various purposes, and this feature of the book is a great help towards learning the possibilities of each method. In fact, one of the take-home points for me was that many methods now support anonymous functions as parameters, offering lots of flexibility.

Next, a bare bones code sample is shown to demonstrate the method. It is followed  by an explanation of the code sample, along with any ‘gotchas’ or quirks that you might experience. Multiple examples of code are shown where there might be different ways to use a particular method. The book really is quite exhaustive, and from that point of view offers great value for money.

Although this book works really well as a reference book, the surprise for me was that it didn’t just work well as a reference book. Reading through the book, I picked up a lot of hints and tips that will almost certainly change how I use jQuery. For example, I realised that my knowledge of selectors was sorely lacking. (Partly this was because I’d never bothered to learn the CSS3 selectors properly, but jQuery gives me a reason!)

Another excellent chapter for me was the last one, regarding plug in architecture. The structure of this chapter was a little different, basically walking the reader through the process of creating various types of plugin. Like the best technical books, this chapter actually inspired me to put the book down and write some code – surely a sign that it was doing its job properly!

It’s worthwhile to note that if you are considering purchasing a copy of this book, Packt will make a contribution to the jQuery project for every copy sold; so in buying this book, you’ll increase your own knowledge, and you’ll be directly helping the jQuery project too. For me, that’s one of the best reasons to buy.

Posted in javascript, jquery, web development at March 24th, 2010. 1 Comment.

Anonymous functons with FireUnit

FireUnit makes it quick and easy to write unit tests in JavaScript, doing something like the following:

fireunit.ok(foo==bar, 'foo should equal bar');

But what if you need to do something a little more complex than a one liner? You can write and execute an anonymous function, like this:

fireunit.ok(function(){
    var myList = $('ul');
    var originalLen = myList.children('li').length;
    fireunit.click( myList );
    return $(myList).children() == originalLen + 1;
}(), 'This test should return true');

Two things to note here:

  1. Make sure that you include the extra parentheses after the function definition; this executes the function straight away
  2. Make sure you  return true or false  from the function (or an expression that evaluates to true or false.)
Posted in javascript, web development at March 22nd, 2010. 1 Comment.

Dan Wellman’s jQuery UI 1.7 reviewed

I recently finished reading Dan Wellman‘s book, jQuery UI 1.7 – The User Interface Library for jQuery. But with the imminent realease of version 1.8 of the popular interface library, is this book still relevant? In my opinion,  it is.

This book is recommended for beginner to intermediate users of jQuery UI, and it hits the mark perfectly for its intended audience. There’s a chapter for each of the high and low level widgets, as well as the CSS and effects frameworks. Each chapter is structured to follow a logical pattern, and Dan’s clear writing style means that the code and text is easy to follow.

Each chapter starts with a basic example, and gradually adds more and more options to show the flexibility and power of the framework. The text is supported by numerous code samples, which can be downloaded from Packt Publishing‘s support site. Finally, each chapter ends with an imaginative, more advanced use of the library. One chapter showcases jQuery UI’s drag and drop functionality with a game, for example. These examples really bring the code to life, and inspire you to open your favourite text editor and start coding!

The most obvious omission from this book (in fact, the only one worth mentioning, really) is the lack of a section on writing your own plugins for jQuery UI. Dan obviously feels the same way, because he’s written a tutorial on the subject here, and something like this is promised for the next update of the book. If this more advanced topic is something that interests you, you might want to wait for the next version of the book.

Alternatively, if your knowledge of jQuery UI is at the other end of the spectrum, there’s no reason at all not to go ahead and buy the current version. The book focuses on the common features of each widget (for example the destroy, enable, and disable methods) so that you can easily adapt to the new components as they appear.

If you’re somewhere between the two camps, as I was, then this book will consolidate your knowledge and give you lots of inspiration to use the parts of the library you might not have touched on yet. And that, for me, was worth the cover price alone.

Posted in javascript, jquery at March 9th, 2010. No Comments.

Twitter Reddit Flickr LinkedIn Stack Overflow Github Email Ne RSS