Python 코드를 자동으로 포맷하려면(autoopep8)

개요


autoopep8을 사용하여 더러운python 코드를 깨끗한 코드로 수정했습니다.

컨디션


$ sw_vers
ProductName:	Mac OS X
ProductVersion:	10.15.6
BuildVersion:	19G2021

$ python3 --version
Python 3.7.3

설치하다.


$ pip3 install autopep8

### こちらもあると嬉しい
$ pip3 install flake8

사용법


autoopep8은 PEP8 스타일에 따라 코드 형식을 지정할 수 있습니다.
-d에서 현재 코드와의 차이를 볼 수 있습니다.
이번에 슬랙 투고 코드(autoopep 8 test.py)를 수정했다.
4
### -d, --diff: print the diff for the fixed source
$ autopep8 --diff autopep8test.py
변경된 코드가 문제 없이 적응된 경우
### -i, --in-place: make changes to files in place
$ autopep8 --in-place autopep8test.py
### もしくは
$ autopep8 autopep8test.py > newfile.py

결과


원시 코드


$ cat autopep8test.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests, json
def post(bot_name:str, msg:str) -> int:
    CHANNEL = '<<CHANNEL>>'; TOKEN = '<<TOKEN>>'
    URL = 'https://slack.com/api/chat.postMessage'

    headers = { 'Content-Type': 'application/json' }
    data = {
      'channel': CHANNEL, 'token': TOKEN,
        'username': bot_name, 'text': msg
        }
    requests.post(url=URL, headers=headers, data=json.dumps(data))

post('mybot', 'hello')

diff


$ autopep8 --diff autopep8test.py
--- original/autopep8test.py
+++ fixed/autopep8test.py
@@ -1,15 +1,20 @@
 #!/usr/bin/env python3
 # -*- coding: utf-8 -*-
-import requests, json
-def post(bot_name:str, msg:str) -> int:
-    CHANNEL = '<<CHANNEL>>'; TOKEN = '<<TOKEN>>'
+import requests
+import json
+
+
+def post(bot_name: str, msg: str) -> int:
+    CHANNEL = '<<CHANNEL>>'
+    TOKEN = '<<TOKEN>>'
     URL = 'https://slack.com/api/chat.postMessage'

-    headers = { 'Content-Type': 'application/json' }
+    headers = {'Content-Type': 'application/json'}
     data = {
-      'channel': CHANNEL, 'token': TOKEN,
+        'channel': CHANNEL, 'token': TOKEN,
         'username': bot_name, 'text': msg
-        }
+    }
     requests.post(url=URL, headers=headers, data=json.dumps(data))

+
 post('mybot', 'hello')

완료 코드


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests
import json


def post(bot_name: str, msg: str) -> int:
    CHANNEL = '<<CHANNEL>>'
    TOKEN = '<<TOKEN>>'
    URL = 'https://slack.com/api/chat.postMessage'

    headers = {'Content-Type': 'application/json'}
    data = {
        'channel': CHANNEL, 'token': TOKEN,
        'username': bot_name, 'text': msg
    }
    requests.post(url=URL, headers=headers, data=json.dumps(data))


post('mybot', 'hello')

대응 문제


4
$ flake8 autopep8test.py
autopep8test.py:3:16: E401 multiple imports on one line
autopep8test.py:4:1: E302 expected 2 blank lines, found 0
autopep8test.py:4:18: E231 missing whitespace after ':'
autopep8test.py:4:27: E231 missing whitespace after ':'
autopep8test.py:5:28: E702 multiple statements on one line (semicolon)
autopep8test.py:8:16: E201 whitespace after '{'
autopep8test.py:8:51: E202 whitespace before '}'
autopep8test.py:11:9: E131 continuation line unaligned for hanging indent
autopep8test.py:15:1: E305 expected 2 blank lines after class or function definition, found 1
	
↓
	
$ flake8 autopep8test.py
### 出力なし
diff와 flake8을 확인하면서 진행하시면 안심하실 수 있습니다!

좋은 웹페이지 즐겨찾기