Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Formatting Code With Ruff

00:00 Ruff is a linter. You can use it to lint your code, but Ruff is also a formatter. You can use it to format your code, for example, to replace Black, which is another well-known Python formatter.

00:14 Now, the command ruff format with the option --diff allows you to preview the changes that Ruff would make when reformatting your code.

00:25 And if you’re happy with the changes, then you can just run ruff format to reformat your code. It’s that simple. To test this, you can open your terminal

00:35 and navigate to the directory where the file one_ring.py is. Now if you run ruff format --diff, you’ll be presented with a diff that shows you, for example, how the global variable CHARACTERS, that is currently spanning one long line, would be split across multiple lines with one item per line and with a trailing comma after the very last item, the string "Sauron".

01:03 And you will also see that inside the function ring_bearer(), the tuple that currently has two items, with different quotes on each item, will be replaced with a tuple where all quotes are the same.

01:16 But this is just a diff, that’s why on your screen you will see some output in white, which is the code that remains the same, some output in red, which is the code that would be removed, and some output in green, which is the new alternative.

01:29 You will also see a couple of green pluses on the left that look like they have no code on the right because they don’t, they’re just extra newlines that Ruff is inserting so that there’s two blank lines between blocks of code at the top level separating your imports, your global variable CHARACTERS, your two functions, and the final if statements.

01:53 So these are just some rules that Ruff usually obeys. And if you’re happy with the changes, you can run ruff format to apply those changes. So you run ruff format, you press Enter, and you will see the output saying that one file was reformatted.

02:09 So if you run ruff format again because you didn’t change the code, naturally Ruff will tell you that one file was left unchanged. And the whole point of this was to fix the errors you had in the previous lesson with a line that was too long and with a weird mixture of quotes.

02:25 So now you can actually run ruff check --select Q,E, and you can in fact double-check that all of your issues were fixed. Or you could just open the file and look at the file and see how you no longer have a very long line, and how you no longer have a mixture of single and double quotes in the file.

02:45 So this is what ruff format does.

02:48 Now, in case you like, for example, to check your code with the rule set E from Pycodestyle, you have two options. You either always type --select E, or you configure Ruff to use those rules by default as well.

03:08 What you will learn in the next lesson is exactly this: how to configure Ruff to use different defaults, for example, by including more rules when checking.

Become a Member to join the conversation.