Styling and Formatting Code
Code is read more often than it is written. -- Guido Van Rossum (author of Python)
When we write a piece of code, it's almost never the last time we see it or the last time it's edited. So we need to (1) explain what's going on (via documentation) but also (2) make it easy to read. One of the easiest ways to make code more readable is to follow consistent style and formatting conventions.
There are many options when it comes to Python style conventions to adhere to, but most are based on PEP8 conventions. You'll notice that different teams follow different conventions and that's perfectly alright. The most important aspects are that everybody is consistently following the same convection and that there are pipelines in place to automatically and effortlessly ensure that consistency. Let's see what this looks like in our application.
We will be using a very popular blend of style and formatting conventions that makes some very opinionated decisions on our behalf (with configurable options).
Black: an in-place reformatter that (mostly) adheres to PEP8.
isort: sorts and formats import statements inside Python scripts.
flake8: a code linter with stylistic conventions that adhere to PEP8.
We installed all of these as they were defined in out
setup.py file under
# setup.py dev_packages = [ "black==20.8b1", "flake8==3.8.3", "isort==5.5.3", "jupyterlab==2.2.8", "pre-commit==2.11.1", ]
Before we can properly use these tools, we'll have to configure them because they may have some discrepancies amongst them since they follow slightly different conventions that extend from PEP8. To configure Black, we could just pass in options using the CLI method, but it's much more efficient (especially so others can easily find all our configurations) to do this through a file. So we'll need to create a pyproject.toml file and place the following configurations:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
The pyproject.toml was created to establish a more human-readable configuration file that is meant to replace a
setup.cfgfile and is increasingly widely adopted by many open-source libraries.
Here we're telling Black that our maximum line length should be 79 characters and to include and exclude certain file extensions. We're going to follow the same configuration steps in our pyproject.toml file for configuring isort as well. Place the following configurations right below Black's configurations:
1 2 3 4 5 6 7 8
Though there is a complete list of configuration options for isort, we've decided to set these explicitly so it works well with Black.
Lastly, we'll set up flake8 but this time we need to create a separate
.flake8 file and place the following configurations:
1 2 3 4 5 6 7 8
Here we setting up some configurations like before but we're including an
ignore option to ignore certain flake8 rules so everything works with our Black and isort configurations.
Besides defining configuration options here, which are applied globally, we can also choose to specifically ignore certain conventions on a line-by-line basis. Here are a few example in our code of where we utilized this method:
1 2 3 4 5 6 7
By placing the
# NOQA: <error-code> on a line, we're telling flake8 to do NO Quality Assurance for that particular error on this line.
To use these tools that we've configured, we could run these commands individually (the
. signifies that the configuration file for that package is in the current directory) but we can also use the
style target command from our
black . flake8 isort .
black . All done! ✨ 🍰 ✨ 9 files left unchanged. flake8 isort . Fixing ...
We may sometimes forget to run these style checks after we finish development. We'll cover how to automate this process using pre-commit so that these checks are automatically executed whenever we want to commit our code.
To cite this lesson, please use:
1 2 3 4 5 6