【解锁】autopep8——python代码格式化工具

传送门

python代码格式化工具

autopep8可以自动格式化Python代码以符合PEP 8样式指南。它使用pycodestyle来确定代码的哪些部分需要格式化。autopep8能够解决pycodestyle报告的大多数格式 问题。

安装

$ pip install --upgrade autopep8

使用

格式化前代码

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)

格式化后的代码

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)

参数说明

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.

特点

autopep8修复了pycodestyle报告的以下问题:

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)。
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 西门子U盘写保护格式化工具是一个非常实用的工具,它可以轻松地解决U盘被写保护的问题。在使用U盘的过程中,很容易出现U盘被病毒或意外操作等原因引起写保护的情况,这时我们就需要使用格式化工具进行处理。 西门子U盘写保护格式化工具是一款专门为西门子U盘设计的工具,它可以快速的解决U盘被写保护的问题,并进行格式化操作。它的操作方法非常简单,只需要打开工具,选择需要格式化的U盘,点击格式化按钮即可完成操作。 使用西门子U盘写保护格式化工具,可以有效的避免因U盘写保护而导致的数据丢失和损坏。此外,它还可以在格式化过程中进行数据清空,确保被格式化的U盘上的所有数据都被清除干净。 总之,西门子U盘写保护格式化工具是一个很实用的工具,它可以方便快捷地解决U盘被写保护的问题,并帮助我们更好地管理U盘上的数据。如果你是西门子U盘的用户,那么这个工具是你必备的辅助工具之一。 ### 回答2: 西门子u盘写保护格式化工具是一款特殊的软件工具,用于解决u盘写保护问题和格式化问题。 u盘写保护是指u盘已被锁定,无法进行数据写入操作,此时需要使用专门的写保护解锁工具进行解除。西门子u盘写保护格式化工具正是针对此类问题的解决工具,能够轻松解除u盘的写保护状态。 此外,如果u盘文件系统损坏或出现其它格式化问题,也可以使用该工具进行格式化操作。不同于普通的格式化工具,西门子u盘写保护格式化工具采用了特殊的格式化方式,能够更有效地恢复u盘的正常使用。 使用该工具时,需要注意一些细节。首先,正常情况下不建议频繁进行格式化操作,因为格式化可能会对u盘造成损伤。其次,为了保证操作的准确性,需要根据工具的提示进行操作,不能出现误操作,否则可能会导致数据丢失。 综上所述,西门子u盘写保护格式化工具是一款非常实用的工具,能够解决u盘写保护和格式化问题,使用时需要注意细节,不要频繁使用,以免造成不必要的损害。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏 克

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值