python - 无法使用 yapf(谷歌的 python 格式化程序)设置/使用旋钮

标签 python formatter pep8 pep8-checker

我最近一直在尝试熟悉google/yapf 。但不知何故,我无法设置 .style.yapf 甚至 setup.cfg。我不确定将这些文件放在哪里。目前,我正在使用当前目录中的任何/或两个文件,但全局设置始终覆盖我的 .style.yapf/setup.cfg 文件。有人可以帮我吗,我使用他们的旋钮和 github/yapf 上给出的示例没有得到预期的结果

最佳答案

来自the docs :

YAPF will search for the formatting style in the following manner:

  1. Specified on the command line
  2. In the [style] section of a .style.yapf file in either the current directory or one of its parent directories.
  3. In the [yapf] section of a setup.cfg file in either the current directory or one of its parent directories.
  4. In the ~/.config/yapf/style file in your home directory.

If none of those files are found, the default style is used (PEP8).

如果我使用以下 Python 输入进行测试:

class Example: # This is an example
    def __init__(self):
        pass

运行yapf example.py不会产生任何变化:

class Example:  # This is an example
    def __init__(self):
        pass

但是如果我在当前目录中创建 .style.yapf 并包含以下内容:

[style]
SPACES_BEFORE_COMMENT=5
BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF=true

然后重新运行yapf example.py,我得到:

class Example:     # This is an example

    def __init__(self):
        pass

如果我希望这些更改全局应用,我会创建具有相同内容的 ~/.config/yapf/style

我还可以在当前目录(或父目录)中创建一个名为 setup.cfg 的文件,其中包含以下内容:

[yapf]
SPACES_BEFORE_COMMENT=5
BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF=true

关于python - 无法使用 yapf(谷歌的 python 格式化程序)设置/使用旋钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55574427/

相关文章:

python - Django ValueError 无法分配 "' 测试 '": "Profile.artist"必须是 "Artist"实例

python - Matplotlib 绘图不显示在 sublimetext 中

python - 我是否错误地使用了Python循环,或者我只是错过了什么?

cocoa NSNumberFormatterCurrencyStyle 没有 "$"返回零

java - 如何在启动时从用户收集系统时间信息

c# - AutoMapper - 添加自定义格式化程序

python - Python中的链式方法调用缩进样式

python - Python 中的亚马逊产品 API

python - 如何使用 virtualenv 在 Aptana Studio 3 上进行 PyDev 代码分析?

python - pep8 是否符合长 json 字典查找?