How to use Pylint to standardize Python code styles

Source: Internet
Author: User
Tags naming convention

What is Https://www.ibm.com/developerworks/cn/linux/l-cn-pylint/Pylint?

Pylint is a Python Code analysis tool that parses errors in Python code to find code style standards that are not compliant (the code style used by Pylint is PEP 8, specific information, see Resources), and potentially problematic code. Currently the latest version of Pylint is pylint-0.18.1.

    • Pylint is a Python tool that provides additional functionality in addition to the usual Code analysis tools: Checking the length of a line of code, whether a variable name conforms to a naming standard, whether a declared interface is actually implemented, and so on.
    • One of the great benefits of Pylint is its high configuration, high customization, and the ability to write small plugins to add functionality.
    • If you run the Pylint two times, it will show both the current and last run results, so you can see if the code quality has improved.
    • The Pylint is now integrated into the eclipse's Pydev plugin.

Back to top of page

Pylint Specific introduction to Pylint installation

The pylint can be used for all Python versions that are above or equal to 2.2 compatible. Packages that require logilab-astng (version >= 0.14) and Logilab-common (version >= 0.13) (see Resources for details), if the Python version is less than 2.3, it also needs Opti K-Pack (this is not considered for the next example in this article).

All of the packages used by Pylint

Logilab-astng's latest package download: http://www.logilab.org/856/

Logilab-common's latest package download: http://www.logilab.org/848/

Optik's Package Download: http://optik.sourceforge.net/

Pylint's latest package download: Http://www.logilab.org/project/pylint

Pylint installation on Linux

1. On Linux, first install the Python package (above version 2.2) and add the path to the Python executable file in the environment variable $PATH.

2. Download the packages for Pylint, logilab-astng (version >= 0.14) and Logilab-common (version >= 0.13), and use the extract to unzip tar zxvf *.tar.gz the packages.

3. Go to Logilab-astng, Logilab-common, and Pylint in the Unpacked folder and run the command Python setup.py install to install.

4. Once the installation is complete, you can pylint [options] module_or_package call Pylint.

Pylint installation on Windows

1. Install the Python package (above version 2.2), right-click on the My Computer icon on the desktop, select Properties, Advanced, environment variables, add the Python installation path in the $PATH, such as C:\Python26\.

2. Unzip all packages using the Unzip tool.

3. Open the command-line window and use the cd logilab-astng, Logilab-common, and Pylint to unpack the folder and run the command python setup.py install to install it.

4. After the installation is complete, a Scripts folder appears under the Python installation path containing some bat scripts, such as Pylint.bat.

5. In order to make the call Pylint.bat do not need to enter the full path, in the Python installation directory to create the Pylint.bat redirect file, which is a plain text file Pylint.bat, which contains pylint.bat the actual path, such as: C # Python26\scripts\pylint.bat.

6. After the installation is complete, you can pylint [options] module_or_package call Pylint.

Pylint Call Listing 1. Pylint Call Command
pylint [Options] Module_or_package

Code check for a module module.py using Pylint:

    • 1. Go to the folder where the module is located, runpylint [options] module.py
      This invocation is always working because the current working directory is automatically added to the Python path.
    • 2. Do not enter the folder where the module is located, runpylint [options] directory/module.py
      This invocation can work when the following conditions are met: Directory is a python package (for example, contains a __init__.py file), or directory is added to the Python path.

Code check for a package pakage using Pylint:

    • 1. Go to the folder where the package is located, runpylint [options] pakage。
      This invocation is always working because the current working directory is automatically added to the Python path.
    • 2. Do not enter the folder where the package is located, runpylint [options] directory/ pakage。
      In this case, it works when the following conditions are met: Directory is added to the Python path. For example, on Linux,export PYTHONPATH=$PYTHONPATH: directory。

In addition, for a machine with the Tkinter package installed, you can use the command to pylint-gui open a simple GUI interface, enter the name of the module or package here (the rule with the command line), click Run, the output of Pylint will be displayed in the GUI.

