Google's Python code formatting tool YAPF detailed

Source: Internet
Author: User

Usually used to write code in a haphazard manner, and the final code assigned, but relies on Pycharm's own formatting tools, as well as its own cue function to standardize the code. and Pycharm Format tool, do not support the multi-file Code batch format, have tried some solutions, the results of reactive and return, nothing. Seeing this tool today is finally promising.

# Local Small test
Cd/home/kylin/program
git clone https://github.com/google/yapf.git
Pythonpath=/home/kylin/program/yapf/python/home/kylin/program/yapf/yapf-h

......

The following is reproduced in the network to the original translation: 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

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

Set up:

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:

Of course, you can do the same thing on the command line through a dictionary, for example:

--style= ' {basedon Style: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:

Reformat into:

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:

You can also disable the format of a single literal, for example:

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.

Note: More details can be obtained on GitHub, click here to enter.

Google's Python code formatting tool YAPF detailed

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.