PyCharm使用autopep8按PEP8風格自動排版Python代碼

前言

autopep8是一個將Python代碼自動排版爲PEP8風格的小工具。它使用pep8工具來決定代碼中的哪部分須要被排版。autopep8能夠修復大部分pep8工具中報告的排版問題。python

PyCharm使用autopep8按PEP8風格自動排版Python代碼

更新記錄

2018年12月27日 - 初稿git

閱讀原文 - https://wsgzao.github.io/post...github

擴展閱讀express

autopep8 - https://pypi.python.org/pypi/...bash

autopep8簡介

使用 autopep8 前咱們有必要先了解下 PEP 8 -- Style Guide for Python Code

autopep8 automatically formats Python code to conform to the PEP 8 style guide. It uses the pycodestyle utility to determine what parts of the code needs to be formatted. autopep8 is capable of fixing most of the formatting issues that can be reported by pycodestyle.app

https://www.python.org/dev/pe...less

https://pypi.python.org/pypi/...ide

安裝和使用autopep8

autopep8是一個開源的命令行工具,它可以將Python代碼自動格式化爲PEP8風格。autopep8使用pycodestyle工具來決定代碼中的哪部分須要被格式化,這可以修復大部分pycodestyle工具中報告的排版問題。autopep8自己也是一個Python語言編寫的工具,所以,咱們能夠直接使用pip進行安裝:函數

pip install autopep8
autopep8 --in-place optparse.py 

# To modify a file in place (with aggressive level 2):
autopep8 --in-place --aggressive --aggressive <filename>

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:
                        ~/.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.

--in-place相似於sed命令的-i選項,若是不包含--in-place選項,則會將autopep8格式化之後的代碼直接輸出到控制檯。咱們可使用這種方式檢查autopep8的修改,使用--in-place則會直接將結果保存到源文件中。工具

咱們來看一個完整的例子,本例中使用的代碼以下:

import os, sys  
 
def main():  
    print [item for item in os.listdir('.') if item.endswith('.py')];  
    print sys.version  
 
if __name__ == '__main__':  
    main()

這段代碼存在三個問題:

  1. 導入的時候,應該每一行只導入一個包;
  2. 包導入和函數定義之間應該空兩行;
  3. Python代碼末尾不須要分號。

接下來,咱們將使用pycodestyple檢查這段代碼,而後使用autopep8將代碼格式化成符合PEP 8風格的代碼。

使用pycodestyle檢查代碼可檢測到代碼中有三個地方不符合PEP 8規範,以下所示:

pycodestyle hello.py  
hello.py:1:10: E401 multiple imports on one line  
hello.py:3:1: E302 expected 2 blank lines, found 1  
hello.py:4:69: E703 statement ends with a semicolon

使用autopep8格式可以轉換Python代碼。在這個例子中,autopep8順利地幫咱們修復了全部問題,以下所示:

$ autopep8 hello.py  
import os  
import sys  
 
 
def main():  
    print [item for item in os.listdir('.') if item.endswith('.py')]  
    print sys.version  
 
 
if __name__ == '__main__':  
    main()

這個時候若是查看源文件的話,會發現仍是和原來同樣,並無修正爲符合PEP 8規範的代碼。前面說過,不指定--in-place選項,只會將結果輸出到命令行。若是咱們使用--in-place選項,將不會有任何輸出,autopep8會直接修改源文件。

$ autopep8 --in-place hello.py

autopep8還存在--aggressive選項,使用該選項會執行更多實質性的更改,能夠屢次使用以達到更佳的效果。

以官網爲例咱們編寫一個test_autopep8.py

Before running 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)
After running autopep8.
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)

autopep8能夠修復的問題

autopep8 fixes the following issues reported by pycodestyle:

https://github.com/PyCQA/pyco...

