Hack things together

A little scripting never hurt anybody

Sometimes you have to do a specific task and you are fully capable of doing it manually, however those tasks are also great to flex your muscles and hack something together.

They can be an excellent tool to sharpen your skills with the tools you use regularly, and improve your quick prototyping skills.

In addition, with a couple of iterations, again sharpening an important skill, you could afterwards create a more general purpose tool from a hacky script.

An example

I recently wanted to add all the movies I ever watched as markdown files to my second brain for reference in journaling.

This just needs a few simple steps:

  • Create a slug from the title
  • Create a file with this slug
  • Place a H1 with the title in the file

I could have just done this in a couple of minutes by hand, after all it’s a one off task and I will probably never do this in bulk again.

However I decided to hack together a simple script, in my case I chose Python:

#!/usr/bin/env python3

import os
from slugify import slugify

def quoted(some_str):
    return '"' + some_str + '"'

    "Hello World",
    "Example Title 123"


PREPEND = "media/movies/"
APPEND = ".md"

for title in TITLES:
        slug = PREPEND + slugify(title) + APPEND
        command = COMMAND.replace("SLUG", quoted(slug))
        command = command.replace("INPUT", quoted(title))

After writing this I thought that such a bulk utility could come in handy for my second brain more often, so I added in a simple CLI interface, abstracted some of the inputs a bit and added some documentation:

#!/usr/bin/env python3
"""Bulk run OS command with a list of titles and their generated slugs.
INPUT and SLUG in the -run parameter get replaced by the script with the actual value in double-quotes.

Example to bulk create notes from the slug, in a specific folder, containing the given title:
./.scripts/bulk_creator.py -i "Hello World" "What's up?" -pre "media/movies/" -run "echo INPUT >> SLUG"

import argparse
import os

def slugify(text):
    """Pre-configured, german compatible, slugify."""
    from slugify import slugify as _slugify

    replacements = [
        ["Ä", "AE"],
        ["ä", "ae"],
        ["Ö", "OE"],
        ["ö", "oe"],
        ["Ü", "UE"],
        ["ü", "ue"],
        ["ß", "ss"],
    return _slugify(text, replacements=replacements)

def quoted(some_str):
    return '"' + some_str + '"'

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("-i", "--inputs", nargs="+", required=True)
    parser.add_argument("-pre", "--prepend", help="Prepend to generated slug", type=str, default="")
    parser.add_argument("-app", "--append", help="Append to generated slug", type=str, default=".md")
    parser.add_argument("-run", "--run-command", help="Run command with slug", type=str, default=None)
    args = parser.parse_args()
    for input_str in args.inputs:
        slug = args.prepend + slugify(input_str) + args.append
        if args.run_command:
            command = args.run_command.replace("SLUG", quoted(slug))
            command = command.replace("INPUT", quoted(input_str))

I still do things in this script I would never do in a serious implementation, like:

  • Running commands on the host through os.system
  • Hard-coded defaults, like the .md append parameter
  • Naively escaping with the quoted function

However it will work great for me in the future, and even if it doesn’t I still reinforced some of my Python and prototyping skills and, almost as important, had a fun time doing something I enjoy.

See also