PEP 8 and autopep8

 

PyCon APAC 2013 LT / 2013.09.15
Hideo Hattori

whoami

PEP 8

  • http://www.python.org/dev/peps/pep-0008/
  • coding conventions for the Python code
  • 変数の命名規則、スペース数とか空行の数とか結構細かく書かれてる
  • 2013.08.01に更新された

PEP 8 modernisation

  • 1行あたりの文字数制限に関する記述
  • 例外に関する記述
  • Python1.5.2に関する記述が削除されてたり
  • Python2/3に関する記述が明記されたり
  • http://hg.python.org/peps/rev/fb24c80e9afb

About autopep8

  • simple command-line tool
  • automatic formatting for Python Code
  • require pep8
  • v0.9.4 (2013.09.08)
$ pip install autopep8

For Example

invalid PEP 8 code

import sys, os

n = True
if n != None:
    print("none")

def f(x): return 2 * x

Output Auto-Formatting Code

$ autopep8 invalid.py
import sys
import os

n = True
if n != None:
    print("none")


def f(x):
    return 2 * x

Aggressive

$ autopep8 -a invalid.py
import sys
import os

n = True
if n is not None:
    print("none")


def f(x):
    return 2 * x

Other Feature

  • ignore/select errors
  • max-line
  • recursive/jobs

いつも心の片隅にPEP 8を

ぜひautopep8を使ってみてください

 

https://github.com/hhatto/autopep8

Thank you