E101 - Reindent all lines.
E11  - Fix indentation.
E121 - Fix indentation to be a multiple of four.
E122 - Add absent indentation for hanging indentation.
E123 - Align closing bracket to match opening bracket.
E124 - Align closing bracket to match visual indentation.
E125 - Indent to distinguish line from next logical line.
E126 - Fix over-indented hanging indentation.
E127 - Fix visual indentation.
E128 - Fix visual indentation.
E129 - Fix visual indentation.
E131 - Fix hanging indent for unaligned continuation line.
E133 - Fix missing indentation for closing bracket.
E20  - Remove extraneous whitespace.
E211 - Remove extraneous whitespace.
E22  - Fix extraneous whitespace around keywords.
E224 - Remove extraneous whitespace around operator.
E225 - Fix missing whitespace around operator.
E226 - Fix missing whitespace around arithmetic operator.
E227 - Fix missing whitespace around bitwise/shift operator.
E228 - Fix missing whitespace around modulo operator.
E231 - Add missing whitespace.
E241 - Fix extraneous whitespace around keywords.
E242 - Remove extraneous whitespace around operator.
E251 - Remove whitespace around parameter '=' sign.
E252 - Missing whitespace around parameter equals.
E26  - Fix spacing after comment hash for inline comments.
E265 - Fix spacing after comment hash for block comments.
E266 - Fix too many leading '#' for block comments.
E27  - Fix extraneous whitespace around keywords.
E301 - Add missing blank line.
E302 - Add missing 2 blank lines.
E303 - Remove extra blank lines.
E304 - Remove blank line following function decorator.
E305 - Expected 2 blank lines after end of function or class.
E306 - Expected 1 blank line before a nested definition.
E401 - Put imports on separate lines.
E402 - Fix module level import not at top of file
E501 - Try to make lines fit within --max-line-length characters.
E502 - Remove extraneous escape of newline.
E701 - Put colon-separated compound statement on separate lines.
E70  - Put semicolon-separated compound statement on separate lines.
E711 - Fix comparison with None.
E712 - Fix comparison with boolean.
E713 - Use 'not in' for test for membership.
E714 - Use 'is not' test for object identity.
E721 - Use "isinstance()" instead of comparing types directly.
E722 - Fix bare except.
E731 - Use a def when use do not assign a lambda expression.
W291 - Remove trailing whitespace.
W292 - Add a single newline at the end of the file.
W293 - Remove trailing whitespace on blank line.
W391 - Remove trailing blank lines.
W503 - Fix line break before binary operator.
W504 - Fix line break after binary operator.
W601 - Use "in" rather than "has_key()".
W602 - Fix deprecated form of raising exception.
W603 - Use "!=" instead of "<>"
W604 - Use "repr()" instead of backticks.
W605 - Fix invalid escape sequence 'x'.
W690 - Fix various deprecated code (via lib2to3).

Pycharm安裝autopep8

pip安裝autopep8: pip install autopep8

PyCharm -> Preferences -> Tools -> Extends Tools -> 點擊+加號

Name: autopep8
Tools settings:

  • Programs: autopep8
  • Parameters: --in-place --aggressive --aggressive $FilePath$
  • Working directory: $ProjectFileDir$
  • Advanced Options -> Output Filters: $FILE_PATH$\:$LINE$\:$COLUMN$\:.*

Python之禪

最後以Python之禪做爲結束語
wangao@wangao-MAC  ~  python3
Python 3.7.1 (default, Nov  6 2018, 18:46:03)
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
翻譯

美 優於 醜

明確 優於 隱晦 (1)

簡單 優於 複雜

複雜 也好過 繁複 (2)

扁平 優於 嵌套

稀疏 優於 擁擠

可讀性很重要(3)

當然代碼實用與否 比潔癖更重要,

咱們覺得的特例也每每沒有特殊到必須打破上述規則的程度

除非刻意地靜默,

不然不要無端忽視異常(4)

若是遇到模棱兩可的邏輯,請不要自做聰明地瞎猜。

應該提供一種,且最好只提供一種,一目瞭然的解決方案

固然這是無法一蹴而就的,除非你是荷蘭人(5)

當然,馬上着手 好過 永遠不作。

然而,永遠不作 也好過 不審慎思考一擼袖子就莽着幹

若是你的實現很難解釋,它就必定不是個好主意

即便你的實現簡單到爆,它也有多是個好辦法

命名空間大法好,不搞不是地球人!

註釋
  1. 該引入的包一個一個列出來不要合併;不要用星號;不要在方法裏藏意想不到的的反作用,等等等等。還一個例子,另一種著名的軟件設計原則 Convention over Configuration(約定優於配置)若是作得不謹慎就會違背這條
  2. SO: 必要的複雜邏輯是不免的,而繁複囉嗦的代碼是不可接受的
  3. Readability counts 不能翻譯成可讀性計數啊喂
  4. 實操中不少人不注意 catch 完就 log 一下 就無論了,這樣很差。軟件界通常都講 Let it fail,學名爲 Fail-fast 法則。簡而言之就是整個項目週期中越早暴露的問題,其修復成本越低
  5. 本文做者 Tim peters 解釋說這裏的荷蘭人指的是 Python 的做者 Guido van Rossum
相關文章
相關標籤/搜索