小村の開発環境構築(16) Pythonのリンターフォーマッター設定
こんばんは!小村だよ!
今日は前回やったpython系のインストールの続きをやっていくよ!
よろしくね!
やること
python
の拡張機能をインストールautopep8
flake8
のインストールautopep8
flake8
の設定(失敗)autopep8
flake8
の再設定
前提条件
- Ubuntuにリモート接続していること(これまでの環境構築参照)
手順
python
の拡張機能をインストール
autopep8
flake8
のインストール
次はフォーマッタとリンター!
開発するうえでフォーマッターとリンターは最初に着手したいよねー
というわけで
autopep8
flake8
をインストールするよ!
pip install autopep8 pip install flake8
autopep8
flake8
の設定(失敗)
上記参考サイトの通り、下記を順番にやっていくよ!
- 追記:うまくいかなかったので次の再設定殻が本題になるよ。ここは飛ばしていいよ!
- pylintの無効化
- pythonのlintingの有効化
- flake8の有効化
- ファイル保存時のlinting
- フォーマットのstyleをautopep8に設定
- 保存時の自動生計を有効化
- 1行文字数制限を外す
- 最終的に追加されたのは下図でした。半分くらいはデフォルトで設定されてたね
おけ完璧!flake8とautopep8の動作確認していきたいと思います!
autopep8公式の整形用サンプルソースを整形してみます
import math, sys; def example1(): ####This is a long comment. This should be wrapped to fit within 72 characters. some_tuple=( 1,2, 3,'a' ); some_variable={'long':'Long code lines should be wrapped within 79 characters.', 'other':[math.pi, 100,200,300,9876543210,'This is a long string that goes on'], 'more':{'inner':'This whole logical line should be wrapped.',some_tuple:[1, 20,300,40000,500000000,60000000000000000]}} return (some_tuple, some_variable) def example2(): return {'has_key() is deprecated':True}.has_key({'f':2}.has_key('')); class Example3( object ): def __init__ ( self, bar ): #Comments should have a space after the hash. if bar : bar+=1; bar=bar* bar ; return bar else: some_string = """ Indentation in multiline strings should not be touched. Only actual code should be reindented. """ return (sys.path, some_string)
いや、autopep8後にflake8のlintが反応するんだが????
えっ、それを解消してくれる参考サイトではなかったの……?
文字数制限はまぁいいとして、改行数が許せぬ……c
autopep8とflake8の再設定
文句言ってても仕方ないので修正しまーす!
色々回って最終的に下記設定に落ち着きました
設定先もリモートタブじゃなくてワークスペースタブに変更
上の設定まじで意味ないな……不要と追記しておこう。
{ "[python]": { "editor.tabSize": 4, "editor.formatOnSave": true, "editor.formatOnPaste": false, "editor.formatOnType": false, }, "python.linting.enabled": true, "python.linting.lintOnSave": true, "python.linting.pylintEnabled": false, "python.linting.flake8Enabled": true, "python.linting.flake8Args": [ "--max-line-length=120", ], "python.formatting.provider": "autopep8", "python.formatting.autopep8Args": [ "--aggressive", "--aggressive", ], "files.autoSave": "afterDelay", "files.autoSaveDelay": 10000, }
倍近くの行に成形されるね!
上の設定と大きく変わってるのは、
--aggressive
2つ付けてるところかな整形後のコードを見ると、下記とかひどいので、こういうのは都度調整だね
some_string = """ Indentation in multiline strings should not be touched. Only actual code should be reindented. """
おわりに
今日はこの辺でおわりますー!
すんなり言ってたらアプリ作成までやりたかったんだけどねー次に回す!
ではでは、ちゃお~~~!