Black: the magic Python code formatter

If you have worked with Python, you might be bothered by the PEP 8
(Style Guide for Python Code) a lot since it has a list of complicated rules in order to make your code more readable.

A good news is that one of my colleagues introduced a automation tool to smartly format your Python code and it can
also be integrated into various IDEs and ediotrs like PyCharm,Sublime, Emacs, Vim, Atom, just name a few.

Following is a very simple example showing how nicely the formatter can make your coding life easier!

Before

1
2
3
4
5
6
def details(self) -> dict:
    """Fetch and buffer catalog details from platform."""
    if not self._catalog_details:
        self._catalog_details = self.api.get_catalog_details(hrn=self.hrn, lookup_apis=self.apis)
        self._catalog_details['layers_by_id'] = {layer['id']: layer for layer in self._catalog_details['layers']}

After

1
2
3
4
5
6
7
8
9
10
def details(self) -> dict:
    """Fetch and buffer catalog details from platform."""
    if not self._catalog_details:
        self._catalog_details = self.api.get_catalog_details(
                hrn=self.hrn, lookup_apis=self.apis
            )
        self._catalog_details["layers_by_id"] = {
                layer["id"]: layer for layer in self._catalog_details["layers"]
            }

Much clear, huh?

Here you can find the official site of the Black tool including how to install and play with it, have fun!