파이썬 & 오픈소스 개발 Tip과 강좌
이곳은 파이썬과 여러 오픈소스 기반 프레임워크 관련 Tip과 강좌 게시판 입니다. 관련 개발을 진행하면서 알아내신 Tip이나 강좌와 새로운 소식을 적어 주시면 다른 공부하는 분들에게 큰 도움이 됩니다. 감사합니다. SQLER.com은 개발자와 IT전문가의 지식 나눔을 실천하기 위해 노력하고 있습니다.
팀 프로젝트에서 코드 작성 후 코드의 syntax나 패턴을 일치시킬 필요가 있다.
이때 pep8과 같은 도구로 보통 linting한다.
autopep8은 이런 코드 린팅을 자동으로 돕는 도구이다. 하지만, 의도치 않게 작동할 경우도 있으니 주의가 필요.
설치 및 실행
$ pip install --upgrade autopep8
이렇게 pip로 설치한다. 이어서 아래와 같은 방식으로 실행한다.
$ autopep8 --in-place --aggressive --aggressive <filename>
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)
그러면, 이런 위와 같은 패턴의 코드가 아래처럼 자동 수정된다.
import math import 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 ('' in {'f': 2}) in {'has_key() is deprecated': True} 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)
다시 강조하지만, 의도치 않는 패턴으로 변경될 수 있으니 주의해야 한다.
참고링크 :