代码规范工具 (formatting tool) BLACK 的使用

安装

pip install black

使用

$ black {source_file_or_directory}

特殊用法

使用 # fmt:on/off 来标记不需要format的代码,fmt : on/off 的缩进必须相同

demo

一个可以format整个项目的demo

# -*- coding: utf-8 -*-
# ---
# @Software: PyCharm
# @File: formatter.py
# @Author: yanggen
# @E-mail: zhull991122@sjtu.edu.cn
# @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])

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值