DEV Community

Carlos Armando Marcano Vargas
Carlos Armando Marcano Vargas

Posted on • Originally published at carlosmv.hashnode.dev on

Exploring Format: The New Feature of Ruff | Python

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

Enter fullscreen mode Exit fullscreen mode

For anyone who doesn't have Ruff installed, run this command:

pip install ruff

Enter fullscreen mode Exit fullscreen mode

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"])

Enter fullscreen mode Exit fullscreen mode

Then, we run the following command:

ruff format main.py

Enter fullscreen mode Exit fullscreen mode

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.

Resources

Ruff Formatter

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Heroku

This site is powered by Heroku

Heroku was created by developers, for developers. Get started today and find out why Heroku has been the platform of choice for brands like DEV for over a decade.

Sign Up