autopep8 testen/einrichten

Code-Stücke können hier veröffentlicht werden.
Antworten
Benutzeravatar
jens
Moderator
Beiträge: 8483
Registriert: Dienstag 10. August 2004, 09:40
Wohnort: duisburg
Kontaktdaten:

Mittwoch 20. August 2014, 11:52

Hier was gehacktes damit man rausfinden kann, welche "Fixes" man von autopep8 nicht möchte:

Code: Alles auswählen

import autopep8


def list_fixes(only_fixes=None):
    if only_fixes is not None:
        only_fixes = [fix.strip() for fix in only_fixes.split(",")]
        print "List fixes %s:\n" % ", ".join(only_fixes)
    else:
        print "List of all existing fixes:\n"

    for code, description in sorted(autopep8.supported_fixes()):
        if only_fixes is not None and code not in only_fixes:
            continue
        print('"{code}", # {description}'.format(
            code=code, description=description))


TEST_CODE='''
import os,sys

import onw_module1,onw_module2

class Foo(object):
    """
    a doc string
    """
    def menu_event_about(self):
#         deactivated_line=True
        tkMessageBox.showinfo("DragonPy", # A comment
            "DragonPy the OpenSource emulator written in python.\\n"
            "more info: https://github.com/jedie/DragonPy"
        )

        log.error("%04x| write $%02x to $%04x -> PIA 0 A side Data reg.\t|%s",
            op_address, value, address, self.cfg.mem_info.get_shortest(op_address)
        )

if __name__ == '__main__':
    setup_logging(log,
#         level=1 # hardcore debug ;)
#         level=10 # DEBUG
#         level=20 # INFO
#         level=30 # WARNING
#         level=40 # ERROR
        level=50  # CRITICAL/FATAL
    )
'''

print "="*79



IGNORE_FIXES=",".join([
    #~ "E101", # Reindent all lines.
    #~ "E112", # Fix under-indented comments.
    #~ "E113", # Fix over-indented comments.
    "E115", # Fix under-indented comments.
    #~ "E116", # Fix over-indented comments.
    #~ "E121", # Fix a badly indented line.
    #~ "E122", # Fix a badly indented line.
    #~ "E123", # Fix a badly indented line.
    "E124", # Fix a badly indented line.
    #~ "E125", # Fix indentation undistinguish from the next logical line.
    #~ "E126", # Fix a badly indented line.
    #~ "E127", # Fix a badly indented line.
    "E128", # Fix a badly indented line.
    #~ "E129", # Fix a badly indented line.
    #~ "E201", # Remove extraneous whitespace.
    #~ "E202", # Remove extraneous whitespace.
    #~ "E203", # Remove extraneous whitespace.
    #~ "E211", # Remove extraneous whitespace.
    #~ "E221", # Fix extraneous whitespace around keywords.
    #~ "E222", # Fix extraneous whitespace around keywords.
    #~ "E223", # Fix extraneous whitespace around keywords.
    #~ "E224", # Remove extraneous whitespace around operator.
    #~ "E225", # Fix missing whitespace around operator.
    #~ "E226", # Fix missing whitespace around operator.
    #~ "E227", # Fix missing whitespace around operator.
    #~ "E228", # Fix missing whitespace around operator.
    #~ "E231", # Add missing whitespace.
    #~ "E231", # Fix various deprecated code (via lib2to3).
    #~ "E241", # Fix extraneous whitespace around keywords.
    #~ "E242", # Remove extraneous whitespace around operator.
    #~ "E251", # Remove whitespace around parameter '=' sign.
    #~ "E261", # Fix spacing after comment hash.
    #~ "E262", # Fix spacing after comment hash.
    "E265", # Format block comments.
    #~ "E271", # Fix extraneous whitespace around keywords.
    #~ "E272", # Fix extraneous whitespace around keywords.
    #~ "E273", # Fix extraneous whitespace around keywords.
    #~ "E274", # Fix extraneous whitespace around keywords.
    "E301", # Add missing blank line.
    #~ "E302", # Add missing 2 blank lines.
    #~ "E303", # Remove extra blank lines.
    #~ "E304", # Remove blank line following function decorator.
    "E309", # Add missing blank line.
    #~ "E401", # Put imports on separate lines.
    "E501", # Try to make lines fit within --max-line-length characters.
    #~ "E502", # Remove extraneous escape of newline.
    #~ "E701", # Put colon-separated compound statement on separate lines.
    #~ "E702", # Put semicolon-separated compound statement on separate lines.
    #~ "E703", # Put semicolon-separated compound statement on separate lines.
    #~ "E711", # Fix comparison with None.
    #~ "E712", # Fix comparison with boolean.
    #~ "E713", # Fix non-membership check.
    #~ "E721", # Fix various deprecated code (via lib2to3).
    #~ "W291", # Remove trailing whitespace.
    #~ "W601", # Fix various deprecated code (via lib2to3).
    #~ "W602", # Fix deprecated form of raising exception.
    #~ "W603", # Fix various deprecated code (via lib2to3).
    #~ "W604", # Fix various deprecated code (via lib2to3).
    #~ "W690", # Fix various deprecated code (via lib2to3).



])
list_fixes(IGNORE_FIXES)
print "\nExclude theses fixes to format the following code:"
print " -"*39
print autopep8.fix_code(TEST_CODE,
    options=autopep8.parse_args([
        #~ '--aggressive',
        "--ignore",IGNORE_FIXES,
        ''
    ])
)

