書式設定ツールBLACKの使用

インストール

pip install black

使用する

$ black {
    
    source_file_or_directory}

特別な使用法

#fmt:on / offを使用して、フォーマットを必要としないコードをマークします。fmt:on / offのインデントは同じである必要があります

デモ

プロジェクト全体をフォーマットできるデモ

# -*- coding: utf-8 -*-
# ---
# @Software: PyCharm
# @File: formatter.py
# @Author: yanggen
# @E-mail: [email protected]
# @Time: 01-18-2021
# ---
import os
import sys


def main(path):
    for (root, dirs, files) in os.walk(path, topdown=True):
        for file in files:
            if file[-3:] == ".py":
                whole_path = os.path.join(root, file)
                os.system("black {}".format(whole_path))


if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage : python formatter.py path_to_project_dir")
        exit(1)
    main(sys.argv[1])

おすすめ

転載: blog.csdn.net/weixin_44602409/article/details/112663622