Sunday, December 13, 2009

Amazon Mechanical Turk Perl Library Error

I've been playing with Amazon Mechanical Turk for a project, and I think it's really, really powerful.  So far, I've done a bunch of research and some tests using the Requester UI, but have not yet dug into the API.  But, over the past few days, I've finally started my research and experimenation using the API directly.  However, my very first time trying to use the modules that Amazon published, I got a big error:

t/01-ListOperations.t ..................... Can't locate object method "new" via package "Net::Amazon::MechanicalTurk::Transport::RESTTransport" at /Users/dviner/.cpan/build/Net-Amazon-MechanicalTurk-1.01-1kIAvQ/blib/lib/Net/Amazon/MechanicalTurk/ line 21.

This error message was repeated a few dozen times.  Also, in searching for the problem, I found this StackOverflow issue: Perl module Net::Amazon::MechanicalTurk failing tests.

I find it pretty amazing that Amazon hasn't fixed this problem.  But, after doing a bunch of searching and sniffing around, I couldn't find any solution at all.

So, I made one of my own.  Here's a short patch that will allow the Net::Amazon::MechanicalTurk module pass all it's tests.  Most will be skipped with the message:

skipped: Configure Amazon AWS Authentication to enable tests against Mechanical Turk Sandbox

If you want to try to get those to verify, you must make a file named in the current directory.  In that file, you must list your access key and secret key as well as the turk service URL and API version you are using.  Here are the sample contents:

access_key: your-access-key
secret_key: your-secret-key
service_version: 2008-08-02

Note that even with this file in place, several of the tests still fail.  I'm not an expert on the Turk API (yet), so I'm not sure how to fix the other errors that occur.

But hopefully this patch will help people.  Feel free to contact me if you find additional changes that should be made to the library.

Wednesday, December 2, 2009

jQuery, tables, and administrative interfaces

A few month ago, I posted about using Jquery to re-imagine simple things. Recently, I've been working with jQuery and jQuery UI quite a bit - in particular in building simple administrative interfaces.

Simple admin interfaces almost always require a table of stuff - users, comments, posts, actions, jobs, whatever - the "things" being administrated. jQuery has a really cool plugin called TableSorter that makes it's incredibly easy to convert a simple HTML table into a fancy, easy-to-read interface.

TableSorter even has a simple pagination capability (see the demo here). The current version (2.0.3) of the paginator requires that you have an html element of class "pagesize" which defines the number of items to display on one page. But the code doesn't actually do an error check to see if that value doesn't exist or if the value is NaN (javascript's fancy way of saying the value there is not a number). So, here's a simple patch to apply to the jquery.tablesorter.pager.js file which will perform the validation & error check.

I found one other drawback of the paginator. By default, the paginator will display something like "1/103" for the current page number and the total number of pages. But, the current code is somewhat limiting. You can change the separator ("/" by default) between the current page number and number of pages, but that's about it. In addition, the code will invoke jQuery's .val() method to display the resulting string. This is also really limiting, since not all elements support using val() to set their contents. For example, invoking .val() on a element does nothing.

So, here's another patch to apply to the jquery.tablesorter.pager.js file which will allow you to register a callback function. By using a callback function, you can control the display as you see appropriate. The callback function is passed 3 arguments:
*func (config, page_active, page_total)
The config parameter is the internal configuration information from the paginator, but the really useful parameters are the other two. page_active is the currently displayed page number. page_total is the total number of pages.

Here's a sample of how you can use the callback:

    .tablesorter({ 'widgets': ['zebra'] })
    'container': jQuery("#jq-pager"),
    'size': 20,
    'positionFixed': false,
    'updatePageDisplayFn': function (config, pg, total)
        jQuery('.pagedisplay_nice').text('Page '
            + pg + ' of ' + total);

This sample code results in displaying the pagination information like this:
Page 1 of 103
in the text area of all elements with the class "pagedisplay_nice". In my HTML page, I have a simple span element in a different place on the page, which looks like this:
<span class="pagedisplay_nice" />
This provides a massive amount of flexibility for the user of TableSorter's Pager plugin.

Hope this helps!

Visible Networking

Recently, Tony Karrer posted an article where he described a pretty interesting new idea - which he termed Visible Networking. I think this concept is really cool. I've always thought that we should use blogs to have more open debates on issues, thoughts, etc. Too often, blogs seem like a one-way broadcast about something - but I think that the most interesting ideas and thoughts are identified and refined in the course of a dialog, not a monologue.

So, I'm going to ask Tony to start visible networking with me. I'm not sure exactly what that means or how it will play out, but I think it's worth trying!

Tony, what do you think?