# YAPF
<p align="center">
<a href="https://badge.fury.io/py/yapf"><img alt="PyPI Version" src="https://badge.fury.io/py/yapf.svg"></a>
<a href="https://github.com/google/yapf/actions/workflows/ci.yml"><img alt="Build Status" src="https://github.com/google/yapf/actions/workflows/ci.yml/badge.svg"></a>
<a href="https://github.com/google/yapf/actions/workflows/pre-commit.yml"><img alt="Actions Status" src="https://github.com/google/yapf/actions/workflows/pre-commit.yml/badge.svg"></a>
<a href="https://coveralls.io/github/google/yapf?branch=main"><img alt="Coverage Status" src="https://coveralls.io/repos/github/google/yapf/badge.svg?branch=main"></a>
</p>
## Introduction
YAPF is a Python formatter based off of [`clang-format`](https://clang.llvm.org/docs/ClangFormat.html)
(developed by Daniel Jasper). In essence, the algorithm takes the code and
calculates the best formatting that conforms to the configured style. It takes
away a lot of the drudgery of maintaining your code.
The ultimate goal is that the code YAPF produces is as good as the code that a
programmer would write if they were following the style guide.
> **Note**
> YAPF is not an official Google product (experimental or otherwise), it is
> just code that happens to be owned by Google.
## Installation
To install YAPF from PyPI:
```bash
$ pip install yapf
```
YAPF is still considered in "beta" stage, and the released version may change
often; therefore, the best way to keep up-to-date with the latest development
is to clone this repository or install directly from github:
```bash
$ pip install git+https://github.com/google/yapf.git
```
Note that if you intend to use YAPF as a command-line tool rather than as a
library, installation is not necessary. YAPF supports being run as a directory
by the Python interpreter. If you cloned/unzipped YAPF into `DIR`, it's
possible to run:
```bash
$ PYTHONPATH=DIR python DIR/yapf [options] ...
```
## Using YAPF within your favorite editor
YAPF is supported by multiple editors via community extensions or plugins. See [Editor Support](EDITOR%20SUPPORT.md) for more info.
## Required Python versions
YAPF supports Python 3.7+.
## Usage
```console
usage: yapf [-h] [-v] [-d | -i | -q] [-r | -l START-END] [-e PATTERN]
[--style STYLE] [--style-help] [--no-local-style] [-p] [-m] [-vv]
[files ...]
Formatter for Python code.
positional arguments:
files reads from stdin when no files are specified.
optional arguments:
-h, --help show this help message and exit
-v, --version show program's version number and exit
-d, --diff print the diff for the fixed source
-i, --in-place make changes to files in place
-q, --quiet output nothing and set return value
-r, --recursive run recursively over directories
-l START-END, --lines START-END
range of lines to reformat, one-based
-e PATTERN, --exclude PATTERN
patterns for files to exclude from formatting
--style STYLE specify formatting style: either a style name (for
example "pep8" or "google"), or the name of a file
with style settings. The default is pep8 unless a
.style.yapf or setup.cfg or pyproject.toml file
located in the same directory as the source or one of
its parent directories (for stdin, the current
directory is used).
--style-help show style settings and exit; this output can be saved
to .style.yapf to make your settings permanent
--no-local-style don't search for local style definition
-p, --parallel run YAPF in parallel when formatting multiple files.
-m, --print-modified print out file names of modified files
-vv, --verbose print out file names while processing
```
### Return Codes
Normally YAPF returns zero on successful program termination and non-zero
otherwise.
If `--diff` is supplied, YAPF returns zero when no changes were necessary,
non-zero otherwise (including program error). You can use this in a CI workflow
to test that code has been YAPF-formatted.
### Excluding files from formatting (.yapfignore or pyproject.toml)
In addition to exclude patterns provided on commandline, YAPF looks for
additional patterns specified in a file named `.yapfignore` or `pyproject.toml`
located in the working directory from which YAPF is invoked.
`.yapfignore`'s syntax is similar to UNIX's filename pattern matching:
```
* matches everything
? matches any single character
[seq] matches any character in seq
[!seq] matches any character not in seq
```
Note that no entry should begin with `./`.
If you use `pyproject.toml`, exclude patterns are specified by `ignore_patterns` key
in `[tool.yapfignore]` section. For example:
```ini
[tool.yapfignore]
ignore_patterns = [
"temp/**/*.py",
"temp2/*.py"
]
```
Formatting style
================
The formatting style used by YAPF is configurable and there are many "knobs"
that can be used to tune how YAPF does formatting. See the `style.py` module
for the full list.
To control the style, run YAPF with the `--style` argument. It accepts one of
the predefined styles (e.g., `pep8` or `google`), a path to a configuration
file that specifies the desired style, or a dictionary of key/value pairs.
The config file is a simple listing of (case-insensitive) `key = value` pairs
with a `[style]` heading. For example:
```ini
[style]
based_on_style = pep8
spaces_before_comment = 4
split_before_logical_operator = true
```
The `based_on_style` setting determines which of the predefined styles this
custom style is based on (think of it like subclassing). Four
styles are predefined:
- `pep8` (default)
- `google` (based off of the [Google Python Style Guide](https://github.com/google/styleguide/blob/gh-pages/pyguide.md))
- `yapf` (for use with Google open source projects)
- `facebook`
See `_STYLE_NAME_TO_FACTORY` in [`style.py`](https://github.com/google/yapf/blob/main/yapf/yapflib/style.py) for details.
It's also possible to do the same on the command line with a dictionary. For
example:
```bash
--style='{based_on_style: pep8, indent_width: 2}'
```
This will take the `pep8` base style and modify it to have two space
indentations.
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 `[tool.yapf]` section of a `pyproject.toml` file in either the current
directory or one of its parent directories.
5. In the `[style]` section of a `~/.config/yapf/style` file in your home
directory.
If none of those files are found, the default style PEP8 is used.
Example
=======
An example of the type of formatting that YAPF can do, it will take this ugly
code:
```python
x = { 'a':37,'b':42,
'c':927}
y = 'hello ''world'
z = 'hello '+'world'
a = 'hello {}'.format('world')
class foo ( object ):
def f (self ):
return 37*-+2
def g(self, x,y=42):
return y
def f ( a ) :
return 37+-+a[42-x : y**3]
```
and reformat it into:
```python
x = {'a': 37, 'b': 42, 'c': 927}
y = 'hello ' 'world'
z = 'hello ' + 'world'
a = 'hello {}'.format('world')
class foo(object):
def f(self):
return 37 * -+2
def g(self, x, y=42):
return y
def f(a):
return 37 + -+a[42 - x:y**3]
```
## Example as a module
The two main APIs for calling YAPF are `FormatCode` and `FormatFile`, these
share several arguments which are described below:
```python
>>> from yapf.yapflib.yapf_api import FormatCode # reformat a string of code
>>> formatted_code, changed
没有合适的资源?快使用搜索试试~ 我知道了~
A formatter for Python files.zip
共112个文件
py:73个
md:6个
yml:4个
需积分: 1 0 下载量 123 浏览量
2024-03-01
23:20:44
上传
评论
收藏 286KB ZIP 举报
温馨提示
A formatter for Python files.zip
资源推荐
资源详情
资源评论
收起资源包目录
A formatter for Python files.zip (112个子文件)
AUTHORS 307B
.isort.cfg 163B
CONTRIBUTORS 685B
.coveragerc 32B
.editorconfig 265B
.flake8 408B
.gitattributes 23B
.gitignore 1KB
MANIFEST.in 259B
tox.ini 356B
settings.default.json 974B
extensions.json 451B
LICENSE 12KB
LICENSE 12KB
LICENSE 11KB
CHANGELOG.md 38KB
README.md 32KB
EDITOR SUPPORT.md 3KB
README.md 3KB
CONTRIBUTING.md 2KB
HACKING.md 2KB
reformatter_basic_test.py 114KB
reformatter_buganizer_test.py 90KB
yapf_test.py 62KB
format_decision_state.py 48KB
style.py 32KB
reformatter_pep8_test.py 31KB
split_penalty_visitor.py 28KB
reformatter.py 28KB
pytree.py 25KB
logical_line.py 25KB
split_penalty.py 24KB
subtype_assigner.py 19KB
tokenize.py 19KB
file_resources_test.py 19KB
reformatter_python3_test.py 18KB
subtype_assigner_test.py 17KB
pytree_unwrapper.py 16KB
comment_splicer.py 15KB
reformatter_facebook_test.py 15KB
fixer_util.py 14KB
parse.py 13KB
__init__.py 13KB
pgen.py 12KB
yapf_api.py 12KB
style_test.py 11KB
format_token.py 11KB
comment_splicer_test.py 11KB
pytree_utils.py 10KB
pytree_unwrapper_test.py 9KB
file_resources.py 9KB
blank_line_calculator_test.py 9KB
driver.py 9KB
conv.py 8KB
pytree_utils_test.py 8KB
object_state.py 8KB
split_penalty_test.py 7KB
reformatter_style_config_test.py 7KB
patcomp.py 6KB
blank_line_calculator.py 6KB
fixer_base.py 6KB
grammar.py 5KB
pyparser.py 5KB
yapf_diff.py 5KB
pytree_visitor.py 4KB
format_decision_state_test.py 4KB
pytree_visitor_test.py 4KB
main_test.py 4KB
line_joiner.py 4KB
logical_line_test.py 3KB
format_token_test.py 3KB
pyparser_utils.py 3KB
yapf_test_helper.py 3KB
line_joiner_test.py 3KB
utils.py 3KB
identify_container.py 2KB
continuation_splicer.py 2KB
errors.py 1KB
literals.py 1KB
token.py 1KB
split_penalty.py 1KB
pygram.py 1KB
subtypes.py 1KB
__main__.py 680B
__init__.py 596B
__init__.py 596B
__init__.py 596B
__init__.py 596B
__init__.py 142B
__init__.py 73B
_version.py 23B
__init__.py 0B
__init__.py 0B
__init__.py 0B
pylintrc 13KB
.python-version 35B
README.rst 383B
pre-commit.sh 3KB
pyparser_visitor.py.tmpl 18KB
pyproject.toml 2KB
共 112 条
- 1
- 2
资源评论
日刷百题
- 粉丝: 5309
- 资源: 951
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功