Motivation

In a previous blog post, I discussed using black to automate Python code styling. You can find that post here. During a recent workshop at Princeton University, I discovered that ruff has been gaining traction for its speed. It was recommended by PythonFZ.

ruff is fast. According to a post by Marsh here, formatting about 250,000 lines of code took only 0.1 seconds with ruff compared to 3.20 seconds for black and 17.77 seconds for yapf.

How to use

To install:

pip install ruff

To check status:

ruff check

-> help: Remove unused import: `cifkit.utils.error_messages.CifParserError`
-> help: Remove unused import: `cifkit.utils.formula`

To format:

ruff format

Cheatsheet

The following is copied from the README.md from the GitHub repository

ruff check                          # Check all
ruff check path/to/code/            # Check all files in `/path/to/code`
ruff check path/to/code/*.py        # Check all `.py` files in `/path/to/code`.
ruff check path/to/code/to/file.py  # Check `file.py`.
ruff format                          # Format all
ruff format path/to/code/            # Format all files in `/path/to/code`
ruff format path/to/code/*.py        # Format all `.py` files in `/path/to/code`.
ruff format path/to/code/to/file.py  # Format f`file.py`.

Last remarks

Since ruff supports the styling format of black, from now on, ruff will replace black in my workflow.

References

  • https://github.com/astral-sh/ruff
  • https://astral.sh/blog/the-ruff-formatter