Nik Kantar

Wednesday, March 31, 2021
3 min read

Quick and Dirty Python

Something I absolutely love about Python is just how well it scales from tiny scripts to complex systems. Here’s an example of the former.

I’ve spent a number of years now building monolithic applications and distributed systems with Python, but a big part of why I love the language so much is how well it serves simple scripting needs as well. That’s not as often talked about, and I suppose that makes sense, but I think it’s worth being reminded of occasionally.

The Problem

I had to choose a health insurance plan for work recently, which is always a pain in the butt—all those numbers, all those caveats, all that legalese. In the end, the thing that matters most to me is reimbursement for out-of-network mental health services, since therapy is easily my largest healthcare cost.

Here’s what the (made up) choices look like:

Plan Name Deductible Percentage Reimbursed Monthly Premium
Bronze $150 20% $0
Silver $300 35% $25
Gold $500 50% $75
Platinum $1000 65% $100

A quick glance provides no easy answers: the Bronze plan is appealing with its low deductible and premium, but the Platinum plan has such a high reimbursement percentage, and over the course of a full year it’s not so clear which one is more cost effective—or whether it’s even one of the others.

The formula to calculate the full annual cost looks something like this:

  (annual cost - deductible) * percentage paid
+ deductible
+ annual premiums
= total

OK, that’s not too bad, but it’s tedious to do for 15 plans. However, computers are really good at math, and I’ve got one right here in front of me.

The Python

I turned the plan data and formula into some Python, and had it do the math for me. The code is pretty straightforward:

#!/usr/bin/env python3

SESSION = 100  # single session cost
    {"name": "Bronze", "deductible": 150, "percentage": 0.2, "monthly": 0},
    {"name": "Silver", "deductible": 300, "percentage": 0.35, "monthly": 25},
    {"name": "Gold", "deductible": 500, "percentage": 0.5, "monthly": 75},
    {"name": "Platinum", "deductible": 1000, "percentage": 0.65, "monthly": 100},

def plan_total(plan):
    post_deductible = GROSS - plan["deductible"]
    percentage = 1 - plan["percentage"]  # we want percentage of cost, not reimbursement
    premiums = 12 * plan["monthly"]
    total = (post_deductible * percentage) + plan["deductible"] + premiums
    return total

def calculate_plan_totals():
    plans = [{"name": plan["name"], "total": plan_total(plan)} for plan in PLANS]
    lowest = min([plan["total"] for plan in plans])
    for plan in plans:
        text = f"{plan['name']}: {plan['total']}"
        if plan["total"] == lowest:
            text = f"{text} <-"

if __name__ == "__main__":

I saved this to a file called—no virtual environments, no repository, no nothin’, just a simple script to do one thing with minimal fuss—and ran it.

The Result

$ python3
Bronze 150: 4190.0
Silver 300: 3785.0
Gold 500: 3750.0
Platinum 1000: 3670.0 <-

As I suspected, with the above numbers it’s actually the Platinum plan that ends up being the most cost effective. With my actual numbers it was also one of non-obvious choices that won out.

The Conclusion

Python is great. Sure, you can use it to build really complex stuff to solve large problems, and you absolutely should, but you can also use it to build really simple stuff to solve small problems, and you should do that too.

The Bonus

Larry Hastings has a great talk about just this sort of thing. It was one of my PyCon 2018 favorites.

Update: I wrote a follow-up: Quick and Dirty Python: HOWTO.

Tags: programming, python

Thanks for reading! You can keep up with my writing via the feed or newsletter, or you can get in touch via email or Mastodon.

HEY? Nay.
Quick and Dirty Python: HOWTO