[Python-Dev] pep 7

Neil Girdhar mistersheik at gmail.com
Sat Mar 21 01:54:39 CET 2015


If ever someone wants to clean up the repository to conform to PEP 7, I
wrote a program that catches a couple hundred PEP 7 violations in ./Python
alone (1400 in the whole codebase):

import os
import re

def grep(path, regex):
    reg_obj = re.compile(regex, re.M)
    res = []
    for root, dirs, fnames in os.walk(path):
        for fname in fnames:
            if fname.endswith('.c'):
                path = os.path.join(root, fname)
                with open(path) as f:
                    data = f.read()
                    for m in reg_obj.finditer(data):
                        line_number = sum(c == '\n'
                                          for c in data[:m.start()]) + 1
                        res.append("{}: {}".format(path, line_number))
    return res

for pattern in [
        r'^\s*\|\|',
        r'^\s*\&\&',
        r'} else {',
        r'\<return\s*\(',
]:
    print("Searching for", pattern)
    print("\n".join(grep('.', pattern)))

In my experience, it was hard to write PEP 7 conforming code when the
surrounding code is inconsistent.

Best,

Neil
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20150320/bf7cae78/attachment.html>


More information about the Python-Dev mailing list