Point here
Most of the Python code formatting tools now (such as AUTOPEP8 and pep8ify) are lint errors that can be removed from the code. This obviously has some limitations. For example, code that follows the PEP 8 guidance may not be formatted, but that doesn't mean the code looks comfortable.
Lint is one of the most famous C language tools, and is a static code analysis developed by Bell Labs Stevejohnson in 1979 on the basis of PCC (Portablec Compiler), which is generally provided by UNIX systems. Compared with most C language compilers, lint can perform a more extensive error analysis of the program and is a more rigorous compilation tool. Initially, the lint tool was used to scan the C source file and warn of non-portable code in the source program. But now most lint utilities have become more rigorous, not only to check for portability but also to check out those features that are portable and completely grammatical but likely to be wrong.
But yapf a very inventive. It is derived from the Clang-format developed by Daniel Jasper. In general, the algorithm obtains the code and then re-orchestrates the initial code, even if the initial code does not violate the specification, it can be the best format to follow the code specification. This concept is similar to the GOFMT tool in the Go language, ending the various "jihad" about the format. If a project's code base, whenever modified, through YAPF optimization, code style can be unified, in every code review, there is no need to argue style.
The ultimate goal of YAPF is to generate the same code written by programmers who follow code specifications. can help you reduce the drudgery of maintaining code.
YAPF supports Python 2.7 and 3.4+.
Usage
1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
usage: yapf [-h] [--style STYLE] [-d | -i] [-l START-END | -r] ... Formatter for Python code. positional arguments: files optional arguments: -h, --help show this help message and exit --style STYLE specify formatting style: either a style name (for example "pep8" or "google"), or the name of a file with style settings. pep8 is the default. -d, --diff print the diff for the fixed source -i, --in-place make changes to files in place -l START-END, --lines START-END range of lines to reformat, one-based -r, --recursive run recursively over directories |
Example
Before beautification:
1 3 4 5 6 7 8 9 10 11 12 13 14 |
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 ] |
After beautification:
1 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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 ] |
详情请参考下方的 Github 链接。
Github:https://github.com/google/yapf
Yapf:google Open Source Python code formatting tool