Notes on using code formatter in Python

Writing Python properly tends to break the coding style (especially the width grows), so I decided to introduce a code formatter and code checker. I think there are a lot of articles like this, but I will write them as a memorandum of my own.

environment

I'm running Python 3.8.2 on Windows 10 WSL using pyenv.

Selection of code formatter

I first tried autopep8 and yapf, referring to this article https://www.kimoton.com/entry/20181223/1545540702. Both are easy to install with pip:

pip install autopep8 pip install yapf

Looking at the code example tested in the article and its format result, it seems that it is different whether to insert a line break every time at the comma separated part.

It's obvious when you try it, especially with long arrays:

test.py


a = [1, 2, 3, 45, 6789, 998244353, 1000000007, 1000000009, 18446744073709551615, 1, 2, 3, 45, 6789, 998244353, 1000000007, 1000000009, 18446744073709551615, 123456789]
$ autopep8 -a test.py
a = [
    1,
    2,
    3,
    45,
    6789,
    998244353,
    1000000007,
    1000000009,
    18446744073709551615,
    1,
    2,
    3,
    45,
    6789,
    998244353,
    1000000007,
    1000000009,
    18446744073709551615,
    123456789]
$ yapf test.py
a = [
    1, 2, 3, 45, 6789, 998244353, 1000000007, 1000000009, 18446744073709551615,
    1, 2, 3, 45, 6789, 998244353, 1000000007, 1000000009, 18446744073709551615,
    123456789
]

(The argument -a in autopep8 is an abbreviation for --aggressive. If you don't specify this, it doesn't seem to format to shorten the line length.)

As you can see, autopep8 inserts line breaks for each element, while yapf seems to insert line breaks conservatively as long as the length limit is not exceeded. As a personal preference, I decided to use yapf because I thought it would be verbose and difficult to read if I could insert line breaks for each element.

Basic usage of yapf

The result of formatting the file <filename> with yapf <filename> is printed to standard output.

yapf seems to have fine control over the style, but here I'd like to use the default settings. Here are some basic options for using the formatter (excerpt from yapf --help):

  -d, --diff            print the diff for the fixed source
  -i, --in-place        make changes to files in place
  -r, --recursive       run recursively over directories

--With the --diff option, only the changes will be pointed out in diff format. --Yapf outputs the entire formatted file to standard output by default, but with the --in-place option, the file is overwritten with the format result. --With the --recursive option, you can format all files under a directory (including inside lower directories).

Code checker

I decided to use flake8 as the code checker. flake8 can also be installed with pip.

pip install flake8

For example, if you apply the long sideways test.py to flake8, it says" the line is too long ", but you can confirm that it can be solved by applying yapf:

$ flake8 test.py
test.py:1:80: E501 line too long (167 > 79 characters)
$ yapf -i test.py
$ flake8 test.py
$

Note that flake8 seems to be able to check the entire folder and below without adding options such as -r.

If you want to partially disable code checking and formatting

For example, in a two-dimensional array (list), you may want to align the commas line by line:

2darray.py


a = [
    [ 1, -2,  3],
    [-4,  5, -6],
    [ 7, -8,  9]
]

For alignment, I intentionally put a space immediately after [, but this causes an error when I run it on flake8:

$ flake8 2darray.py
2darray.py:2:6: E201 whitespace after '['
2darray.py:4:6: E201 whitespace after '['

According to the flake8 documentation (https://flake8.pycqa.org/en/latest/user/violations.html#in-line-ignoring-errors), you can ignore the error by adding the comment # noqa: E201 to the end of the line. (In this case I want to ignore the same error over multiple lines, but from the documentation it seems that I can't do that unless I put an exception in the whole file ...)

a = [
    [ 1, -2,  3],  # noqa: E201
    [-4,  5, -6],  # noqa: E201
    [ 7, -8,  9]   # noqa: E201
]
$ flake8 2darray.py
(Nothing is displayed)

However, if you apply yapf in this state, it will still be regarded as an extra blank and will be erased:

$ yapf 2darray.py
a = [
    [1, -2, 3],  # noqa: E201
    [-4, 5, -6],  # noqa: E201
    [7, -8, 9]  # noqa: E201
]

In this case, you can partially disable the format by adding a comment as in yapf docs (unlike flake8, you can also specify in expression or range units. is):

a = [
    [ 1, -2,  3],  # noqa: E201
    [-4,  5, -6],  # noqa: E201
    [ 7, -8,  9]   # noqa: E201
]  # yapf: disable

You can now safely disable yapf checking as well.

$ yapf -d 2darray.py
(Nothing is displayed)

Recommended Posts

Notes on using code formatter in Python
Notes on using dict in python [Competition Pro]
Notes on using MeCab from Python
Notes on installing Python using PyEnv
Notes on using rstrip with python.
Notes for using OpenCV on Windows10 Python 3.8.3.
Notes using cChardet and python3-chardet in Python 3.3.1.
Execute Python code on C ++ (using Boost.Python)
Notes on nfc.ContactlessFrontend () for nfcpy in python
Notes for using python (pydev) in eclipse
Personal notes to doc Python code in Sphinx
ABC125_C --GCD on Blackboard [Notes solved in Python]
[Python] Notes on accelerating genetic algorithms using multiprocessing
Web scraping notes in python3
[Unity (C #), Python] Try running Python code in Unity using IronPython
[Django] Notes on using django-debug-toolbar
[Python] Notes on data analysis
Notes on installing Python on Mac
Generate QR code in Python
Broadcast on LINE using python
Get Evernote notes in Python
Character code learned in Python
Notes on imshow () in OpenCV
Translate using googletrans in Python
Notes on installing Python on CentOS
Using Python mode in Processing
Minimum notes when using Python on Mac (Homebrew edition)
Notes on reading and writing float32 TIFF images in python
GUI programming in Python using Appjar
Notes on Python and dictionary types
Precautions when using pit in Python
[Python] Generate QR code in memory
Introducing Python using pyenv on Ubuntu 20.04
Preparing python using vscode on ubuntu
Notes on using post-receive and post-merge
Using global variables in python functions
Study on Tokyo Rent Using Python (3-2)
Write selenium test code in python
Let's see using input in python
Infinite product in Python (using functools)
Edit videos in Python using MoviePy
Check python code styles using pep8
Notes on accessing dashDB from python
Install Python on CentOS using Pyenv
Study on Tokyo Rent Using Python (3-3)
Handwriting recognition using KNN in Python
Notes on using matplotlib on the server
Code tests around time in Python
Try using Leap Motion in Python
Depth-first search using stack in Python
[VS Code] ~ Tips when using python ~
When using regular expressions in Python
(Beginner) Notes on using pyenv on Mac
GUI creation in python using tkinter 2
Create a Python environment for professionals in VS Code on Windows
Bash, Python, Javascript, code command, etc. in Visual Studio Code on Mac
Try to log in to Netflix automatically using python on your PC
[SEO] Flow / sample code when using Google Analytics API in Python
Mouse operation using Windows API in Python
Fourier series verification code written in Python
Try using the Wunderlist API in Python