よしたく blog

ほぼ週刊で記事を書いています

コードをきれいにするため、pycodestyleを使ってみた

f:id:yoshitaku_jp:20180728144033p:plain

プログラムコードはきれいにしておきたいですよね。そのために様々な言語で規約もあるかと思います。 Pythonを独学で書いている自分としてはサンプルコードを見て真似して感覚的に覚えていったりもしました。正しいものがあるなら、それを見て正しく書いていきたいし、自分の目視じゃなくてチェックもして欲しい!そんなときに便利なのがpycodestyleです!

github.com

これを使えば、自分が今書いてるコードがPEP8にそって書かれているかチェックしてくれます!

インストール

pip install pycodestyle

使い方

対象のhoge.pyファイルをチェックするときは下のように実行します!

pycodestyle hoge.py

試しに自分で作っているスクレイピングのツールを実行してみると…

yoshitaku$ pycodestyle scraping_netkeiba/getHtml.py 
scraping_netkeiba/getHtml.py:16:1: E302 expected 2 blank lines, found 1
scraping_netkeiba/getHtml.py:24:1: E302 expected 2 blank lines, found 1
scraping_netkeiba/getHtml.py:28:80: E501 line too long (107 > 79 characters)
scraping_netkeiba/getHtml.py:31:1: E302 expected 2 blank lines, found 1
scraping_netkeiba/getHtml.py:31:23: E231 missing whitespace after ','
scraping_netkeiba/getHtml.py:38:1: E302 expected 2 blank lines, found 1
scraping_netkeiba/getHtml.py:38:22: E231 missing whitespace after ','
scraping_netkeiba/getHtml.py:42:43: E712 comparison to True should be 'if cond is True:' or 'if cond:'
scraping_netkeiba/getHtml.py:44:33: E221 multiple spaces before operator
scraping_netkeiba/getHtml.py:44:46: E712 comparison to False should be 'if cond is False:' or 'if not cond:'
scraping_netkeiba/getHtml.py:59:38: E251 unexpected spaces around keyword / parameter equals
scraping_netkeiba/getHtml.py:59:80: E501 line too long (87 > 79 characters)
scraping_netkeiba/getHtml.py:61:80: E501 line too long (81 > 79 characters)
scraping_netkeiba/getHtml.py:73:38: E231 missing whitespace after ','
scraping_netkeiba/getHtml.py:75:53: E231 missing whitespace after ','
scraping_netkeiba/getHtml.py:80:44: E231 missing whitespace after ','
scraping_netkeiba/getHtml.py:80:55: E712 comparison to False should be 'if cond is False:' or 'if not cond:'
scraping_netkeiba/getHtml.py:80:80: E501 line too long (114 > 79 characters)
scraping_netkeiba/getHtml.py:80:95: E231 missing whitespace after ','
scraping_netkeiba/getHtml.py:83:48: E221 multiple spaces before operator
scraping_netkeiba/getHtml.py:89:48: E221 multiple spaces before operator
scraping_netkeiba/getHtml.py:92:49: E712 comparison to True should be 'if cond is True:' or 'if cond:'
scraping_netkeiba/getHtml.py:100:16: E221 multiple spaces before operator
scraping_netkeiba/getHtml.py:102:34: E231 missing whitespace after ','
scraping_netkeiba/getHtml.py:103:38: E231 missing whitespace after ','
scraping_netkeiba/getHtml.py:104:42: E231 missing whitespace after ','
scraping_netkeiba/getHtml.py:106:80: E501 line too long (81 > 79 characters)
scraping_netkeiba/getHtml.py:108:56: E231 missing whitespace after ','
scraping_netkeiba/getHtml.py:109:35: E712 comparison to False should be 'if cond is False:' or 'if not cond:'
scraping_netkeiba/getHtml.py:111:53: E221 multiple spaces before operator
scraping_netkeiba/getHtml.py:114:56: E221 multiple spaces before operator
scraping_netkeiba/getHtml.py:114:80: E501 line too long (81 > 79 characters)
scraping_netkeiba/getHtml.py:121:5: E722 do not use bare 'except'

辛い。 まぁ一つ一つ地道に直していこう。

なんとなく怒られている内容もわかりますが、エラー内容の一覧もあるのでご参照ください

Introduction — pycodestyle 2.4.0 documentation