Original address: http://geek.csdn.net/news/detail/29735
Introduced
Current formatters for Python, such as AUTOPEP8 and pep8ify, are used to remove lint errors in the code. This has obvious limitations.
YAPF uses a different approach, based on the "Clang-format" developed by Daniel Jasper. Essentially, the algorithm takes the code and re-typeset it to conform to the best format of the style guide, even though the original code does not violate the style guidelines. The idea is also a gofmt tool similar to the Go programming language.
The ultimate goal is to make the code produced by Yape as good as the code written by the programmer (provided the programmer follows the style guide), which replaces some of the drudgery of maintaining the code.
Installation
Project Address: HTTPS://GITHUB.COM/GOOGLE/YAPF
If you plan to use YAPF as a command-line tool instead of a library, then installation is not necessary. With the Python interpreter, YAPF can run as a directory. If you clone/unzip Yapf to Dir, you can run this:
$ pythonpath=dir Python dir/yapf [options] ...
Python version
YAPF supports Python2.7 and 3.4+.
Usage: (Please back up your code before use!!!)
Python yapf-i-R--style= ' {based_on_style:google, indent_width:4, column_limit:999} ' ${path}
Set up:
Http://img.my.csdn.net/uploads/201504/03/1428039886_1797.png
Formatting styles
Formatting styles that use YPF are configurable, and there are many "knobs" that can be used to adjust how the YAPF is formatted. (View style.py module)
To control the style, run the YAPF with--style. It accepts a predefined style (for example, PEP8 or Google)-a path to a configuration file (a dictionary that specifies the desired style or a key/value pair).
A configuration file is a simple list of key = value pairs (case sensitive), with a "style", for example:
Http://img.my.csdn.net/uploads/201504/03/1428040100_5148.png
Of course, you can do the same thing on the command line through a dictionary, for example:
--style= ' {basedonstyle:google, indent_width:4} '
This will use Google as the base style and be modified to have 4 spaces indented.
Case
With YAPF, you can use the following code:
Http://img.my.csdn.net/uploads/201504/03/1428040319_2295.png
Reformat into:
Http://img.my.csdn.net/uploads/201504/03/1428040355_2787.png
Problems
Why did YAPF ruin my great format?
For some code, it will not be better than the manual format, especially the large data text, which will get worse under YAPF. The reasons for this are diverse, but essentially, YAPF is just a simple tool to help develop, keeping things consistent with style guidelines, but this is not equal to readability.
To mitigate this situation, you need to indicate areas that you need to ignore when reformatting YAPF:
Http://img.my.csdn.net/uploads/201504/03/1428040424_3904.png
You can also disable the format of a single literal, for example:
Http://img.my.csdn.net/uploads/201504/03/1428040444_5056.png
Why not improve the existing tools?
The existing tools take into account different goals and require a large number of modification transformations to use the Clang-format algorithm. We want to use the Clang-format reorganization algorithm.
Can I use YAPF in my program?
The answer is YES! YAPF is designed to be used as a library and as a command-line tool. This means that when using YAPF, a tool or IDE plugin is free.
Google's Python code formatting tool YAPF detailed