Recent Posts (Page 9)

How to Add a Favicon to Your Django Site

Your site’s favicon appears in the browser tab, and is a key way to brand your site. Setting up a favicon is a simple task, but once you start considering vendor-specific icons, it becomes more complicated.

Read more...

Python type hints: split types by Python version

The typing module continues to evolve, with new features in every Python version. This can make it tricky if you’re trying to type code that supports multiple Python versions. To help write such code, Mypy identifies version checks using sys.version_info and reads the appropriate branch.

Read more...

Set up EditorConfig for your Django project

The “tabs versus spaces” war is scheduled to rage on until the heat death of the universe. And whilst the Python ecosystem is firmly in the “spaces” camp, there remain numerous other text formatting options.

Read more...

How to Fix a Python “SyntaxError: invalid character” Caused by Curly Quotes

Here’s an innocent enough looking Python file:

Read more...

Python: make simple mocks with SimpleNamespace

When testing Python code you may need a mock object. That’s okay! But what’s the best way to build a simple mock?

Read more...

Book-Driven Development from “Boost Your Django DX”

On Monday I released my new book “Boost Your Django DX”. It covers many tools and practices that are useful for developing Django projects.

Read more...

Removing Python 3.6 Support from My Packages

Python 3.6 reached its end of life on the 23rd December. As its release manager put on the Python forum, it has gracefully “ridden off into the sunset”.

Read more...

“Boost Your Django DX” Released

My new book, Boost Your Django DX is out now. I’m so glad it has shipped and I can relax, a bit 😅

Read more...

My Third Appearance on Django Chat

I’ve again had the pleasure of joining Carlton and Will on the Django Chat podcast, in Episode #105. They moved fast with this one - we spoke yesterday, and the podcast is live today!

Read more...

How to Make an Immutable Dict in Python

Python’s built-in collection types come in mutable and immutable flavours, but one is conspicuously missing:

Read more...

Today’s Django Security Release Deconstructed (4.0.1, 3.2.11, and 2.2.26)

Happy new year, and happy new upgrade! Django has issued a new security release today. This is the first set of security fixes that I’ve been involved in, so I thought I’d take the opportunity to explain the issues in a bit more depth.

Read more...

Python type hints: handle optional imports

This post is not about importing typing.Optional, but instead imports that are themselves optional. Libraries often have optional dependencies, and the code should work whether or not the import is there. A common pattern to solve this to catch ImportError and replace the module with None:

Read more...

Set up a Gunicorn Configuration File, and Test It

If you use Gunicorn, it’s likely you have a configuration file. This is a Python module that contains settings as module-level variables. Here’s an example with some essential settings:

Read more...

Django: introducing django-browser-reload, for automatically refreshing your browser during development

Hitting “refresh” to see your changes is an instinct many web developers develop. But it’s a small waste of time that adds up to many hours per year spent waiting. It’s a sub-optimal development experience (DX).

Read more...

Preorder My New Book: Boost Your Django DX

Developer Experience (DX) is a catch-all term for anything that can improve your development workflow. Such improvements can help you write better code, faster, with fewer bugs.

Read more...

Django-related Deals for Black Friday and Cyber Monday 2021

Here are some Django-related deals for this year’s Black Friday (26th Nov) and Cyber Monday (29th Nov), including my own.

Read more...

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