Finding the Best Posts

See my top posts page for a selection of my best posts. Or search the site with Duck Duck Go:

Recent Posts

Python Type Hints - How to Type a Descriptor

Now invoking The Scroll of Descriptors.

The descriptor protocol allow us to completely customize attribute access. Python’s documentation describes the protocol with types involved described with words. Let’s look at how we can write those as type hints.


A Python Script Template with Sub-commands (and Type Hints)

Now there are two of them!

Earlier this week I shared my Python script template. Here’s an extended version with sub-command support, and an example script.


How to Create a Transparent Attribute Alias in Python

“You don’t know my secret alias,” said the hedgehog.

When dealing with evolvng API’s, it may be useful to rename an attribute in a class, but keep the old name around for backwards compatibility. This would mean making one attribute an alias for another. In this post we’ll look at two ways to achieve this.


Three more uses for functools.partial() in Django

I remain partial to a plant-scroll.

I remain convinced that Python’s functools.partial() is underappreciated. Following my previous post, here are three more ways to use partial() with Django.


The Many Ways to Exit in Python

Show me the exit

It’s fundamentally useful to exit your program when it’s done. Here are five(!) ways to do so in Python.


A Python Script Template, with and without Type Hints and Async

Read on for a “cat trick”!

Python is great for writing scripts for the command line. In this post we’ll look at my script template, an example script, and some alternative versions (without type hints, and in async flavour).


Tips for debugging with print()

A machine of much printing.

If you’re embarrassed at debugging with print(), please don’t be - it’s perfectly fine! Many bugs are easily tackled with just a few checks in the right places. As much as I love using a debugger, I often reach for a print() statement first.


New Testing Features in Django 4.0

Another bunch of testing treasure!

Django 4.0 had its first alpha release last week and the final release should be out in December. It contains an abundance of new features, which you can check out in the release notes. In this post we’ll look at the changes to testing in a bit more depth.


Truncating my blog posts with Python’s HTMLParser

Not a bowl of beautiful soup.

I recently converted this blog to Pelican, a Python powered static site generator. On the way I added a few customizations. One customization is a Jinja template filter to truncate a post’s HTML as a summary, using Python’s HTMLParser class. Here’s how I wrote it.


How to Fix Pip “Yanked Version” Warnings

Are you yanking my chain?

Sometimes pip install will flag a warning saying “The candidate selected for download or install is a yanked version”. For example, if we install attrs version 21.1.0: