All Pythonistas who do not follow the PEP8 coding conventions are equally feces

I think the only rule that Pythonista must follow is the PEP8 Coding Code. If you are a beginner, you should install a plugin to check PEP8 in your editor before you get a particularly weird habit. There is a clear correlation between code with many PEP8 violations and code with many bugs and problems. In this entry, we will introduce how to introduce a code or confirmation tool that conforms to the PEP8 standard, the PEP8 standard compliance ratio of famous libraries, and the PEP8 tool for each editor.

In Python, PEP20 --The Zen of Python (Japanese translation )) Has the following attitudes.

Easy to read is good. Being special is not a reason to break the rules. However, when seeking practicality, nature may be lost.

You don't have to force yourself to aim for zero PEP8 violations, but I would like you to write unified, easy-to-read code that conforms to PEP8 as much as possible. I tried to list the extent of violations in the violation status of the PyPi ranking upper library at the bottom. Thank you.

Discover PEP8 violations in your project

With flake8, you can easily check the PEP8 compliance status.

install


pip install flake8
flake8 {project_root} --filename=*.py --exclude=commands,migrations,[^_]*.py --max-line-length=79

Execution result


xxxxxx.py:20:1: E101 indentation contains mixed spaces and tabs
xxxxxx.py:20:1: W191 indentation contains tabs
xxxxxx.py:20:9: E223 tab before operator
xxxxxx.py:20:14: E261 at least two spaces before inline comment
xxxxxx.py:21:1: W191 indentation contains tabs
xxxxxx.py:21:8: E223 tab before operator
xxxxxx.py:21:13: E261 at least two spaces before inline comment
xxxxxx.py:22:1: W191 indentation contains tabs
xxxxxx.py:22:10: E223 tab before operator
xxxxxx.py:22:14: E261 at least two spaces before inline comment
xxxxxx.py:23:1: W191 indentation contains tabs
xxxxxx.py:23:9: E223 tab before operator



#Dreadful when encountering code that violates PEP8 line by line

PEP8 violation status of famous libraries in PyPi ranking

I investigated how appropriate a PEP8 violation is for the top 50 projects by download in PyPi Ranking. If the average PEP8 violation is 0.1 or less per line, it can be said that it is an excellent project. Experience shows that Python beginners who are unaware of PEP8 often write more than 0.3 lines per line.

スクリーンショット 2015-11-04 16.22.50.png

PEP8 violation status confirmation code


# -*- coding:utf-8 -*-
from __future__ import absolute_import, unicode_literals
import os
import commands

pypi_ranking = [
    'simplejson',
    'setuptools',
    'requests',
    'distribute',
    'virtualenv',
    'six',
    'pip',
    'certifi',
    'boto',
    'wincertstore',
    'pbr',
    'python-dateutil',
    'nose',
    'Jinja2',
    'lxml',
    'docutils',
    'MarkupSafe',
    'pyasn1',
    'pytz',
    'PyYAML',
    'pycrypto',
    'pika',
    'rsa',
    'coverage',
    'colorama',
    'Django',
    'psycopg2',
    'botocore',
    'cffi',
    'awscli',
    'paramiko',
    'jmespath',
    'pycparser',
    'SQLAlchemy',
    'ecdsa',
    'redis',
    'selenium',
    'bcdoc',
    'supervisor',
    'Werkzeug',
    'mock',
    'zc.buildout',
    'httplib2',
    'Paste',
    'Flask',
    'pep8',
    'pymongo',
    'carbon',
    'ssl',
    'meld3',

]


def get_py_file_line_count(_dir):
    _cmd_base = 'find {} -name \*.py|xargs wc -l'
    cmd = _cmd_base.format(_dir)
    result = commands.getoutput(cmd)
    return int(result.split('\n')[-1].replace(' total', ''))


def get_flake8_warning(_dir):
    cmd_base = 'flake8 {} --filename=*.py --exclude=commands,migrations,[^_]*.py ' \
               '--max-line-length=79|wc -l'
    cmd = cmd_base.format(_dir)
    return int(commands.getoutput(cmd))

# pip install
for app_name in pypi_ranking:
    os.system('pip install {}'.format(app_name))

#Investigation of PEP8 violation situation
for app_name in pypi_ranking:
    _path_base = '~/.virtualenvs/test1/lib/python2.7/site-packages/{}/'
    path = _path_base.format(app_name)

    #PEP8 violation at flake8
    try:
        flake8_warning = get_flake8_warning(path)

        #Count the total number of lines in the py file
        lines = get_py_file_line_count(path)
        per_line = float(flake8_warning) / float(lines)

        print 'APP:{} TotalLine:{} PEP8-warning:{} ' \
              'PEP8-warning-each-line:{}'.\
            format(app_name, lines, flake8_warning, per_line)
    except Exception:
        print "ERROR directory does not exist:{}".format(app_name)


Execution result


>>> python check.py
APP:simplejson TotalLine:4041 PEP8-warning:176 PEP8-warning-each-line:0.0435535758476
APP:setuptools TotalLine:9004 PEP8-warning:496 PEP8-warning-each-line:0.0550866281653
APP:requests TotalLine:17168 PEP8-warning:3218 PEP8-warning-each-line:0.187441752097
ERROR directory does not exist:distribute
APP:Jinja2 TotalLine:10806 PEP8-warning:93 PEP8-warning-each-line:0.00860632981677
APP:lxml TotalLine:6533 PEP8-warning:354 PEP8-warning-each-line:0.0541864380836
APP:docutils TotalLine:39915 PEP8-warning:6685 PEP8-warning-each-line:0.167480896906
APP:MarkupSafe TotalLine:816 PEP8-warning:8 PEP8-warning-each-line:0.00980392156863
....

Introducing PEP8 check plugin for each editor

--PyCharm: It is installed as standard. --vim: Automatically check & fix python coding style with vim --Emacs: Always check PEP8 while editing Python source code in Emacs --Ecrips: Compliant with # PEP8, which creates a Python development environment with Eclipse (luna) and Pydev

at the end

I'm scared of boomerang, so I checked the PEP8 violation status of the module I published. If you properly install the PEP8 check plugin and write the code consciously, I think that you will not violate PEP8 so much.

スクリーンショット 2015-11-04 16.48.43.png

reference

PEP 0008 -- Style Guide for Python Code PEP 20 -- The Zen of Python The Zen of Python PyPI Ranking

Recommended Posts

All Pythonistas who do not follow the PEP8 coding conventions are equally feces
Follow all users who are not following on Twitter