This weekend I had a project to write. I have a huge archive of MP3s left over from the mid-90s through mid-00′s, and they’re a bit of a mess and moving them onto my iPod is always a pain in the neck because they might be inconsistently labeled or have proto-Unicode issues or whatever. So I decided to write a little command line utility that would help me sanify my files. Since I’m old-school and organize my songs by album and genre, my tool would pick the most frequent album and genre names from the ID3 files, while taking in alternative sources for those if they weren’t complete, such as the name of the directory (which also would give artist, if not otherwise available). It would take the song title from the filename if it weren’t available in ID3.
And I decided to write it in Hy. Hy is a Lisp parser that produces Python AST, and since the Python VM accepts and executes raw AST, it’s possible to write performant Python using a completely Lisp-like syntax. Having not written Lisp in many years, this sounded like fun.
You can find the entire source code at my elfsternberg/mp_suggest repository on Github.
I like the feel of this code. It’s a series of let statements that gets the MP3s and then creates a new list of details (genre, album, artist, title, position), then searches through the command line options and details for genre and album names. Each let statement creates a new object; it looks mostly like immutable code, although that fantasy is blown by my frequent use of ap-reduce. Some of the objects created are anonymous functions containing closures, so they’re new functions with pre-created knowledge of things they care about, such as the legal commands and whether or not to override the ID3 title with a modified copy of the filename.