April 2008 Mix (The Way I Rock)

The other day, ph wanted to some new music suggestions. When I asked him to name some bands that he liked, he named ABBA and Michael Jackson. Now, while this seems suspiciously like the response of someone who doesn’t need any new music, I decided to interpret it as a “call for help.” It’s time for a new mix anyway, so what the heck… Here’s a quick try at putting together a mix of (mostly) not-too big artists/songs together. It’s poppier and more major key than my regular mixes. 🙂

Sorting Lists of Lists in Python

No language is perfect, and Python is no exception. In Python 2.4, for example, sorting a list of lists sorted by a value in the nested list (more common than you’d think), requires a bit of work and a library:

import operator
max = sorted(list_of_lists, reverse=True, key=operator.itemgetter(1))[0]

Python 2.5 makes things easier:

max = max(list_of_lists, key=lambda x: x[1])

The official Python Wiki has a nice HOWTO on Sorting in Python (also a much less comprehensive one on sorting dictionaries).

(note: OS X Leopard comes w/ Python 2.5 while, Debian Etch remains on 2.4 by default)

Some Cygwin Setup Tips

I don’t touch Windows much anymore (games and demos in Boot Camp, and EAC and a few other programs in VMWare), but when I’m in there, cmd.exe serves as a constant reminder that I’d rather be elsewhere (Console is an open source attempt to improve the default console). The obvious solution is to bring your UNIX tools with you, which is where Cygwin comes in. Here are some setup notes.

  • I make a setup folder in C:\cygwin\setup, otherwise it just dumps the it’s packages in a folder where you’re running the setup.exe from
  • Some “essentials” that don’t get installed by default: Devel/subversion, Net/openssh, Net/rsync, Shells/rxvt
  • The last one is important because by default, the default blue is too dark (of course). Here’s directions on setting up a proper rxvt .bat file, and then the relevant lines you want in your .Xdefaults:
    rxvt*color4: SteelBlue1
    rxvt*color12: SteelBlue1

    These change the dark blue and blue defaults respectively (I also like RoyalBlue — here’s a list of XTerm colors; you can also enter hex colors directly if you’d like) Also, for a good list of what color0-color15 are. (aside: this is the correct answer to an unanswered Ask Metafilter question from a few years back, but I guess there’s no way to comment on old closed questions)

  • The other big thing is file permissions, which I’ve only run into when rsyncing (I use -avP as my default copy mode). I haven’t done anything with that except for making aliases that chmod 755 and 644 before I need to make copies.

Also, while Fusion has it’s own fast launch (cmd-L), if you’re in Boot Camp, I recommend Launchy, which helps a bit w/ the Quicksilver withdrawals.

Getting Started w/ Python

As you might have heard, Google AppEngine launched tonight, with Python as its initial (and only) programming language to interface with its services. I started switching over to Python (from Perl) a few years ago for general processing and daemon tasks (mostly for its sweet RPC bindings and its comprehensive, if still somewhat convoluted Unicode handling). Over time, as the libraries matured, I started moving more and more over – some things were long overdue, like a CPAN equivalent (pypi and EasyInstall have finally stepped up to the plate), but in some areas, like with cross-platform GUI toolkits, things like py2app/pyexe, or with libraries like Twisted, and SciPy, and Beautiful Soup, Python has long since blown past the competition.

Earlier this year, as I was wrapping up at Yahoo!, I knew I wanted a clean start, and after reviewing what was out there decided on switching to Python as my primary language and making a go of writing my new web apps in Django (deployment, performance, and decoupling being among the primary factors; less wankery in the development community was also a big part of it). I’ve been somewhat sidetracked by a slew of other projects, but so far it’s been a good experience (and I hope to have some stuff to publish soon).

Anyway, all this is a very, very, long setup for a list of resources that may help those who are looking to get started working w/ Python. I’m still not as proficient as I’d like, so here are the references that I typically reach for:

  • PLEAC PythonPLEAC (Programming Language Examples Alike Cookbook) is a project that aims to port the Perl Cookbook to other languages. The Python port has been at 85% for years, but is invaluable when looking at basic constructs.
  • (the eff-bot guide to) The Standard Python Library – although a bit out of date and not comprehensive, it offers short and useful examples for most of the modules in Python. This is great because often times the official library docs while technically complete are also at times completely opaque. If I were to give any advice to people writing API docs, it would be to 1) have some simple real-world usage examples and to 2) allow user annotations (PHP was (and remains!) way ahead of the curve on this one. It’s amazing how primitive the core language/library docs are.)
  • Dive Into Python – I waffle back and forth on how much I like Mark Pilgrim’s book – it’s oftentimes just short of useful and not organized so well (I’m still looking for a good language reference), but it also has really useful tidbits, like when I forget how to append the system import path
  • Python-by-example – this is a new one, and I haven’t used it much (inline-search would do wonders for this) but I wholly approve of the intent: “This guide aims to show examples of use of all Python Library Reference functions, methods and classes.”
  • Otherwise, I’ve found that doing a web search almost always turns up something on ASPN or on a mailing list somewhere.
  • Lastly, there are some interactive shells that are useful, specifically IPython. Reinteract is less of a tool that I use everyday and more of something that’s damn cool. The same w/ Nodebox.

Of course, one of the biggest benefits of Python is how readable the source code is – it’s definitely a big help for seeing how things works. Have any of your own favorite Python resources? Please post ’em up on the comments.

