pythonでコーディング規約(pep8)に準拠しているかチェック方法

pythonでコーディング規約といえばpep8。
ソースコードがpep8に準拠しているかチェックするにはpycodestyleというのを使うそうです。

ちなみにpep8の日本語ドキュメントは [こちら](https://pep8-ja.readthedocs.io/ja/latest/)。

# pycodestyleインストール

```
> pip install pycodestyle
Collecting pycodestyle
  Downloading https://files.pythonhosted.org/packages/0e/0c/04a353e104d2f324f8ee5f4b32012618c1c86dd79e52a433b64fceed511b/pycodestyle-2.5.0-py2.py3-none-any.whl (51kB)
    100% |████████████████████████████████| 51kB 1.3MB/s
Installing collected packages: pycodestyle
Successfully installed pycodestyle-2.5.0
```

# pycodestyle 使い方

pycodestyle [filename or directory]

## 特定のファイルをチェック

```
> pycodestyle Test.py
Test.py:6:5: E301 expected 1 blank line, found 0
Test.py:8:25: E203 whitespace before ','
Test.py:9:5: E301 expected 1 blank line, found 0
Test.py:16:1: E305 expected 2 blank lines after class or function definition, found 1
Test.py:17:19: E225 missing whitespace around operator
Test.py:38:1: E303 too many blank lines (3)
```

## ディレクトリまるごとチェック

```
> pycodestyle ./
.\Test.py:6:5: E301 expected 1 blank line, found 0
.\Test.py:8:25: E203 whitespace before ','
.\Test.py:9:5: E301 expected 1 blank line, found 0
.\Test.py:16:1: E305 expected 2 blank lines after class or function definition, found 1
.\Test.py:17:19: E225 missing whitespace around operator
.\Test.py:38:1: E303 too many blank lines (3)
.\Test2.py:2:27: E231 missing whitespace after ':'
.\Test2.py:2:40: E231 missing whitespace after ':'
```

## オプション

#### --first

同じ指摘は1つのみ表示する。

```
> pycodestyle --first Test.py
Test.py:6:5: E301 expected 1 blank line, found 0
Test.py:8:25: E203 whitespace before ','
Test.py:16:1: E305 expected 2 blank lines after class or function definition, found 1
Test.py:17:19: E225 missing whitespace around operator
Test.py:38:1: E303 too many blank lines (3)
```

#### --show-source

ソースコード上の指摘箇所も表示してくれます。

```
> pycodestyle --show-source Test.py
Test.py:6:5: E301 expected 1 blank line, found 0
    def __lt__(self, other):
    ^
Test.py:8:25: E203 whitespace before ','
        return (self.cnt , self.age) > (other.cnt, self.age)
                        ^
Test.py:9:5: E301 expected 1 blank line, found 0
    def __repr__(self):
    ^
Test.py:16:1: E305 expected 2 blank lines after class or function definition, found 1
dict = {}
^
Test.py:17:19: E225 missing whitespace around operator
member_data_file =open('data.csv')
                  ^
Test.py:38:1: E303 too many blank lines (3)
# 並べ替える
^
```

#### --show-pep8

pep8も表示してくれます。
見づらいので--show-sourceとセットで使う方がよさそうです。

```
> pycodestyle --show-source --show-pep8 Test.py
Test.py:6:5: E301 expected 1 blank line, found 0
    def __lt__(self, other):
    ^
    Separate top-level function and class definitions with two blank
    lines.

    Method definitions inside a class are separated by a single blank
    line.

    Extra blank lines may be used (sparingly) to separate groups of
    related functions.  Blank lines may be omitted between a bunch of
    related one-liners (e.g. a set of dummy implementations).

    Use blank lines in functions, sparingly, to indicate logical
    sections.

    Okay: def a():\n    pass\n\n\ndef b():\n    pass
    Okay: def a():\n    pass\n\n\nasync def b():\n    pass
    Okay: def a():\n    pass\n\n\n# Foo\n# Bar\n\ndef b():\n    pass
    Okay: default = 1\nfoo = 1
    Okay: classify = 1\nfoo = 1

    E301: class Foo:\n    b = 0\n    def bar():\n        pass
    E302: def a():\n    pass\n\ndef b(n):\n    pass
    E302: def a():\n    pass\n\nasync def b(n):\n    pass
    E303: def a():\n    pass\n\n\n\ndef b(n):\n    pass
    E303: def a():\n\n\n\n    pass
    E304: @decorator\n\ndef a():\n    pass
    E305: def a():\n    pass\na()
    E306: def a():\n    def b():\n        pass\n    def c():\n        pass
```

#### --help

オプション見るとき使います。

```
> pycodestyle --help
Usage: pycodestyle [options] input ...

Options:
  --version            show program's version number and exit
  -h, --help           show this help message and exit
  -v, --verbose        print status messages, or debug with -vv
  -q, --quiet          report only file names, or nothing with -qq
  -r, --repeat         (obsolete) show all occurrences of the same error
  --first              show first occurrence of each error
  --exclude=patterns   exclude files or directories which match these comma
                       separated patterns (default:
                       .svn,CVS,.bzr,.hg,.git,__pycache__,.tox)
  --filename=patterns  when parsing directories, only check filenames matching
                       these comma separated patterns (default: *.py)
  --select=errors      select errors and warnings (e.g. E,W6)
  --ignore=errors      skip errors and warnings (e.g. E4,W) (default:
                       E121,E123,E126,E226,E24,E704,W503,W504)
  --show-source        show source code for each error
  --show-pep8          show text of PEP 8 for each error (implies --first)
  --statistics         count errors and warnings
  --count              print total number of errors and warnings to standard
                       error and set exit code to 1 if total is not null
  --max-line-length=n  set maximum allowed line length (default: 79)
  --max-doc-length=n   set maximum allowed doc line length and perform these
                       checks (unchecked if not set)
  --hang-closing       hang closing bracket instead of matching indentation of
                       opening bracket's line
  --format=format      set the error format [default|pylint|<custom>]
  --diff               report changes only within line number ranges in the
                       unified diff received on STDIN

  Testing Options:
    --benchmark        measure processing speed

  Configuration:
    The project options are read from the [pycodestyle] section of the
    tox.ini file or the setup.cfg file located in any parent folder of the
    path(s) being processed.  Allowed options are: exclude, filename,
    select, ignore, max-line-length, max-doc-length, hang-closing, count,
    format, quiet, show-pep8, show-source, statistics, verbose.

    --config=path      user config file location
```