View on GitHub

Python-best-practices-course

Material for the course "Python best practices", Scientific Software Center, Heidelberg University

Code formatter

A code formatter takes care of formatting your code so that it adheres to the styling standards. For Python, a popular code formatter is black or also Ruff. Black is PEP 8 compliant but also adds some own flavor to the code. Comparing code that has been formatted with black makes it easier to spot the differences. You can even try it out online.

Please note that the formatter reformats the file in-place, that means, substituting the content of the original file!

Note that the formatter does not reformat comments other than inserting proper whitespace before and after the #.

Using the Ruff formatter

To invoke the Ruff formatter, use

ruff format

or

ruff format --preview

similarly to the linter.

You may also use ruff check --fix. This will not reformat all the code, but apply the fixes from linting that can be safely done.

Ruff configuration

You can configure Ruff quite freely, and also use it to format docstrings and markdown files. Take a look here at some examples. You may also configure your IDE to lint and format your files using Ruff. It is also great for using in pre-commit hooks.

Task 3: GitHub assignment code formatter: Use the same repo as from Part 2 - Linter

Format the code using ruff format. Check what it changes. See if you can get the autograding to pass.