Common command-line arguments for Pylint
  • -h,--help

    Displays all help information.

  • --generate-rcfile

    You can use Pylint--generate-rcfile to generate a sample configuration file. You can use redirection to save this configuration file for later use. You can also precede the other options so that the values of these options are included in the resulting configuration file. For example pylint --persistent=n --generate-rcfile > pylint.conf , to view pylint.conf, you can see Persistent=no instead of its default value of Yes.

  • --rcfile=<file>

    Specifies a configuration file. Put the configuration in the configuration file, which not only regulates your own code, but also makes it easy to share these specifications with others.

  • -i <y_or_n>, --include-ids=<y_or_n>

    Include the ID of the message in the output, and then pylint --help-msg=<msg-id> view the details of the error by looking at it, so that you can locate the error specifically.

  • -r <y_or_n>, --reports=<y_or_n>

    The default is Y, which means that the output of Pylint contains the source Code Analysis section and the report section.

  • --files-output=<y_or_n>

    Output the message of each module/package to a pylint_module/package. [Txt|html] In the named file, if there is a report, output to the name Pylint_global. [txt|html] in the file. The default is output to the screen does not output to the file.

  • -f <format>, --output-format=<format>

    Sets the output format. The formats you can choose are text, parseable, colorized, MSVs (Visual Studio) and HTML, and the default output format is text.

  • --disable-msg=<msg ids>

    Suppresses the message of the specified ID. For example, the output contains the message W0402 this warning, if you do not want it to appear in the output, you can use--disable-msg= W0402

Output of the Pylint

Pylint 的默认输出格式是原始文 (raw text) format ,可以通过 -f <format>,--output-format=<format> 来指定别的输出格式如 HTML 等等。在 pylint的输出中有如下两个部分:源代码分析部分和报告部分。

Source Code Analysis Section:

For each Python module, the result of Pylint first displays some "*" characters, followed by the name of the module, followed by a series of message, the format of the message is as follows:

Message_type:line_num:[object:] MESSAGE

There are several message_type:

(C) practice. Violates coding style standards

(R) refactoring. Code that was badly written.

(W) warning. Some Python-specific issues.

(E) error. Most likely an error in the code.

(F) Fatal error. Errors that prevent Pylint from running further.

Listing 2. Output of the Utils module in the Pylint
Module utils  c:88:message:missing docstring  R:88:message:too Few public methods (0/2)  c:183 : messageshandlermixin._cat_ids:missing docstring  R:183:messageshandlermixin._cat_ids:method could be a function  R:282:messageshandlermixin.list_messages:too Many branches (14/12)

Report section:

After the end of the source code analysis, there will be a series of reports, each focusing on some aspects of the project, such as the number of message per category, the dependency of the module, and so on. Specifically, the report contains the following aspects:

    • The number of module to check.
    • For each module, the percentage of errors and warnings in them. For example there are two module A and B, if a total of 4 errors are checked out, 1 errors are in a, 3 errors are in B, then A's error percentage is 25%, B's error percentage is 75%.
    • The total number of errors, warnings.

Back to top of page

Specific examples of using Pylint to parse Python code

The following is a Python code dw.py that reads some values from an XML file and displays the code as follows:

Listing 3. Source
Import string  #!/usr/bin/env python  import xml.dom.minidom  xmldom=xml.dom.minidom.parse ("Identity.xml ")  organizations = Xmldom.getelementsbytagname (' DW ') for  org in organizations: Products  = Org.getelementsbytagname (' Linux ') for product in products     :         print ' ID: ' + product.getattribute (' id ')         print ' name: ' + product.getattribute (' name ')         print ' Word count: ' + product.getattribute (' count ')
Listing 4. Identity.xml's Content
<IBM>         <DW>                 <linux id= "name=" python "count=" "/>         </DW>  </ibm >

The result of using Pylint at this time (which is copied from the output in HTML format) is:

