Since I was able to write the Applescript part of sql_to_playlist in Python3 I was inspired to get rid of my Ruby applescript things and rewrite them in Python. So I present to you in glorious Python3, new hotness imdb_movie_ratings_adder (old and busted ruby version) and update_itunes_selection (ruby version). I never liked writing Ruby code. If you want to compare
The change was pretty easy and I was able to improve both scripts while I ported them over. They both remained about the same number of lines of code, despite using black on the new code. The threading was more complicated in Python and I’m sure I can could have done it more concisely had I known more or been better, but at least the new version uses a work queue rather than just firing off threads willy nilly.
I also learned or re-learned some nice little Python things I’m going to mention here so I can find them again. f"hello {who}"
is a nice way to put variables into strings. @property
was a neat way to add calculated properties to classes and for general getters and setters. json.loads(text_data, object_hook=lambda d: types.SimpleNamespace(**d))
made nice objects from json that I could access as properties rather than dict lookups. e.g. reply.Error
rather than reply["Error"]
, downside is you have to use hasattr(reply, "Error")
rather than "Error" in reply
but I found it better. I also really appreciate
argparse.ArgumentParser()
. I upped my game a little by writing to other variables using dest=
like this:
parser.add_argument(
"--no-open",
dest="open",
default=True,
help="open in a browser",
action="store_false",
)
Posted Monday 30 March 2020 Share