我使用以下方法安装了 yapf:
conda install yapf
Run Code Online (Sandbox Code Playgroud)
并在我的.vscode/settings.json文件中添加下一行:
{
//"python.linting.pylintEnabled": true,
//"python.linting.pycodestyleEnabled": false,
//"python.linting.flake8Enabled": true,
"python.formatting.provider": "yapf",
"python.formatting.yapfArgs": [
" — style",
"{based_on_style: pep8, indent_width: 4}"
],
"python.linting.enabled": true,
}
Run Code Online (Sandbox Code Playgroud)
但我不明白如何使用它 - 它在格式错误的脚本中没有显示任何错误:
import pandas as pd
class MyClass(object):
def __init__(self, some_value: int):
self.value = some_value
def one_more_function(self, another_value):
print(another_value)
myObject = MyClass(45)
myObject.one_more_function(2)
my__object2 = MyClass(324)
print('ok')
def some_foo():
"""
"""
pass
Run Code Online (Sandbox Code Playgroud) yapfformatter可以在列表解析中的表达式中的括号内的代码中包含长行:
threads = [threading.Thread(
target=worker, args=[q], daemon=True) for _ in range(worker_count)]
Run Code Online (Sandbox Code Playgroud)
我想在for-loop 之前换行:
threads = [threading.Thread(target=worker, args=[q], daemon=True)
for _ in range(worker_count)]
Run Code Online (Sandbox Code Playgroud)
yapf --help-style 没有任何显而易见的东西可以帮到这里.
我可以# yapf: disable在该行的末尾添加,以禁用yapf.有没有其他方法来强制不同的格式偏好?
当前yapf/style设置:
[style]
based_on_style = pep8
column_limit = 89
Run Code Online (Sandbox Code Playgroud)
版:
>>> import yapf
>>> yapf.__version__
'0.13.2' # the latest on github
Run Code Online (Sandbox Code Playgroud) 我正在使用一个名为 Snakemake 的 python-dsl,如下所示:
from bx.intervals.cluster import ClusterTree
from epipp.config import system_prefix, include_prefix, config, expression_matrix
config["name"] = "correlate_chip_regions_and_rna_seq"
bin_sizes = {"H3K4me3": 1000, "PolII": 200, "H3K27me3": 200}
rule all:
input:
expand("data/{bin_size}_{modification}.bed", zip,
bin_size=bin_sizes.values(), modification=bin_sizes.keys())
rule get_gene_expression:
input:
expression_matrix
output:
"data/expression/series.csv"
run:
expression_matrix = pd.read_table(input[0])
expression_series = expression_matrix.sum(1).sort_values(ascending=False)
expression_series.to_csv(output[0], sep=" ")
Run Code Online (Sandbox Code Playgroud)
我想对run:块内的东西运行 yapf 。
是否有可能让 yapf 忽略 python 中不存在的内容,例如rule关键字等,而只在文件的特定部分使用它?
我使用yapf自动格式化我的 python 代码。总的来说,我对此非常满意,但是有一个样式约定我不知道如何配置。当一对括号内有一长串参数,超出了最大column_limit(例如80)时,我希望它将它们分成单独的行,但如果可能的话保留左括号的缩进。例如:
def func(argument1, argument2, argument3, argument4, argument5, argument6, argument7):
pass
Run Code Online (Sandbox Code Playgroud)
应该成为
def func(argument1,
argument2,
argument3,
argument4,
argument5,
argument6,
argument7):
pass
Run Code Online (Sandbox Code Playgroud)
但我只能让它做:
def func(
argument1,
argument2,
argument3,
argument4,
argument5,
argument6,
argument7):
pass
Run Code Online (Sandbox Code Playgroud)
有人知道我想要的是否可能吗?如何?
如何强制 YAPF 始终像SPLIT_COMPLEX_COMPREHENSIONwhen那样拆分/折叠理解column_limit超出
我一直在尝试多种配置设置 - 均无济于事。我宁愿避免在代码库上喷洒yapf: disable大多数重要的理解......
我已经在项目下安装了Google yapf(还有另一个python格式化程序),并尝试就地格式化所有python文件,但是出现以下错误:
$ yapf -i -r files **.py
yapf: Input filenames did not match any python files
Run Code Online (Sandbox Code Playgroud)
为什么yapf无法理解模式?我应该怎么做才能达到同样的目的?
编辑我也按照建议尝试了yapf -ir,但是我得到了:
$ yapf -ir
usage: yapf [-h] [-v] [-d | -i] [-r | -l START-END] [-e PATTERN]
[--style STYLE] [--style-help] [--no-local-style] [-p] [-vv]
[files [files ...]]
yapf: error: cannot use --in-place or --diff flags when reading from stdin
Run Code Online (Sandbox Code Playgroud)
这很奇怪,因为我没有从标准输入中读取
我无法排除使用 yapf 自动格式化的目录。
测试用途:
yapf --in-place --recursive --parallel --exclude 'somedir' --exclude '*_pb2.py' .
yapf --in-place --recursive --parallel --exclude 'somedir/*' --exclude '*_pb2.py' .
yapf --in-place --recursive --parallel --exclude 'somedir/**' --exclude '*_pb2.py' .
Run Code Online (Sandbox Code Playgroud)
它似乎可以很好地采用文件模式,但不能很好地采用文件夹模式。