Recent Posts

See also posts by tag or search with DuckDuckGo:

Django: Introducing Djade, a template formatter

Happy DjangoCon US 2024 to you. Whilst I am not there, I have adopted the spirit of the season and got to work hacking together a new tool.

Read more...

Git: find when a commit was reverted or reapplied

Git doesn’t store reversion links between commits. It only relies on commit messages to track this information. When you run git revert, the default message includes a line about the reverted commit:

Read more...

Python: my new uv setup for development

uv is a new, fast Python packaging tool. (I believe it is pronounced as written, “uhv”, rather than spelt out like “you-vee”. This saves a syllable, fitting the tool’s ethos of speed.)

Read more...

Django: speed up tests slightly by disabling update_last_login

Django’s test client provides two methods to log in a user: login() and force_login(). The latter one is faster because it bypasses the authentication backend, including password hashing, and just sets a session to have the user logged in. Typically, you’d want to use it in setUp() like this:

Read more...

Django: hoist repeated decorator definitions

Django provides us with a rich set of view decorators. In this post, we’ll look at a technique for hoisting repeated use of these decorators to reduce repetition.

Read more...

Django: a pattern for settings-configured API clients

Here’s an example of a common pattern in Django projects:

Read more...

Django: build a Microsoft Teams bot

Recently, I built a Microsoft Teams bot for a client, inside their Django project. It wasn’t fun or easy, but the experience did increase my resiliency as a developer. I also went into this forewarned by my wife, a product manager also known as “the integration queen”, who has experienced the difficulties of the Teams API first-hand.

Read more...

Git: generate statistics with shortlog

git shortlog generate statistics about the commits in a repository, intended to help generate project release notes. By default, it groups commits by author, but it can use other fields too. Use it for a more powerful dissection of a repository than tools like GitHub’s Insights tab.

Read more...

Git: avoid reset --hard, use reset --keep instead

When I started learning Git, I found many references covering two ways to undo commits with git reset:

Read more...

Django: avoid “useless use of .all()

Here’s a little ORM pet peeve of mine that may deepen your understanding of how QuerySets work.

Read more...

Django: rotate your secret key, fast or slow

Django’s SECRET_KEY setting is used for cryptographic signing in various places, such as for session storage and password reset tokens. This makes keeping it secure a high priority since an attacker with the key could forge things like password reset tokens.

Read more...

Python: profile total memory allocated with tracemalloc

tracemalloc is Python’s standard library module for tracking memory allocations. It has many bells and whistles for detailed analysis, allowing you to slice allocations by file and line or compare snapshots. But for simple purposes, displaying the total memory allocated is sufficient, which is what this recipe does:

Read more...

Django: create sub-commands within a management command

argparse, the standard library module that Django uses for parsing command line options, supports sub-commands. These are pretty neat for providing an expansive API without hundreds of individual commands. Here’s an example of using sub-commands in a Django management command:

Read more...

Django: Test for pending migrations

Django requires every change to model fields and meta classes to be reflected in database migrations. This applies even to things that don’t typically affect the database, such as Field.choices. When iterating on code, it’s easy to make a model change and forget to update the migrations accordingly. If you don’t have any protection, you might even deploy code that crashes due to out-of-date migrations!

Read more...

Python: Fail in three characters with 1/0

Here’s a code snippet I often type:

Read more...

Python: Import by string with pkgutil.resolve_name()

Django and other frameworks often allow you to configure classes, functions, or modules to import using strings. To do the same in your own code, use pkgutil.resolve_name() from the standard library (added in Python 3.9):

Read more...

Python: Mock an inner import

Sometimes, you want to test a function that uses an inner import and mock that imported object. For example, say you wanted to mock dig() in this example:

Read more...

Python: Show all subclasses of a class

class.__subclasses__() returns a list of the direct subclasses of a given class:

Read more...

Django: Introducing django-harlequin, a launcher for Terminal-based SQL IDE Harlequin

Harlequin is a Terminal-based SQL IDE by Ted Conbeer. It is pretty popular, with over 2,500 GitHub Stars and counting. It looks like this:

Read more...

Django: An admin extension to prevent state leaking between requests

Here’s a small protection I added to a project a few years ago. I was considering it again since I saw a similar potential bug in a Django middleware.

Read more...

Python: Diffing unit tests to keep a copy-pasted code in sync

Copy-paste-tweaking library code feels like a dirty but inevitable programming practice. Often driven by deadlines or other constraints, it seems all projects end up with something copy-pasted in and tweaked for one specific use case.

Read more...

Python: Make line number paths with inspect

Many terminals and text editors support what I’ll call “line number paths” of the form <filename>:<lineno>. Opening that path, whether by clicking or passing to a CLI, opens the given file at that line.

Read more...

Django: Pinpoint upstream changes with Git

Django’s release notes are extensive and describe nearly all changes. Still, when upgrading between Django versions, you may encounter behaviour changes that are hard to relate to any particular release note.

Read more...

Git: Show the first tag containing a commit SHA

Say you have a commit SHA and want to know the first version it was released in. You can use this command:

Read more...

Boost Your Git DX update out now

I have just released an update to my book Boost Your Git DX, six months after its initial release. This update adds some extra content and has a bunch of edits based on reader feedback. The PDF is now ten pages longer, for a total of 363.

Read more...