Listing 5. Analysis Results of Pylint
Module DW  c:1:missing docstring  c:5:operator not preceded by a space xmldom=xml.dom.minidom.parse ("Identity.xml") ^  c:5:invalid name "XmlDom" (Should match (([a-z_][a-z0-9_]*) | ( __.*__))  c:6:invalid name "Organizations" (Should match (([a-z_][a-z0-9_]*) | ( __.*__)) $) Report  part omitted

The first part of the output is source code analysis, and the second part is the report. There is so much information in the output, where do you start the analysis? First use the following steps to analyze the code:

1. Because the output is too long, you can not let it output the report section first, based on the source Code Analysis section to find the problem in the code. Use options"--reports=n"

2. Use the options "--include-ids=y" . You can get the ID of each piece of information into the Source Code Analysis section.

Listing 6. Results of using Pylint--reports=n--include-ids=y dw.py
Module DW c0111:1: Missing docstring c0322:5: Operator not preceded by a space xmldom=xml.dom.minidom.pars E ("Identity.xml") ^ c0103:5: Invalid Name "XmlDom" (Should match (([a-z_][a-z0-9_]*) | ( __.*__)) C0103:6: Invalid name "Organizations" (Should match (([a-z_][a-z0-9_]*) | ( __.*__))$)

Each message is preceded by an ID, which can be viewed if you do not understand the meaning of the message pylint --help-msg=id .

Listing 7. Results of using Pylint--help-msg= C0111
C0111: *missing docstring*   used when a module, function, class or method have no docstring. Some Special  methods like __init__ doesn ' t necessary require a docstring.  This message belongs to the basic checker.

3. Start analyzing the issues in each source code. As you know from above, the reason for the first problem is missing docstring , adding in the code docstring , the modified code is as follows:

Listing 8. Add docstring Modified source code
#!/usr/bin/env python "" "This script parse the content of a XML file" "" Import xml.dom.minidom  xmldom=xml.dom.minidom. Parse ("Identity.xml")  organizations = Xmldom.getelementsbytagname (' DW ') for  org in organizations:     Products = Org.getelementsbytagname (' Linux ') for product in products     :         print ' ID: ' + product.getattribute (' id ')         print ' name: ' + product.getattribute (' name ')         print ' Word count: ' + product.getattribute (' count ')

Rerun pylint --reports=n --include-ids=y dw.py with the result:

Listing 9. Run results
Module DW  C0322:  7:operator not preceded by a space  xmldom=xml.dom.minidom.parse (" Identity.xml ")       ^  C0103:  7:invalid name" XmlDom "(Should match (([a-z_][a-z0-9_]*) | ( __.*__))  C0103:  8:invalid Name "Organizations" (Should match (([a-z_][a-z0-9_]*) | ( __.*__))$)

You can see that the first problem in the source code has been resolved.

4. With regard to the second C0322, the analysis here shows that it is clear that there are no spaces on either side of the Equals operator in line seventh of the code. We add a space here, rerun pylint --reports=n --include-ids=y dw.py , the result is:

Listing 10. Run results
Module DW  C0103:  7:invalid name "XmlDom" (Should match (([a-z_][a-z0-9_]*) | ( __.*__))  C0103:  8:invalid Name "Organizations" (Should match (([a-z_][a-z0-9_]*) | ( __.*__))$)

5. You can see that now the problem is only C0103. This means that the variable naming rules should conform to the provisions of the following regular expressions. Pylint defines a series of naming conventions for names of variables, functions, classes, etc. In practice we do not necessarily need to use such a naming convention, we can define our own naming rules using regular expressions, such as the use of options --const-rgx=‘[a-z_][a-z0-9_]{2,30}$‘ , we will xmlDom change xmldom the variable, the code is as follows:

Listing 11. Change the variable xmlDom to the source code after XmlDom
#!/usr/bin/env python "" "This script parse the content of a XML file" "" Import xml.dom.minidom  xmldom = Xml.dom.minido M.parse ("Identity.xml")  organizations = Xmldom.getelementsbytagname (' DW ') for  org in organizations:     Products = Org.getelementsbytagname (' Linux ') for product in products     :         print ' ID: ' + product.getattribute (' id ')         print ' name: ' + product.getattribute (' name ')         print ' Word count: ' + product.getattribute (' count ')

Run pylint --reports=n --include-ids=y --const-rgx=‘[a-z_][a-z0-9_]{2,30}$‘ dw.py , there is no problem in the results.

6. If you want a group of people to use these uniform rules, to standardize the code style of a department. For example, if you use it --const-rgx=‘[a-z_][a-z0-9_]{2,30}$‘ as a naming convention, a convenient way is to use a configuration file.

Use pylint --generate-rcfile > pylint.conf to generate a sample configuration file, and then edit the --const-rgx options in it. Or you can directly pylint --const-rgx=‘[a-z_][a-z0-9_]{2,30}$‘ --generate-rcfile > pylint.conf , so the options in the generated configuration file --const-rgx are straightforward ‘[a-z_][a-z0-9_]{2,30}$‘ .

Specify the configuration file when running Pylint later:pylint --rcfile=pylint.conf dw.py

The pylint will then pylint.conf specify the parameters according to the options in the configuration file. In one department, you can use the same configuration file together, so that you can maintain a consistent code style.

7. If the report part is added, it is not used --reports= n,可以看到报告部分的内容 .

Back to top of page

Conclusion

In this paper, the Python Code analysis tool Pylint is fully introduced through detailed theory introduction and simple and understandable examples. I believe that after reading the reader will be able to easily apply pylint to their own development projects.

Resources
    • Pylint official website.
    • Logilab-astng's latest package download.
    • Logilab-common's latest package download.
    • Download the Optik package.
    • Pylint's latest package download.
    • View Python code style standard PEP 8-style guide for Python code download.
    • For more information on Python, refer to the Python topic on developerWorks.

How to use Pylint to standardize Python code styles

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.