やってみる

アウトプットすべく己を導くためのブログ。その試行錯誤すらたれ流す。

autopep8でコードを自動整形する(オートフォーマッタ)

 統一感のあるコードにしてくれるツール。

前回まで

インストール

pip3 install autopep8

使ってみる

コード

 a.pyは以下のと同じのを使う。

実行

autopep8 a.py

結果

#!/usr/bin/env python3
# coding: utf8
def my_Method(p):
    print('Hello Python!!')


if __name__ == '__main__':
    my_Method('value')

 改行は治った。でもやはりメソッド名が大文字を含んだままだよ。やれやれ。

ファイル上書き

 標準出力にされても困る。ファイルを更新してほしい。なので引数をかえて再試行。

autopep8 -ivr .
[file:./a.py]
--->  Applying global fix for E265
--->  2 issue(s) to fix {'E305': {5}, 'W391': {7}}
--->  1 issue(s) to fix {'E305': {6}}
--->  0 issue(s) to fix {}

flake8でチェック

flake8 .
(なにも出ない)

 OK! 前回は以下でエラーが出ていた。それがひとつも出なくなっている。

ヘルプ

autopep8 --help
usage: autopep8 [-h] [--version] [-v] [-d] [-i] [--global-config filename]
                [--ignore-local-config] [-r] [-j n] [-p n] [-a]
                [--experimental] [--exclude globs] [--list-fixes]
                [--ignore errors] [--select errors] [--max-line-length n]
                [--line-range line line] [--hang-closing] [--exit-code]
                [files [files ...]]

Automatically formats Python code to conform to the PEP 8 style guide.

positional arguments:
  files                 files to format or '-' for standard in

optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  -v, --verbose         print verbose messages; multiple -v result in more
                        verbose messages
  -d, --diff            print the diff for the fixed source
  -i, --in-place        make changes to files in place
  --global-config filename
                        path to a global pep8 config file; if this file does
                        not exist then this is ignored (default:
                        /home/pi/.config/pep8)
  --ignore-local-config
                        don't look for and apply local config files; if not
                        passed, defaults are updated with any config files in
                        the project's root directory
  -r, --recursive       run recursively over directories; must be used with
                        --in-place or --diff
  -j n, --jobs n        number of parallel jobs; match CPU count if value is
                        less than 1
  -p n, --pep8-passes n
                        maximum number of additional pep8 passes (default:
                        infinite)
  -a, --aggressive      enable non-whitespace changes; multiple -a result in
                        more aggressive changes
  --experimental        enable experimental fixes
  --exclude globs       exclude file/directory names that match these comma-
                        separated globs
  --list-fixes          list codes for fixes; used by --ignore and --select
  --ignore errors       do not fix these errors/warnings (default:
                        E226,E24,W50,W690)
  --select errors       fix only these errors/warnings (e.g. E4,W)
  --max-line-length n   set maximum allowed line length (default: 79)
  --line-range line line, --range line line
                        only fix errors found within this inclusive range of
                        line numbers (e.g. 1 99); line numbers are indexed at
                        1
  --hang-closing        hang-closing option passed to pycodestyle
  --exit-code           change to behavior of exit code. default behavior of
                        return value, 0 is no differences, 1 is error exit.
                        return 2 when add this option. 2 is exists
                        differences.

所感

 命名規則がちゃんと修正できていないのがダメ。本当に使えるんだろうか、このツール。

 中途半端なくせにコードを書き換えやがる。なんか腹立つんだよなぁ。仕事でやってるわけでないから自由に書いていたほうが気楽でいい。

対象環境

$ uname -a
Linux raspberrypi 5.10.52-v7l+ #1441 SMP Tue Aug 3 18:11:56 BST 2021 armv7l GNU/Linux