天天看點

Pycharm 配置自動格式化工具 Black Autopep8的使用

首先,pycharm自帶格式化功能,即 code 菜單下的 reformat code 功能

另外,還可以通過插件/工具等方式配置其他格式化工具

如下,格式化功能會出現在右鍵菜單中

Pycharm 配置自動格式化工具 Black Autopep8的使用

不過,也可以配置為儲存檔案時自動格式化,通過配置檔案螢幕 File Watchers 來實作

通過pip安裝black後,按下圖添加一個 File Watchers 配置即可.當然也可以使用autopep8,yapf等工具

Pycharm 配置自動格式化工具 Black Autopep8的使用

什麼是Autopep8?

在python開發中, 大家都知道,python編碼規範是PEP8,但是在市級開發中有的公司嚴格要求PEP8規範開發, 有的公司不會在乎那些,在我的了解中,程式員如果想走的更高,或者更遠,幹任何事情必須得專業化(本人了解方式), 不要求很多東西都是精通,但最少得有一門精通的語言,小弟在此在大佬面前裝逼了, 忘看過的大牛不要揭穿, 留下你懂的我不懂的知識,大家一起學習,一起進步。 謝謝。

Autopep8是一個将python代碼自動編排的一個工具,它使用pep8工具來決定代碼中的那部分需要被排版,Autopep8可以修複大部分pep8工具中報告的排版問題。很多人都知道 Ctrl+Alt+L 也可以排版, 但是我要告訴你,快捷鍵隻是可以簡單的排版。跟Autopep8是無法相比的。

安裝Autopep8:

pip install autopep8
           

安裝完成之後,import導入一下,測試是否安裝成功。

Aytopep8的使用

安裝完成之後,打開pycharm,建立一個新的python檔案, demo.py 将一下代碼放入檔案中。

def example1():
    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
        else:
            some_string = """
                       Indentation in multiline strings should not be touched.Only actual code should be reindented.
"""
           

這幾行代碼看上去是不是很亂, 接下來就要使用:Autopep8子產品了

打開cmd找到demo.py的檔案的上級目錄,

然後輸入以下指令:

autopep8 --in-place --aggressive --aggressive file.py
           

file.py 是你的demo.py

輸入指令,按回車執行成功是不傳回的, 執行完成之後就可以了,在次打開檔案就可以看到變化了。

import math
import sys


def example1():
    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
        else:
            some_string = """
                       Indentation in multiline strings should not be touched.Only actual code should be reindented.
"""
           

執行完Autopep8之後代碼是不是看上去簡潔多了。

有人會說,沒寫一個函數就執行一遍指令, 是不是有點麻煩啊, 是的, 有有點麻煩, 但是pycharm是可以配置的, 配置過程如下:

1: File ---> Settings ---> Tools ---> External Tools

打開之後,可以看見窗體左上角有一個 + 号, 點選+号添加。

Pycharm 配置自動格式化工具 Black Autopep8的使用
Name: 名稱可以随意

Program: autopep8        # 前提必須先安裝
Arguments: --in-place --aggressive --aggressive $FilePath$
Working directory: $ProjectFileDir$

Advanced Options
                ---- Outputfilters:
$FILE_PATH$\:$LINE$\:$COLUMN$\:.*
           

以上配置完成之後點選 OK 儲存即可。

快捷使用:

Pycharm 配置自動格式化工具 Black Autopep8的使用

Tools ---> External Tools ---> Autopep8 滑鼠點選一下即可。

繼續閱讀