Poking around with Python

With my usual caveat that I’m not primarily a programmer (and thus I’m sure there are better ways to do things), I thought I’d share a quick python script I put together.

For some context: I’ve been a long-time subscriber to the KEXP Song of the Day podcast, and I’m not prone to deleting songs after listening to them, which means that podcast is up to ~3000 songs, all sitting in a single folder. My car stereo has usb support, so ostensibly I can dump the whole folder onto a usb key and play it all while driving. Unfortunately, the car stereo can’t handle a folder with that many songs, so I needed to split it into smaller subfolders of around 100 songs each.

Now, I could do this manually (and did, the first time around), but I wanted an excuse to experiment with some scripting, and also wanted to fix some of the song titles (sometimes the ID3 title tag would include the artist, other times not). Doing some searching around, it seems like there’s a pretty solid python library for reading and manipulating ID3 tags, so I decided to use that.

You can see the resulting script here. It does two things: copy the files into subfolders (in a somewhat arbitrary order, which I may try to fix later), and then compare the ID3 artist tag to the ID3 title tag, and if the title tag starts with the same thing as the artist tag, strip the artist name out of the title tag (since it’s redundant, and meant the title was often cut off from being seen on the stereo’s display).

If it’s useful for you, cool! If not, I figured I’d at least share what I’ve been puttering with lately.

Extracting a Nested JSON Value in Python

First, some context: I’ve been working on some Python libraries at work that do things with sets of json data. This is generally pretty easy: Python has a nice library for reading json, so it can be worked on as a native dictionary object in Python. This is great for simple json objects, but there’s some pretty complex json data sources out there, whether it’s being returned as part of an API, or is stored in a file. Sometimes you need to access a specific value from a key buried a dozen layers deep, and maybe some of those layers are actually arrays of nested json objects inside them.

While both arrays and dictionaries are native to Python, so you can do this, it’s kind of a pain. Thankfully, many smart people have already been tackling things like this, which is how there are now handy libraries implementing a (pseudo-standard) approach for getting the value given a specific json path. (There’s even an online evaluator to help you craft a good jsonpath.) The one I’ve been using is called jsonpath-rw. When I looked at the docs for jsonpath-rw, I was a little frustrated, and I wanted to take a minute to write out my eventual understanding, in case there are other folks in a similar boat to me.

Note: I’m not primarily a programmer (currently I’m a Technical Writer, and prior to that I was a QA Engineer — mostly exploratory, not automation), so while my example works, there’s probably more robust and pythonic ways to do the same thing. Continue reading “Extracting a Nested JSON Value in Python”

Link: Why Learning to Code is So Damn Hard

Why Learning to Code is So Damn Hard over at Viking Code School. Nice article discussing the different phases of learning to code, and how to deal with each.

I usually wipe out somewhere in phase 2 or rarely phase 3, which is why I like to say I can read/figure out a fair number of languages, but not write them. Next time I take a stab at learning to program, I’ll try to bear this info in mind.