Check python code styles using pep8

2019.08.01 See below as it is old. ** The story of Python hated by cowboys ** https://qiita.com/mima_ita/items/cabcf014aa08e27c8de7

Overview

This document describes how to check for violations of PEP8, the coding style for Python code.

PEP 8 -- Style Guide for Python Code http://legacy.python.org/dev/peps/pep-0008/

Translation: https://dl.dropboxusercontent.com/u/555254/pep-0008.ja.html

Installation method

PEP8 --Install Python style guide checker https://pypi.python.org/pypi/pep8/

easy_install pep8

Or

pip install pep8

This allows you to run pep8 from the command line.

pep8 test.py
pep8 /test/directory

If you specify a directory, check the subdirectories as well

Main options

Option name Description
--version Show version
-h,--help Get help
-v,--verbose Display status messages such as the name of the file being checked.--Debug messages are displayed in vv
-q,--quiet Display only the file name.-qq does not display anything.
--first In case of the same error, display only at the beginning
--exclude=patterns Describe the pattern of file names and directory names to be excluded. You can have more than one by separating them with commas.
Default:.svn,CVS,.bzr,.hg,.git,pycache
--filename=patterns When searching a directory, only files with the pattern specified here are searched. You can specify multiple items separated by commas.
Default: *.py
--select=errors Specify errors and warnings
Example: E,W6
--ignore=errors Ignore the specified error
Example: E,W6
--show-source Show source for each error
--show-pep8 Add a description of PEP8 to each error.--You should use it with first
--statistics Aggregate the number of errors and warnings and display at the end
--count Finally, display the total number of errors and warnings
--config=path You can specify the location of the configuration file.

setting file

The value of each option can be specified in the configuration file that can be specified with the config option.

[pep8]
ignore = E111

Use from Python

It can also be used from Python by importing pep8.

import pep8
pep8style = pep8.StyleGuide(quiet=True)
ret = pep8style.check_files(['test.py']);
print ret.total_errors

Use from Jenkins

Aggregation is possible using Violations, a Jenkins plugin.

pep8_2.png

When executing from a shell script, it is necessary to write "#! / Bin / sh" at the beginning and process that does not return an error code at the end as follows.

#!/bin/sh
pep8 /share/py/test.py > ${WORKSPACE}/test.txt
echo "....finished"

See the page below for the reason why you need to do this. About running the Jenkins shell The settings for Violations are as follows.

pep8_1.png

There is no need to output in XML.

Recommended Posts

Check python code styles using pep8
Check the return value using PEP 380
Always check PEP8 while editing Python source code in Emacs
[VS Code] ~ Tips when using python ~
Execute Python code on C ++ (using Boost.Python)
Write test-driven FizzBuzz code using Python doctest.
Getting Python source code metrics using radon
Check stock prices with slackbot using python
python> coding guide> PEP 0008 --Style Guide for Python Code
Notes on using code formatter in Python
python setup.py test the code using multiprocess
Python nan check
python grammar check
Start using Python
python character code
Scraping using Python
Debug with VS Code using boost python numpy
I want to write in Python! (1) Code format check
Fibonacci sequence using Python
[Unity (C #), Python] Try running Python code in Unity using IronPython
Check SSL certificate renewal deadline using Python3 M2Crypto library
[Python] I immediately tried using Pylance's VS Code extension.
Python code acceleration approach
Data analysis using Python 0
Rewrite Python2 code to Python3 (2to3)
infomap python draw code
Before writing Python code
Data cleaning using Python
Check types_map when using mimetypes on AWS Lambda (Python)
About Python3 character code
Using Python #external packages
How to make a Python package using VS Code
WiringPi-SPI communication using Python
Age calculation using python
Domain check with Python
Search Twitter using Python
Name identification using python
Python Requests status code
Notes using Python subprocesses
Check Python # type identity
OpenCV basic code (python)
Try using Tweepy [Python2.7]
Check version with python
IP spoof using tor on macOS and check with python
Python notes using perl-ternary operator
Scraping using Python 3.5 async / await
Get country code with python
Python with VS Code (Windows 10)
Save images using python3 requests
[S3] CRUD with S3 using Python [Python]
[Python] Try using Tkinter's canvas
Check python coverage with pytest-cov
Using Quaternion with Python ~ numpy-quaternion ~
Python --Check type of values
Python notes using perl-special variables
[Python] Using OpenCV with Python (Basic)
Tool to check code style
Scraping using Python 3.5 Async syntax
Website change monitoring using python
Python code memo for yourself
Source code character code check script