This blog is still Python-wise. When it comes to 10 developers developing a project, there will be 10 coding styles certainly. Today I have an interesting helper to make all Python scripts give a same experience, same format of the code.

This would be credited to my team now. They standardize this for the work as well.


Black

Black is a package for formatting job to Python. With the tagline, "The Uncompromising Code Formatter", we have no need to configure or customize any format as the package itself barely allow us to do so.

Take a second to preview how Black can tidy up the mess code. We can write any and let Black handle when we save it. Like this.

Feel free to visit the main page on PyPI here.

black
The uncompromising code formatter.

Setup on VSCode

Assumed that we all are developing on VSCode. It would be easy-peasy to setup like 1-2-3.

1. Install Black

Run the command

pip install black

And let the package be installed completely.

I also recommend to install Python plugin as well.

Python - Visual Studio Marketplace
Extension for Visual Studio Code - IntelliSense (Pylance), Linting, Debugging (multi-threaded, remote), Jupyter Notebooks, code formatting, refactoring, unit tests, and more.

2. Define Python formatting provider

Open palette by cmd + shift + p

Then search for "Python > Formatting: Provider" and select "black"

3. Enable Format on Save

Now search for "Editor: Format on Save" then check the checkbox.

And that's it.


Hope we review codey happily with this tip.