Ruff introduces the format
feature for the v0.1.2. So right now Ruff, not only is a linter but also a formatter. In this short article, we are going to learn how to use the formatter.
Requirements
- Python installed
The Ruff Formatter
As the documentation says, the Ruff formatter is an extremely fast Python code formatter designed as a drop-in replacement for Black, available as part of the ruff
CLI (as of Ruff v0.0.289).
If you already have a previous version of Ruff installed, run this command in your console:
pip install --upgrade ruff
For anyone who doesn't have Ruff installed, run this command:
pip install ruff
Now, to use the formatter feature in a Python file, we run the ruff format /path/to/file.py
command, and it will format the file.
To format all the files in a directory, we run the ruff format .
command.
Example
I have this code in a main.py file:
log = new_log(tracker["ip_address"], tracker["request_url"], tracker["request_port"],
tracker["request_path"], tracker["request_method"],
tracker["browser_type"], tracker["operating_system"],tracker["request_time"])
Then, we run the following command:
ruff format main.py
Conclusion
The formatter is an incredible addition to Ruff, it helps us to write more readable and maintainable code. Now, we have a fast linter and formatter.
Top comments (0)