The high-speed landing practice of Python Coding style guide

Source: Internet
Author: User
Tags visibility

The high-speed landing practice of Python Coding style guide

Machine and human strengths, such as coding style Check this can be self-active work should be handed over to the machine, so this article helps you in a few minutes to achieve the coding style of self-active inspection.

1. What are the famous Python Coding Style guide
    • PEP8

https://www.python.org/dev/peps/pep-0008/

Invented the Python language monument character Guido van Rossum's personally written coding Style, visibility 5 stars, operable 5 stars.

    • Google Python Coding Style Guide

Http://google-styleguide.googlecode.com/svn/trunk/pyguide.html

Python is widely used within Google as a development language, and the coding Style is widespread. Visibility 5 stars, operability 5 stars.

It is worth mentioning that Guido also worked at Google for some time.

2.flake8-coding style Check 自己主动化The weapon

You may have heard of PEP8. This is a PEP8 self-motivated tool that checks the Python code style according to the specification.

flake8is to pep8 carry out the packaging, give full play to the advantages of plug-in. Added checks such as code complexity, functions, variable naming conventions, import order, and so on.

2.1 Installing Flake8

Install Flake8. Install some useful plugins at the same time.

    • Pep8-nameing

Https://github.com/PyCQA/pep8-naming
Name Check

    • Flake8-import-order

Https://github.com/public/flake8-import-order
Import Order check. Can have two styles of order check cryptography, Google. This google means that the import order is (1) the standard library (2) third-party library (3) The local project library. Can be specified by a code check --import-order-style=google .

    • Flake8-todo

Https://github.com/schlamar/flake8-todo
Check the Todo in the code.

    • Flake8-quotes

https://github.com/zheller/flake8-quotes/
Check that the use of single and double cited is correct.

Detailed installation commands such as the following:

$ pip install flake8$ pip install pep8-naming$ pip install flake8-import-order$ pip install flake8-todo$ pip install flake8-quotes

Check which plugins are installed:

$ flake8 --version  # output, such as the following, shows the installed plugins:  2.5  .1  (PEP8: 1.5  .7 , Import-order: 0.6  .1 , naming: 0.3  .3 , Pyflakes: 1.0  .0 , McCabe: 0.3  .1 , Flake8-todo: 0.4 , Flake8_quotes: 0.1  .1 ) CPython 2.6  .6   on  linux    
2.2 Checking Python Codes with Flake8

For example, the following code:

# test.py fromOrderImportPlace_orderImportOS, sys class website_api(object):     def __init__(self):Self.user_name ="'Self. Gender =' Male '        #comment in wrong identSelf.active =False     def login(self, person):Self.user_name=person.user_name Not_used_var =0        return True     def Logout(self):Self.active =False     def place_order(self):Place_order () def action():Client_a = Website_api () client_a.login () Client_a.place_order () Client_a.logout ()

Run the check command:

----import-order-style=google test.py

The output is as follows, you can fix the code based on the error code, as in the N802 meaning that function name should not include uppercase English letters.

# Flake8 Outputtest.py:2:1: F401' sys 'Imported but unusedtest.py:2:1: I100 Imports Statements isinch  theWrong order. fromOS, SYS should bebefore  fromordertest.py:2:1: I201 Missing NewLinebeforeSectionsorimports.test.py:2:Ten: E401 Multiple Imports     on one linetest.py:4:7: N801 class names should use Capwords conventiontest.py:8:9: E265 block Comment should start with ' # 'test.py:9: A: E225 missing whitespace around operatortest.py: One:9: N803 argument name should be lowercasetest.py: -:9: F841Local variable ' Not_used_var 'is assigned toBut never usedtest.py: -:9: N802 function name should  be lowercase test.py: at:5: N806variable inch  function should  be lowercase 

Besides. Flake8 is also able to recursively check the code in a folder:

$ flake8 your_project_dir

Flake8 frequently used options are:

    • –show-source

Show source code for each error

    • –first

Show first occurrence of each error

    • –import-order-style=google

Import order style to follow

    • –count

Print total number of errors and warnings to standard error and set exit code to 1 if total is not null

    • Help

Get help

2.3 Flake8 Warning/error Codes List
Codes Notes Link
e***/w*** pep8 errors and warnings http://pep8.readthedocs.org/en/latest/intr O.html#error-codes
f*** pyflakes codes (see below) https://flake8.readthe docs.org/en/latest/warnings.html
c9** McCabe complexity, right now there are only C901 https: Github.com/pycqa/mccabe
n8** PEP-8 naming conventions https://github.co M/pycqa/pep8-naming#plugin-for-flake8
i*** checks the ordering of your imports https://github.com/public/flake8-import-order#warnings
t*** There is only T000 to check if the code includes TODO, Fixme Https://github.com/schlamar/flake8-todo
q*** There are Q000 on behalf of single and double quotes using error https://github.com/zheller/flake8-quotes/

With the integration of the new Flake8 plugin, there may be other codes, assuming that your project has special code-checking requirements. You can also develop your own plugin.

2.4 Flake8 's Personalized configuration

Depending on the need, the Flake8 configuration can be global (valid for all project). can also be part of project. Here are just examples of global configuration methods. See Flake8 Per project configuration for project configurations.

Edit~/.config/flake8

[flake8]ignore = E201,E202,E302exclude = .tox,*.eggmax-line-length = 120

The above configuration means that Flake8 does not check E201, E202, E302 These three errors, does not check the. tox,*.egg file. The longest single-line code that is agreed on is 120 characters long.

2.5 Flake8 Advanced Use Method-Git Commit Hook

Flake8 can be combined with Git commit to check the purpose of coding style, assuming Flake8 error, it is not possible commit .

Run the following command to install the git pre-commit hook under the root folder of Python project.

$ flake8 --install-hook$ true
References
    1. PEP8
      https://www.python.org/dev/peps/pep-0008/

    2. Google Python Coding Style
      Http://google-styleguide.googlecode.com/svn/trunk/pyguide.html

    3. PEP8 Tools
      Https://github.com/PyCQA/pep8

    4. Flake8
      https://flake8.readthedocs.org

    5. Warning/error Codes of Flake8
      Https://flake8.readthedocs.org/en/latest/warnings.html

The high-speed landing practice of Python Coding style guide

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.