Getting up to speed on Django probably deserves its own post…

R.E.M.

I’ve never been a huge R.E.M. fan, most probably because I was introduced to them by their Out of Time radio hits (Losing My Religion and Shiny Happy People still annoy the shit out of me) — I remember listening to their followup albums Automatic for the People and Monster, and then losing interest. I didn’t have anything against them, and over the years they’ve popped up on my radar from time to time (more for their videos than their songs — Everybody Hurts still holds up as an amazing work, and more recently, the Hammer & Tongs directed Imitation of Life was also quite good), but they they’ve just never resonated with me all that much.

That being said, after reading an interesting discussion on early R.E.M., I chased down their pre-90s work and will have to spend a day sometime giving it a listen. Also, their new album (Accelerate) isn’t bad (single). I’ve only given it one spin so far, although I’ve been deluged w/ new stuff lately and I guess in a different headspace at the moment (new things on rotation: Styrofoam, Cut Copy, M83).

Here’s a passage from a last.fm journal linked from the discussion:

Much like a hat I used to own, R.E.M. was perfect because it fit perfectly into the space where nothing used to be. They drew on the energy of punk without its harshness and nihilism, replacing growling distortion with chiming Byrds guitars and hoarse, angry shouts with pretty harmonies, and filtered out the cornpone from country/roots, leaving only its forthright beauty, in a way that made perfect and astonishing sense to white Southern kids of a certain age, to whom real country was a cliche beloved by redneck uncles and to whom real punk, however satisfying its aggressive pleasures, was music about New York, and London, and Southern California, exotic places that bore scant resemblance to the tree-lined streets of our hometowns. R.E.M.’s sound validated a kind of modern-South lifestyle that we were already living, and made it seem both mythic and earnestly real. You couldn’t buy drugs that did that. Not consistently, anyway.

Administratrivia

  • Inbox Zeroed! Honestly, I had myself pretty convinced that the 4-figure number on my Mail.app icon didn’t matter, but it is honestly pretty freeing. I’m now being much more aggressive w/ my procmail filtering as well. I think I can reduce that distraction drastically. It also means that if you sent me an email and I never replied, I’d be much more likely to do so if you resent it now.
  • While I work on my fancy new blog rewrite I’ve made some little changes like adding a usercard with a randomly rotating avatar pic and comments on individual pages via Disqus.

Dashboard Widget for Posting Blog Entries to Confluence via XML-RPC

First of all, here’s a zip of the working widget (10.4.3+): Confluence Daily Log.zip. I also checked in the Dashcode project.

I had an old WordPress widget from a year or two back that I had written, so I thought this would be a simple port, but I forgot that I had written it w/ a set of Python proxies because 1) xmlrpclib is awesome and 2) the JavaScript XML-RPC libraries I had tried (at the time I believe the best was jsolait) were maddening.

I’ve moved onto jsxmlRPC, which is an improvement from the prior options, but still has some issues. I also gave JS-XMLRPC a try, but the lack of documentation, examples, and the verbose retardedness of it all quickly convinced me otherwise. And I looked at @tomic briefly, but just couldn’t justify 300+K of JS dependencies for it. If I continue to have problems w/ jsxmlRPC, I may switch. Mostly I went w/ jsxmlRPC because I approve of its interface and of its documentation (it’s not that hard, a reference implementation would cover most of it).

My preference would have been something that magically did its business, but I actually had to really dig into jsxmlRPC’s code to get things working. While the demo worked well enough, it was barfing when interacting with Confluence. Turns out this is because jsxmlRPC does not handle parsing of payloads according to the spec – param values can be returned without a nested type tag (defaults to a string data format). jsxmlRPC tried to find a nested value all the time, which as you might guess, caused all sorts of brokenness. This can be fixed by changing the following (swap the while with this if) at the top of the getResultFromValueNode() function (line 385ish):

if ("#text"==valueNode.nodeName){                                              
  return valueNode.textContent;                                                  
}

Note: I saw that jsxmlRPC was still barfing on empty values. That’s probably a simple fix, but my brain’s pretty fried and I’m tired of looking at the code.

I developed this widget in Dashcode, which is many sorts of awesome and very much simplified the process, however the debugger and stack frame was for some reason not quite as helpful as one would imagine it to be. (It was all sorts of unhelpful in tracking down the errors I was looking for even when stepping through). Once I ported the code to Firebug and added a few console.logs(), the problems became much clearer.

In terms of functionality, everything seems to work for me. It does whipped up a very dumb local autosave as well. Here’s what’s missing:

  • Confirm dialog when navigating away from edited entries – I track the editing, but when I found out that Dashboard doesn’t support confirm() I tabled that feature
  • Handling reauth: I have no idea how long tokens last (forever? it’s not specified in the Confluence RPC docs), but if they expire, I don’t have a good way of trying to reauth (this would just involve adding some extra timers and some exception handling so not that bad to implement) – Update: – looks like these expire pretty quickly. I’ve uploaded a new version that just reauths before every API call (getBlogEntries, getBlogEntry, and storeBlogEntry)
  • Help – Some notes for the settings might be useful. IE, the “Space Key” is the shortname for your Confluence Space and the “Endpoint” is http://yourconfluenceinstall/rpc/xmlrpc
  • Widget Icon

OK, it’s late. I’m going to bed.