Comply with Python coding standard PEP8

In this Qiita series, Python has been used as the main language for data analysis. By the way, do you know the Python coding standard PEP8?

Source Code Style Guide PEP8

Source code generally takes longer to read than to write, and based on that fact, it was created based on the idea of "unifying the style and writing easy-to-read code." Is this guide.

Style Guide for Python Code http://legacy.python.org/dev/peps/pep-0008/

The head family is of course in English, but volunteers have translated it into Japanese.

PEP8 Japanese translation https://github.com/mumumu/pep8-ja

Either way, if you use Python, you should definitely read it.

Automatically check the source code

By the way, it is a lot of work to hit a long guide into your head and check it without omission. Being a human being, you may make mistakes. Therefore, it is useful to have a tool to check whether the guidelines are followed.

In the Python world, there is a command with the same name as the guideline pep8. You can check by using this.

Use PEP8 Check Tool

First, install it with pip as follows.

At this time, it is convenient to include pyflakes as well. What is pyflakes? It's a tool that checks Python grammar. Not only does this detect obvious grammatical errors, it also detects useless code such as unwanted import statements. Let's introduce it together.

pip install pep8
pip install pyflakes

Django For those who are creating web applications with the framework, [Introduce flake8 as an alternative to pyflakes](http://naoina.plog.la/2013/05 / 09233720000000) and happiness will be realized.

Perform Python grammar checks together

After installing pep8 and pyflakes, create a shell script as follows and put it in a directory in your path. By the way, I use this as a command called pyck.

setup_environment() {
    #Specify the Python installation path
    PYTHON_PATH=/opt/python/current
    IGNORE_ERRORS=E221,E701,E202
}

main() {
    setup_environment
    which $PYTHON_PATH/bin/pyflakes > /dev/null || exit 254
    which $PYTHON_PATH/bin/pep8 > /dev/null || exit 254
    $PYTHON_PATH/bin/pyflakes $*
    $PYTHON_PATH/bin/pep8 --ignore=$IGNORE_ERRORS --repeat $*
    exit 0
}

main $*

If you do pyck your_file.py for the file you want to check, grammar check and conformity check to the guideline will be done. If nothing is output, there is no error. In the above script, if pep8 or pyflakes itself does not exist, it ends with a return value of 254.

Also, some projects may have trouble trying to fit the guidelines perfectly. In such cases, you can specify the guideline number you want to ignore with the pep8 --ignore option as shown above.

Please refer to PEP8 documentation for which error number corresponds to which grammar.

Actually check the source code

Let's check the syntax as an example. This time, I prepared the following files as a sample.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys, os

def list_files(path):
    for root, dirs, files in os.walk(path):
        for filename in files:
            fullname = os.path.join(root, filename)
            print( fullname )

def main(args):
    path = args[1]
    list_files(path)

if __name__ == '__main__':
    if len(sys.argv) > 1:
        main(sys.argv)

This file os_walk.py is a simple program that lists the file list in the directory specified in the argument and outputs it to the screen.

The program itself does work fine. However, when a syntax check was performed on this, the following guideline violations were found.

$ pyck os_walk.py 
os_walk.py:4:11: E401 multiple imports on one line
os_walk.py:6:1: E302 expected 2 blank lines, found 1
os_walk.py:10:19: E201 whitespace after '('
os_walk.py:12:1: E302 expected 2 blank lines, found 1
os_walk.py:19:1: W391 blank line at end of file

In this way, if you write the source code without thinking about it, you will immediately violate the guidelines.

Use more sophisticated check tools

The pep8 message will be a little hard to see until you get used to where to fix it. You will be even more happy with use py.test, which will display the message more kindly.

First, install with pip as usual.

pip install pep8 pytest pytest-pep8

All you have to do is check the desired file with py.test --pep8 instead of the pep8 command. Let's check os_walk.py earlier.

2.png

A more detailed message was displayed than the plain pep8. In this case, it is obvious where in the source code the problem is and how to fix it.

There is also a tool called PyCharm that I haven't used. You may use this.

Automatically modify the source code

Now that you can check the compliance with the source code guidelines with a tool, fixing all of this manually can sometimes be a daunting task. Therefore, use the automatic correction tool. autopep8 is exactly this auto-fix tool. Let's introduce it immediately.

pip install autopep8

Let's use the tool as autopep8 os_walk.py for the sample code above. Then, the modified source code was output to the standard output as shown below.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import os


def list_files(path):
    for root, dirs, files in os.walk(path):
        for filename in files:
            fullname = os.path.join(root, filename)
            print(fullname)


def main(args):
    path = args[1]
    list_files(path)

if __name__ == '__main__':
    if len(sys.argv) > 1:
        main(sys.argv)

Certainly the problem seems to be resolved. To actually write this to a file and reflect it, use the -i option like autopep8 -i os_walk.py.

If you want to modify the source code under the current directory at once, you can modify it all at once by doing the following.

pep8 . | cut -d: -f 1 | sort | uniq | xargs autopep8 -i

Also, like pep8, autopep8 allows you to specify the error number you want to ignore with the --ignore option.

However, doing autopep8 -i without thinking is a pretty rough practice, so check the diff, check the grammar with pyflakes, nose, etc. Don't forget to run tests to ensure quality.

If you improve the shell script as follows, you can specify the -i option to automatically fix it with autopep8, and without the option, you can just check it.

run_check() {
    $PYTHON_PATH/bin/flake8 --ignore=$IGNORE_ERRORS $*
}

autofix() {
    shift && $PYTHON_PATH/bin/autopep8 --ignore=$IGNORE_ERRORS -v -i $*
}

main() {
    setup_environment
    which $PYTHON_PATH/bin/autopep8 > /dev/null || exit 254
    which $PYTHON_PATH/bin/flake8 > /dev/null || exit 254
    test "$1" = "-i" || run_check $*
    test "$1" = "-i" && autofix $*
}

main $*

Check from the editor

By the way, when this happens, you will want to check the syntax from your usual Emacs. In such a case, I recommend Emacs Lisp called python-pep8.el.

python-pep8.el https://gist.github.com/ieure/302847

When actually using it, specify the path of pep8. Rewrite the path as follows:

;; /opt/python/If current is the Python installation path
(defcustom python-pep8-command "/opt/python/current/bin/pep8"
  "PEP8 command."
  :type '(file)
  :group 'python-pep8)

After that, you can load python-pep8.el in a directory that is in the Emacs load path. The following Emacs Lisp loads python-pep8 and assigns C-c p as the key binding for syntax checking.

(when (load "python-pep8")
  (define-key global-map "\C-c\ p" 'python-pep8))

Now you can open the Python code in Emacs and C-c p to check the syntax with pep8.

3.png

You can now seamlessly check syntax and modify it.

Note that C-c p will work while editing any file as it is, so it is better to use the hook of python-mode.

(load "python-pep8")
(setq python-mode-hook
  (function (lambda ()
    (local-set-key "\C-c\ p" 'python-pep8))))
    (setq auto-mode-alist (cons '("\\.py$" . python-mode) auto-mode-alist))
    (setq interpreter-mode-alist (cons '("python" . python-mode)
                                       interpreter-mode-alist)))

Emacs also allows you to constantly perform a PEP8 conformance check and visualize it while editing the source code. This is summarized in a separate article, so please see if you are using Emacs.

Summary

PEP8 is a must-have source code style guide for all Python programmers. Make full use of tools and write the code so that it complies with the guidelines as much as possible.

Recommended Posts

Comply with Python coding standard PEP8
Building an environment to comply with the Python coding standard (PEP8) with Eclipse + PyDev
Let's summarize the Python coding standard PEP8 (1)
Let's summarize the Python coding standard PEP8 (2)
[Python coding standard] PEP 8 vs Google Style
Matrix representation with Python standard input
RPC completed with standard Python3 modules
python> coding guide> PEP 0008 --Style Guide for Python Code
UnicodeEncodeError struggle with standard output of python3
Statistics with python
With PEP8 and PEP257, Python coding that is not embarrassing to show to people!
Python with Go
[Python] Standard input
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
First Python ~ Coding 2 ~
Bingo with python
Zundokokiyoshi with python
Compliant with pep8
Excel with Python
Microcomputer with Python
Cast with python
Read wav files with only Python standard packages
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Serial communication with python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3
[Python] Redirect with CGIHTTPServer
Voice analysis with python
Get standard output in real time with Python subprocess
Think yaml with python
Operate Kinesis with Python
Getting Started with Python
Use DynamoDB with Python
Zundko getter with python