pycharm集成autopep8

在Pycharm中配置autopep8

關於PEP 8

PEP 8,Style Guide for Python Code,是Python官方推出的Python編碼風格的約定,雖然這不是硬性的規定,可是若是Python程序員都儘可能遵循這個文檔,那麼編碼風格的統一會讓代碼的可讀性大大提高。經過它,能夠修復大部分PEP8工具中報告的代碼排版問題。舉個官網的例子:python

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)

這是一個比較極端狀況的例子,在使用了autopep8自動修復後:程序員

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)

Pycharm中使用autopep8做爲擴展工具

安裝autopep8

pip install autopep8

Pycharm進行設置:

Settings–>Tools–>External Tools 點擊添加按鈕express

Name:autopep8(能夠自定義)
Tools settings:
Programs:autopep8(不能修改)
Parameters:--in-place --aggressive --aggressive $FilePath$
Working directory:$ProjectFileDir$

點擊Output Filesapp

  • 點擊添加,名稱能夠任意填寫
  • Regular expression to match output:$FILE_PATH$:$LINE$:$COLUMN$:.*

實際使用

在右擊上代碼–>External Tool–>autopep8,Pycharm自動調用了autopep8對當前文件進行PEP8優化。ide

autopep8的一些設置點

在上邊說到,在Parameters的設置是:--in-place --aggressive --aggressive $FilePath$工具

  • –in-place 表明會直接修改源文件優化

  • –aggressive autopep8默認只修復空白,對齊相關的PEP8問題,加入--aggressive設置,會增長修復如 x == None 修復爲 x is None,{「a」: 1, 「b」: 2}.has_key(‘a’) 修復爲’a’ in {「a」: 1, 「b」: 2}ui

  • –ignore 忽略PEP8檢查項編碼

若是隻打算用autopep8來修復空格,空行這一類的排版問題,同時要忽略每一行長度過長的檢測(E501 - Try to make lines fit within –max-line-length characters.),因此最終設置是code

--in-place --ignore=E501 $FilePath$
相關文章
相關標籤/搜索