目录
- 官网
- 前言
- 安装
- 配置
- 展示
官网
autopep8 · PyPI——https://pypi.org/project/autopep8/
前言
            Python 编程语言需要遵循 
            
              PEP8
            
             规范,但是很多人在编写代码时往往记不住这个规范,代码写得比较丑。这怎么办呢?别担心,autopep8 来帮你。
          
            autopep8 可以自动格式化 Python 代码以符合 
            
              PEP8
            
             规范。它使用 
            
              pycodestyle
            
             实用程序来确定需要格式化代码的是哪些部分。autopep8 能够修复 
            
              pycodestyle
            
             可以报告的大多数格式问题。只需要使用 autopep8,麻麻再也不用担心我的代码不规范了。
          
安装
            使用 
            
              pip
            
             进行安装即可,不会安装 
            
              pip
            
             的可以看一下这个博客——python中pip安装、升级、升级指定的包。安装 
            
              pip
            
             之后,运行 
            
              cmd
            
             命令窗。
            
            
               
            
            
            
               
            
            
            
               
            
            
             使用如下命令安装 autopep8 即可。
          
            
              pip install autopep8
            
          
配置
            文件(File)- 设置(Settings)- 工具(Tools)- 外部工具(External Tools)- 添加(+)
            
            
               
            
            
            
               
            
            
             然后会出现这个界面,下面就可以设置配置参数了。
            
            
               
            
            
            
              Name:
            
          
            
              autopep8
            
          
          Program:
            
              autopep8
            
          
          Arguments:
            
              --in-place --aggressive --aggressive $FilePath$
            
          
          Working directory:
            
              $ProjectFileDir$
            
          
          Output filters:
            
              $FILE_PATH$\:$LINE$\:$COLUMN$\:.*
            
          
          
            把上面的配置参数按照对应的名字填写就可以了,具体配置如下图:
            
            
               
            
            
             然后点击 OK - Apply - OK 即可。
            
            
               
            
            
             到这里就已经设置完毕了,使用方法也比较简单,将鼠标在文件编辑器中 - 点击右键 - External Tools - autopep8,这样就会自动运行 autopep8,帮你规范化代码。
            
            
               
            
          
展示
使用 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
              
                )
              
            
          
          是不是比原来更优美了呢?哈哈哈,到这里就 OK 了。


 
             
					 
					