Docker: Clean up unused stuff on your development machine
Docker keeps all objects (images, containers, volumes, etc.), with no automatic cleanup. It thus consumes disk space without bound, which can eventually fill up your development machine.
Docker keeps all objects (images, containers, volumes, etc.), with no automatic cleanup. It thus consumes disk space without bound, which can eventually fill up your development machine.
To output the root directory of the current repository, use:
To print just the name of the current branch, use:
string_if_invalid in templatesDjango’s template engine has a string_if_invalid option that replaces missing variable lookups with a string of your choice:
When trying to improve a slow function or module, it’s always a good idea to profile it. Here’s a snippet for quickly profiling a section of code with Python’s cProfile module, in two flavours. It’s adapted from the cProfile documentation’s Profile example. I have used versions of this snippet over the years to narrow in on performance issues.
Any for unused parameters in testsWhen you create a function to match an interface, it often needs to accept parameters that it doesn’t use. Once you introduce type hints, testing such functions can become a little irksome as Mypy will require all arguments to have the correct types. Your tests can end up creating unused objects only to match the tested function’s signature. Here’s a technique to avoid that work.
As projects evolve, old functionality gets removed. Often such deletions are incomplete, leaving in their wake unused functions, classes, and other code objects. Unused code is clutter that brings no joy: it hinders comprehension, taxes codebase-wide quality improvements, and can sometimes lead to errors if later used.
QuerySetsSometimes code depends on the order of a QuerySet whilst not specifying an order. This can lead to random, flaky test failures because databases can return rows in any order when none is specified. The problem is made worse by some databases, notably PostgreSQL, which nearly always return rows in insert order, but occasionally use a different order when a table has had recent deletions.
json_script for pre-serialized JSON stringsDjango’s json_script template filter is a convenient and safe way to pass a larger amount of data to JavaScript. I covered it in my post last year How to Safely Pass Data to JavaScript in a Django Template.
Mypy 1.4.0 was released last week (2023-06-20). I’m happy to see it includes three improvements that modernize error messages with newer type hint syntax:
format_html()Can you spot the problem with this Django snippet?
I’m happy to announce that my new book, Boost Your Git DX is available now, in “early access”.
To detect if a certain operation is in progress in a Git repository, use git rev-parse like so:
Django’s template engine allows you to augment template contexts with context processors. These are functions that take the current request and return a dictionary to be merged into the context:
Here’s an application of “test smarter, not harder”, as per Luke Plant’s post. I came up with this recently whilst working on my client Silvr’s project, and I’m pretty proud of it. It should apply to any project using Django’s admin.
Sometimes you want to download a whole website so you have a local copy that you can browse offline. When programming, this is often useful for documentation that sites that do not provide downloadable versions, and are not available in offline tools like DevDocs.
Your Django project’s startup time impacts how smooth it is to work with. Django has to restart your project every time you run a management command and when runserver reloads. This involves importing all your apps, and thus all the modules that they import.
If you upgrade to Django 4.2, you may see system check warnings like:
I just released django-upgrade 1.13.0. The headline features are some new fixers targeting Django 4.2, which is currently in alpha.
Git hooks are useful for enforcing standards in your repositories. But sometimes, they’re in the way and you gotta skip ’em (locally), such as when writing “WIP” commits, experiments, and even testing hooks. Here are a few techniques for skipping hooks.
The pre-commit framework lists hundreds of hook repositories on its hooks page. You can drop these into your configuration file and get a tool running in seconds. But there are many more tools out there that you might want to use, which you can run with custom configuration.
Since shortly after the launch of my first book, I’ve offered a simple “purchasing power parity” deal: a 50% discount for those outside the top 50 countries by GDP (according to Wikipedia’s table). I really want to make sure my knowledge is accessible by as many people as possible, at a fair price.
POSIX permissions include the execute permission, which allows the file to be executed as a program. The permission can be set for each of the three classes: user, group, and others. So, for example, you can have a file that is executable only by its owning user.
Since December, several of my open source packages have been “lifted” on Tidelift. This means that Tidelift are paying me a portion of their subscription fee for continued, standardized maintenance of these packages. These funds are very appreciated, so thanks to Tidelift subscribers 🫡.
Different filesystems have different case sensitivity, for example: