Recent Posts (Page 10)

The Well-Maintained Test: 12 Questions for New Dependencies

Joel Spolsky’s infamous Joel Test is a quick heuristic test for checking a software engineering team’s technical chops. I’ve come up with a similar test that we can use to decide whether a new package we’re considering depending on is well-maintained.

Read more...

Software engineering is programming integrated over time

The preface of Software Engineering at Google opens with this thesis:

Read more...

Python type hints: types for a descriptor

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.

Read more...

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

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

Read more...

How to Create a Transparent Attribute Alias in Python

When dealing with evolvng APIs, 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.

Read more...

Three more uses for functools.partial() in Django

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

Read more...

The Many Ways to Exit in Python

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

Read more...

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

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).

Read more...

Tips for debugging with print()

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.

Read more...

New Testing Features in Django 4.0

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.

Read more...

Truncating my blog posts with Python’s HTMLParser

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.

Read more...

How to Fix Pip “Yanked Version” Warnings

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:

Read more...

Does Python support semicolons?

Many languages follow the syntax of C, and use semicolons to indicate the end of a statement. For example, in JavaScript:

Read more...

Why does Python log a warning for “invalid decimal literal”?

Take this function:

Read more...

Introducing django-upgrade, a tool for upgrading your Django projects

Django deprecates a small list of features with every feature release, requiring us to update our projects, which can be monotonous. Today I’m announcing a new tool I’ve created, django-upgrade, that automates some of this drudgery for us all.

Read more...

I converted my Lambda@Edge Function to CloudFront Functions

When Lambda@Edge first came out, I added it to my blog’s CloudFront distribution in order to add security headers. Then, when Lambda@Edge added Python support, I converted my function from JavaScript to Python.

Read more...

Three Cheers for blacken-docs

Black is the de facto standard code formatter for Python, and these days I use it on all my projects. blacken-docs is a tool that also allows you to apply Black to code samples in your docs. I recently rolled it out on my projects to great effect.

Read more...

This Blog Is Now a Pythonic Pelican-Powered Publication

I started this blog in 2014 using the popular Jekyll. Whilst it served me well, I’ve wanted to migrate to a Python-based tool for a while now, for a few reasons:

Read more...

Python type hints: use cases for the types module

Writing type hints gives us some familiarity with the typing module. But Python also includes the similarly-named types module, which can also come in handy. Let’s look at the history of these two modules, some use cases of types, and one way in which it’s not so useful.

Read more...

Python type hints: types for regular expressions

Python’s re module lets us search both str and bytes strings with regular expressions (regexes). Our type checker can ensure we call re functions with the correct types, thanks to some parametrized classes.

Read more...

Python type hints: vary return type based on an argument

Here’s a recipe that combines typing.Literal with @overload to define a function that switches its return type based on the value of an argument.

Read more...

Python type hints: how to avoid “the boolean trap”

“The Boolean Trap” is a programming anti-pattern where a boolean argument switches behaviour, leading to confusion. In this post we’ll look at the trap in more detail, and several ways to avoid it in Python, with added safety from type hints.

Read more...

Python type hints: how to use typing.Literal

To put it tautologically, type hints normally specify the types of variables. But when a variable can only contain a limited set of literal values, we can use typing.Literal for its type. This allows the type checker to make extra inferences, giving our code an increased level of safety.

Read more...

Django 3.2 Update for “Speed Up Your Django Tests” Released

I released my book “Speed Up Your Django Tests” over a year ago, in May 2020. Since then, we’ve seen two major Django releases, including a whole bunch of test-related changes, some of which I worked on as part of the book.

Read more...

Python type hints: how to use typing.cast()

Python’s dynamism means that, although support continues to expand, type hints will never cover every situation. For edge cases we need to use an “escape hatch” to override the type checker.

Read more...