print "="*79
list_fixes() # List all existing fixes
print "="*79

Ausgabe in diesem Fall sieht so aus:

Code: Alles auswählen

===============================================================================
List fixes E115, E124, E128, E265, E301, E309, E501:

"E115", # Fix under-indented comments.
"E124", # Fix a badly indented line.
"E128", # Fix a badly indented line.
"E265", # Format block comments.
"E301", # Add missing blank line.
"E309", # Add missing blank line.
"E501", # Try to make lines fit within --max-line-length characters.

Exclude theses fixes to format the following code:
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

import os
import sys

import onw_module1
import onw_module2


class Foo(object):
    """
    a doc string
    """
    def menu_event_about(self):
#         deactivated_line=True
        tkMessageBox.showinfo("DragonPy",  # A comment
            "DragonPy the OpenSource emulator written in python.\n"
            "more info: https://github.com/jedie/DragonPy"
        )

        log.error("%04x| write $%02x to $%04x -> PIA 0 A side Data reg.	|%s",
            op_address, value, address, self.cfg.mem_info.get_shortest(op_address)
        )

if __name__ == '__main__':
    setup_logging(log,
#         level=1 # hardcore debug ;)
#         level=10 # DEBUG
#         level=20 # INFO
#         level=30 # WARNING
#         level=40 # ERROR
        level=50  # CRITICAL/FATAL
    )

===============================================================================
List of all existing fixes:

"E101", # Reindent all lines.
"E112", # Fix under-indented comments.
"E113", # Fix over-indented comments.
"E115", # Fix under-indented comments.
"E116", # Fix over-indented comments.
"E121", # Fix a badly indented line.
"E122", # Fix a badly indented line.
"E123", # Fix a badly indented line.
"E124", # Fix a badly indented line.
"E125", # Fix indentation undistinguish from the next logical line.
"E126", # Fix a badly indented line.
"E127", # Fix a badly indented line.
"E128", # Fix a badly indented line.
"E129", # Fix a badly indented line.
"E201", # Remove extraneous whitespace.
"E202", # Remove extraneous whitespace.
"E203", # Remove extraneous whitespace.
"E211", # Remove extraneous whitespace.
"E221", # Fix extraneous whitespace around keywords.
"E222", # Fix extraneous whitespace around keywords.
"E223", # Fix extraneous whitespace around keywords.
"E224", # Remove extraneous whitespace around operator.
"E225", # Fix missing whitespace around operator.
"E226", # Fix missing whitespace around operator.
"E227", # Fix missing whitespace around operator.
"E228", # Fix missing whitespace around operator.
"E231", # Add missing whitespace.
"E231", # Fix various deprecated code (via lib2to3).
"E241", # Fix extraneous whitespace around keywords.
"E242", # Remove extraneous whitespace around operator.
"E251", # Remove whitespace around parameter '=' sign.
"E261", # Fix spacing after comment hash.
"E262", # Fix spacing after comment hash.
"E265", # Format block comments.
"E271", # Fix extraneous whitespace around keywords.
"E272", # Fix extraneous whitespace around keywords.
"E273", # Fix extraneous whitespace around keywords.
"E274", # Fix extraneous whitespace around keywords.
"E301", # Add missing blank line.
"E302", # Add missing 2 blank lines.
"E303", # Remove extra blank lines.
"E304", # Remove blank line following function decorator.
"E309", # Add missing blank line.
"E401", # Put imports on separate lines.
"E501", # Try to make lines fit within --max-line-length characters.
"E502", # Remove extraneous escape of newline.
"E701", # Put colon-separated compound statement on separate lines.
"E702", # Put semicolon-separated compound statement on separate lines.
"E703", # Put semicolon-separated compound statement on separate lines.
"E711", # Fix comparison with None.
"E712", # Fix comparison with boolean.
"E713", # Fix non-membership check.
"E721", # Fix various deprecated code (via lib2to3).
"W291", # Remove trailing whitespace.
"W601", # Fix various deprecated code (via lib2to3).
"W602", # Fix deprecated form of raising exception.
"W603", # Fix various deprecated code (via lib2to3).
"W604", # Fix various deprecated code (via lib2to3).
"W690", # Fix various deprecated code (via lib2to3).
===============================================================================


Hintergrund ist der, das man bei der neuen PyDev Version "autopep8" nutzten kann, anstatt die PyDev eigene Formatierung. Aber so alle Regeln finde ich nicht geeignet.
z.B. "E115", # Fix under-indented comments.

Weil PyDev leider "#" für Kommentare immer am Zeilen Anfang einfügt, kommt nach der Formatierung dann das raus:

Code: Alles auswählen

class Foo(object):
    """
    a doc string
    """
    def menu_event_about(self):
        #         deactivated_line=True
        tkMessageBox.showinfo("DragonPy",  # A comment
s. Zeile 6


Mit dem Skript kann man halt schnell sehen, was passiert. Dazu in der IGNORE_FIXES fixes deaktivieren und vergleichen.
Evtl. muß der Beispielcode erweitert werden...

Alles nicht so schön, aber brauchbar um seien "--ignore" Liste zusammen zu bekommen...

CMS in Python: http://www.pylucid.org
GitHub | Open HUB | Xing | Linked in
Bitcoins to: 1JEgSQepxGjdprNedC9tXQWLpS424AL8cd
Antworten