[Unlock] autopep8 - python code formatting tools

python code formatting tools

autopep8 Python code can be automatically formatted to match the PEP 8 style guide. It uses pycodestyle which parts of the code to determine the required format. Most formats can solve the problem autopep8 pycodestyle report.

installation

$ pip install --upgrade autopep8

use

Before formatting codes

import math, sys;

def example1():
    ####This is a long comment. This should be wrapped to fit within 72 characters.
    some_tuple=(   1,2, 3,'a'  );
    some_variable={'long':'Long code lines should be wrapped within 79 characters.',
    'other':[math.pi, 100,200,300,9876543210,'This is a long string that goes on'],
    'more':{'inner':'This whole logical line should be wrapped.',some_tuple:[1,
    20,300,40000,500000000,60000000000000000]}}
    return (some_tuple, some_variable)
def example2(): return {'has_key() is deprecated':True}.has_key({'f':2}.has_key(''));
class Example3(   object ):
    def __init__    ( self, bar ):
     #Comments should have a space after the hash.
     if bar : bar+=1;  bar=bar* bar   ; return bar
     else:
                    some_string = """
                       Indentation in multiline strings should not be touched.
Only actual code should be reindented.
"""
                    return (sys.path, some_string)

After formatting code

import math
import sys


def example1():
    # This is a long comment. This should be wrapped to fit within 72
    # characters.
    some_tuple = (1, 2, 3, 'a')
    some_variable = {
        'long': 'Long code lines should be wrapped within 79 characters.',
        'other': [
            math.pi,
            100,
            200,
            300,
            9876543210,
            'This is a long string that goes on'],
        'more': {
            'inner': 'This whole logical line should be wrapped.',
            some_tuple: [
                1,
                20,
                300,
                40000,
                500000000,
                60000000000000000]}}
    return (some_tuple, some_variable)


def example2(): return ('' in {'f': 2}) in {'has_key() is deprecated': True}


class Example3(object):
    def __init__(self, bar):
        # Comments should have a space after the hash.
        if bar:
            bar += 1
            bar = bar * bar
            return bar
        else:
            some_string = """
                       Indentation in multiline strings should not be touched.
Only actual code should be reindented.
"""
            return (sys.path, some_string)

Parameter Description

Automatically formats Python code to conform to the PEP 8 style guide.

positional arguments:
  files                 files to format or '-' for standard in

optional arguments:
  -h, --help            show this help message and exit
  --version             show program s version number and exit
  -v, --verbose         print verbose messages; multiple -v result in more
                        verbose messages
  -d, --diff            print the diff for the fixed source
  -i, --in-place        make changes to files in place
  --global-config filename
                        path to a global pep8 config file; if this file does
                        not exist then this is ignored (default:
                        /home/frank/.config/pep8)
  --ignore-local-config
                        don't look for and apply local config files; if not
                        passed, defaults are updated with any config files in
                        the project's root directory
  -r, --recursive       run recursively over directories; must be used with
                        --in-place or --diff
  -j n, --jobs n        number of parallel jobs; match CPU count if value is
                        less than 1
  -p n, --pep8-passes n
                        maximum number of additional pep8 passes (default:
                        infinite)
  -a, --aggressive      enable non-whitespace changes; multiple -a result in
                        more aggressive changes
  --experimental        enable experimental fixes
  --exclude globs       exclude file/directory names that match these comma-
                        separated globs
  --list-fixes          list codes for fixes; used by --ignore and --select
  --ignore errors       do not fix these errors/warnings (default:
                        E226,E24,W50,W690)
  --select errors       fix only these errors/warnings (e.g. E4,W)
  --max-line-length n   set maximum allowed line length (default: 79)
  --line-range line line, --range line line
                        only fix errors found within this inclusive range of
                        line numbers (e.g. 1 99); line numbers are indexed at
                        1
  --hang-closing        hang-closing option passed to pycodestyle
  --exit-code           change to behavior of exit code. default behavior of
                        return value, 0 is no differences, 1 is error exit.
                        return 2 when add this option. 2 is exists
                        differences.

Feature

autopep8 fixes the following issues pycodestyle report:

E101-重新插入所有行。
E11-修复缩进。
E121-将缩进固定为四的倍数。
E122-为悬挂的凹痕添加不存在的凹痕。
E123-将关闭支架对准打开支架。
E124-对齐右括号以匹配视觉缩进。
E125-缩进以区分行与下一条逻辑行。
E126-修复过度缩进的悬挂式缩进。
E127-修复视觉缩进。
E128-修复视觉缩进。
E129-修复视觉缩进。
E131-修复悬空凹痕,以确保未对齐的延续线。
E133-修复缺少的凹痕,用于闭合括号。
E20-删除多余的空格。
E211-删除多余的空格。
E22-修正关键字周围多余的空格。
E224-删除操作员周围的多余空白。
E225-修正运算符周围缺少的空格。
E226-修正算术运算符周围缺少的空格。
E227-修复按位/移位运算符周围缺少的空格。
E228-修正模运算符周围缺少的空格。
E231-添加缺少的空格。
E241-修正关键字周围多余的空格。
E242-删除操作员周围的多余空白。
E251-删除参数“ =”符号周围的空格。
E252-参数等于周围缺少空格。
E26-修正内嵌评论的评论哈希后的间距。
E265-修复块注释的注释哈希后的间距。
E266-修复了太多的前导“#”以阻止注释。
E27-修正关键字周围多余的空格。
E301-添加缺少的空白行。
E302-添加缺少的2空行。
E303-删除多余的空白行。
E304-删除功能装饰器后面的空白行。
E305-函数或类结束后,预期有2个空行。
E306-嵌套定义前应有1个空白行。
E401-将进口放在单独的行上。
E402-修复模块级别导入不在文件顶部
E501-尝试使行适合--max-line-length个字符。
E502-删除多余的换行符。
E701-将冒号分隔的复合语句放在单独的行上。
E70-将用分号分隔的复合语句放在单独的行上。
E711-修正“无”的比较。
E712-修正与布尔值的比较。
E713-使用“不参加”测试会员资格。
E714-使用“不是”测试对象身份。
E721-使用“ isinstance()”代替直接比较类型。
E722-裸露地修理。
E731-使用def时不分配lambda表达式。
W291-删除尾随空格。
W292-在文件末尾添加一个换行符。
W293-删除空白行上的尾随空格。
W391-删除尾随的空白行。
W503-在二进制运算符之前修复换行符。
W504-修复二进制运算符后的换行符。
W601-使用“输入”而不是“ has_key()”。
W602-修复了引发异常的不建议使用的形式。
W603-使用“!=“代替“ <>”
W604-使用“ repr()”代替反引号。
W605-修正无效的转义序列'x'。
W690-修复各种不赞成使用的代码(通过lib2to3)。
Published 27 original articles · won praise 6 · views 3665

Guess you like

Origin blog.csdn.net/xk_xx/article/details/104209735