Code readability is one of the criteria to judge code quality. One criterion to measure code quality is The “WFT” law proposed by Martin, that is, the number of “WTF” bursts per minute. Do you have a “WTF” impulse when reading someone else’s Code or doing a Code Review?

In order to help developers unify their codestyle, the Python community developed the PEP8 codestyle. It is not mandatory for everyone to follow it. Python also released a tool for checking codestyle for PEP8, also called PEP8, later renamed PyCodeStyle.

Look at the following code:

import time, datetime

class ListNode:
    def __init__(self, val):
        self.val = val
        self.next = None

    # in python next is a reversed word
    def reverse(self, head):
        prev = None
        while head:
            temp = head.next
            head.next = prev
            prev = head
            head = temp

        a = [
            [
                1,
                u'hello world',
                0
            ],
            [
                2,
                "hello python",
                0
            ],
        ]
Copy the code

Pycodestyle – This is a non-PEP8 code snippet. Pycodestyle can be used to detect any non-Pep8 code snippets

$ pycodestyle link.py
link.py:1: [E401] multiple imports on one line
link.py:3: [E302] expected 2 blank lines, found 1
Copy the code

Pycodestyle reminds us that there are two things that do not conform to the specification. The first is that there are multiple imports in a single line, and the second is that there are two empty lines between classes and modules. This is just a simple code example, the real business code may have hundreds or even hundreds of lines. In our development process, if we always pay attention to whether each line of code fully complies with PEP8, it will affect the development efficiency.

Black is an uncompromising code formatter. Why is it called uncompromising? Because it detects non-conforming code styles and formats them for you, it doesn’t even need you to decide. It is also one of the requests authors’ favorite tools

It is very simple to use. After the installation is successful, it is used like any other system command. You only need to specify the file or directory to be formatted after the black command

black link.py
Copy the code

It’s a nice little tool, but it’s not exactly PEP8 formatted. For example, the default is 88 characters per line of code. Of course, you can customize the length by using the -l parameter

# in:

l = [1,
     2,
     3,
]

# out:

l = [1, 2, 3]
Copy the code

The latter, which puts multiple elements on a single line, is obviously easier to read and more compact (not recommended if you’re paid by lines of code), and Black is a strict subset of PEP8. My best practice is to use the formatting tool that comes with PyCharm with Black. Black also supports integration into Pycharm.

How do I integrate Pycharm

1. Install black:

pip install black
Copy the code

2. Find the installation path of Black

$ which black  # linux/mac
 $ where black  # windows
Copy the code

3, Add the extension tool, open the Preferences->Tools->External Tools, add a new extension tool Program black installation path, Arguments fill F I le P at h” role=”presentation”>

4, Select Tools-> External Tools-> Black to format the code of the currently opened file. Of course, you can specify a shortcut key for it to make it easier to operate.

It is worth noting that the tool only supports Python3.6 and is still in beta.

This article is from the Python Enthusiast Community, a partner of the cloud computing community. For more information, you can follow the Python Enthusiast Community.