10 Tools I Use to Craft Better Python Code
Posted on May 19, 2020 in Tips & Tricks
10 Tools I Use to Craft Better Python Code

10 Tools I Use to Craft Better Python Code
I use these utilities every day to make my Python code presentable. They’re free and easy to use.
Writing beautiful Python is harder than it seems. As part of my release workflow, I use the following tools to make my code presentable and to remove avoidable mistakes.
1. Black
At #1 we have Black. Black is a no-compromises Python code reformatter. It will make sure that your code meets the PEP 8 recommendations.
You can get black from pypi using pip3:
pip3 install --upgrade --user black
Black has rich options, use black -h
to print the help text.
black -t py38 --check <yourfile>

To make black fix the file, remove
the — check
option when running black
.
2. Eradicate
Eradicate is a handy tool for finding commented out code blocks, coming in at #2. You probably don’t need them any more!
You can get eradicate from pypi using pip3:
pip3 install --upgrade --user eradicate
Eradicate has few options, use eradicate -h
to print the help text.
eradicate <yourfile>

Using the option -i
will fix your code in-place.
3. Vulture
Wow, these modules have exciting names! Vulture at #3 will find unreachable, dead code and get rid of it. I use this early in my beautify-pipeline.
You can get Vulture from pypi using pip3:
pip3 install --upgrade --user vulture
Vulture has a few options, use vulture -h
to print the help text.
vulture <yourfile>

4. Coverage
Coverage is the
great-granddaddy of Python code analysis! At #4, I use coverage in my release workflow only to
find unreachable code in my file which I haven’t executed with my assert
statements.
You can get coverage from pypi using pip3:
pip3 install --upgrade --user coverage
Coverage has very many options, I am using it here to find the line numbers of unreached code:
coverage erase # erase previous data
coverage run --branch <yourfile> # run a branch analysis
coverage report -m <yourfile> # create a report

5. Pycodestyle
Pycodestyle is an awesome tool for nit-picking your code formatting. It tells you where your code differs from the recommendations in PEP-8. Because I usually run black first in my workflow, this tool usually has nothing to report on. At number #5, this is a worthy addition to this list!
You can get pycodestyle from pypi using pip3:
pip3 install --upgrade --user pycodestyle
Pycodestyle has very many options, I am using it here to find the line numbers of unreached code:
pycodestyle --show-source --statistics <yourfile>

6. Pylint
Pylint is the one essential tool in this list! I use it frequently when writing code to be published to a wide audience.
You can get pylint from pypi using pip3:
pip3 install --upgrade --user pylint
Pylint has a huge amount of options. I’m using it here with optional plugins. Pylint also provides a very handy code rating, good for gamification :)
pylint --include-naming-hint=y --load-plugins=pylint.extensions.mccabe,pylint.extensions.redefined_variable_type <yourfile>

7. Mypy
Mypy is useful in Python 3.8 because it checks that the type hints that I’ve defined are being used correctly. Mypy is really useful if you’re using type hints in your Python code.
You can get mypy from pypi using pip3:
pip3 install --upgrade --user mypy
Mypy has many options. I’m using it here in strict mode and specifying Python version 3.8 as the target.
mypy --python-version 3.8 --strict <yourfile>

8. Pyflakes
Pyflakes is a very powerful program for finding errors. At this point in my pipeline, pyflakes rarely finds anything new, but it costs nothing to run it anyhow! Pyflakes doesn’t check style, it checks for bugs.
You can get pyflakes from pypi using pip3:
pip3 install --upgrade --user pyflakes
Pyflakes has practically no options — it is designed to be very simple and fast.
pyflakes <yourfile>

9. Doctest
Doctest is a part of the Python installation, a big bonus! If I have used a doctest in my code then I’d like to run it before publishing. There is an interesting concept here: testing through documentation (literal testing). This article by Doug Hellmann gives some nice background.
Doctest doesn’t need to be installed. Most of the options can be configured from within the doctest section of your code file. Here I’m using the -v option for verbose output.
python3 -m doctest -v <yourfile>

10. Bandit
Bandit is a security testing tool. I include it in my workflow as a last line of defence, no one wants to be responsible for a security breach!
You can get bandit from pypi using pip3:
pip3 install --upgrade --user bandit
Bandit has many options — it is designed to be very configurable. Here I’m configuring it to skip error B101 (use of assertions) and only report on medium and high errors.
bandit -s B101 -ll -f screen <yourfile>

11. Radon
Bonus time! Radon is a really nice static code analysis tool. It provides all kinds of metrics, some of which you might want to keep an eye on. Radon gives your modules, classes and methods a rating of A through to F.
You can get radon from pypi using pip3:
pip3 install --upgrade --user radon
Radon has a bunch of options. Here I’m running all its checks and printing all the results to the terminal.
radon cc mi raw <yourfile> -na

I really hope you’ve enjoyed this story! Maybe you’ve found something you can use, or maybe you have some interesting additions, in either case happy coding, Pythonistas!