Easy templating with Python and Jinja

At work, I’ve been using both Golang and Ruby to render Nomad jobspec files in a consistent manner across environments. But Python is my one true love and I wanted to learn how to do it in Python. Turns out I already knew how… you can do it in Jinja2! I’ve used Jinja2 in the past a lot for Django and Ansible, so this was pretty easy to pick up. Here are some notes for future me when I want to inevitably do this again....

2023-07-04

Python dataclasses are awesome!

Last week, I was working on some Python at work and I found myself wishing for an equivalent to Golang’s structs. Rather than passing in a bunch of core data types (string, dict, int, etc.) in an out of methods, I wanted to pass in a single object with predictable attributes. But…. I didn’t want to go through all the trouble of creating a class with a constructor, and passing in all the various attributes....

2023-05-22

Python type hints

After working with Go for a while, one of the biggest drawbacks to Python for me is that the types of parameters to functions and methods are completely opaque without good documentation. For example, let’s take this silly function right here: def my_func(thing1, thing2): print(thing1 + thing2) This function will happily accept strings or ints (or floats, for that matter) as input and depending on the type, will do completely separate things!...

2022-08-05