当前位置: 首页 > 工具软件 > YAPF > 使用案例 >

VsCode 格式化工具:yapf

萧宣
2023-12-01

目录

yapf简介

安装

使用方法

更改行最大字数

另一种方法

参考网址

 

yapf简介

yapf是一款由Google开源的Python代码自动格式化工具,它根据PEP 8规范可以帮我们自动格式化我们的代码,让代码更规范、更漂亮。

本文介绍VSCODE下面安装配置yapf

安装

pip install yapf

使用方法

yapf -i filename.py //这样可以成功格式化

启用

  1. File -> Preferences -> Setting
  2. 搜索框输入:formatting.provider
  3. 下拉菜单选择 yapf

更改行最大字数

其中最大列宽被限制为80,如果超过80,在格式化时就会被yapf换行,随着现在人们的显示器越来越大,分辨率也越来越高,我们可以将这个限制适当修改。

找到对应项目里面的文件

../site-packages/yapf/yapflib/style.py

找到下面的函数

def CreatePEP8Style():
    return dict(
        ALIGN_CLOSING_BRACKET_WITH_VISUAL_INDENT=True,
        ALLOW_MULTILINE_LAMBDAS=False,
        ALLOW_MULTILINE_DICTIONARY_KEYS=False,
        ALLOW_SPLIT_BEFORE_DEFAULT_OR_NAMED_ASSIGNS=True,
        ALLOW_SPLIT_BEFORE_DICT_VALUE=True,
        ARITHMETIC_PRECEDENCE_INDICATION=False,
        BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF=False,
        BLANK_LINE_BEFORE_CLASS_DOCSTRING=False,
        BLANK_LINE_BEFORE_MODULE_DOCSTRING=False,
        BLANK_LINES_AROUND_TOP_LEVEL_DEFINITION=2,
        COALESCE_BRACKETS=False,
        COLUMN_LIMIT=79,
        CONTINUATION_ALIGN_STYLE='SPACE',
        CONTINUATION_INDENT_WIDTH=4,
        DEDENT_CLOSING_BRACKETS=False,
        INDENT_CLOSING_BRACKETS=False,
        DISABLE_ENDING_COMMA_HEURISTIC=False,
        EACH_DICT_ENTRY_ON_SEPARATE_LINE=True,
        I18N_COMMENT='',
        I18N_FUNCTION_CALL='',
        INDENT_DICTIONARY_VALUE=False,
        INDENT_WIDTH=4,
        INDENT_BLANK_LINES=False,
        JOIN_MULTIPLE_LINES=True,
        NO_SPACES_AROUND_SELECTED_BINARY_OPERATORS=set(),
        SPACE_BETWEEN_ENDING_COMMA_AND_CLOSING_BRACKET=True,
        SPACES_AROUND_POWER_OPERATOR=False,
        SPACES_AROUND_DEFAULT_OR_NAMED_ASSIGN=False,
        SPACES_BEFORE_COMMENT=2,
        SPLIT_ARGUMENTS_WHEN_COMMA_TERMINATED=False,
        SPLIT_ALL_COMMA_SEPARATED_VALUES=False,
        SPLIT_ALL_TOP_LEVEL_COMMA_SEPARATED_VALUES=False,
        SPLIT_BEFORE_ARITHMETIC_OPERATOR=False,
        SPLIT_BEFORE_BITWISE_OPERATOR=True,
        SPLIT_BEFORE_CLOSING_BRACKET=True,
        SPLIT_BEFORE_DICT_SET_GENERATOR=True,
        SPLIT_BEFORE_DOT=False,
        SPLIT_BEFORE_EXPRESSION_AFTER_OPENING_PAREN=False,
        SPLIT_BEFORE_FIRST_ARGUMENT=False,
        SPLIT_BEFORE_LOGICAL_OPERATOR=True,
        SPLIT_BEFORE_NAMED_ASSIGNS=True,
        SPLIT_COMPLEX_COMPREHENSION=False,
        SPLIT_PENALTY_AFTER_OPENING_BRACKET=300,
        SPLIT_PENALTY_AFTER_UNARY_OPERATOR=10000,
        SPLIT_PENALTY_ARITHMETIC_OPERATOR=300,
        SPLIT_PENALTY_BEFORE_IF_EXPR=0,
        SPLIT_PENALTY_BITWISE_OPERATOR=300,
        SPLIT_PENALTY_COMPREHENSION=80,
        SPLIT_PENALTY_EXCESS_CHARACTER=7000,
        SPLIT_PENALTY_FOR_ADDED_LINE_SPLIT=30,
        SPLIT_PENALTY_IMPORT_NAMES=0,
        SPLIT_PENALTY_LOGICAL_OPERATOR=300,
        USE_TABS=False,
    )

COLUMN_LIMIT=79,
修改为
COLUMN_LIMIT=120,
这样你的列宽就修改为120了。

另一种方法

vscode的settings.json做如下修改,输入想要的字符数:

{
    "python.formatting.provider": "yapf",
    "python.formatting.yapfArgs": ["--style", "{column_limit: 79}"],
}

 

参考网址

  1. https://www.jianshu.com/p/acdc1fbb3648
  2. https://blog.csdn.net/founderznd/article/details/104837973
  3. https://zhuanlan.zhihu.com/p/109360933
  4. https://github.com/google/yapf
  5. https://blog.csdn.net/Evaristexu/article/details/109895379
  6. https://blog.csdn.net/robertsong2004/article/details/50388830/

 

 类似资料: