杂谈——Python代码写得丑怎么办?autopep8来帮你

系统 1812 0

目录

      • 官网
      • 前言
      • 安装
      • 配置
      • 展示

官网

autopep8 · PyPI——https://pypi.org/project/autopep8/

前言

Python 编程语言需要遵循 PEP8 规范,但是很多人在编写代码时往往记不住这个规范,代码写得比较丑。这怎么办呢?别担心,autopep8 来帮你。

autopep8 可以自动格式化 Python 代码以符合 PEP8 规范。它使用 pycodestyle 实用程序来确定需要格式化代码的是哪些部分。autopep8 能够修复 pycodestyle 可以报告的大多数格式问题。只需要使用 autopep8,麻麻再也不用担心我的代码不规范了。

安装

使用 pip 进行安装即可,不会安装 pip 的可以看一下这个博客——python中pip安装、升级、升级指定的包。安装 pip 之后,运行 cmd 命令窗。
杂谈——Python代码写得丑怎么办?autopep8来帮你_第1张图片
杂谈——Python代码写得丑怎么办?autopep8来帮你_第2张图片
杂谈——Python代码写得丑怎么办?autopep8来帮你_第3张图片
使用如下命令安装 autopep8 即可。

pip install autopep8

配置

文件(File)- 设置(Settings)- 工具(Tools)- 外部工具(External Tools)- 添加(+)
杂谈——Python代码写得丑怎么办?autopep8来帮你_第4张图片
杂谈——Python代码写得丑怎么办?autopep8来帮你_第5张图片
然后会出现这个界面,下面就可以设置配置参数了。
杂谈——Python代码写得丑怎么办?autopep8来帮你_第6张图片
Name:

            
              autopep8

            
          

Program:

            
              autopep8

            
          

Arguments:

            
              --in-place --aggressive --aggressive $FilePath$

            
          

Working directory:

            
              $ProjectFileDir$

            
          

Output filters:

            
              $FILE_PATH$\:$LINE$\:$COLUMN$\:.*

            
          

把上面的配置参数按照对应的名字填写就可以了,具体配置如下图:
杂谈——Python代码写得丑怎么办?autopep8来帮你_第7张图片
然后点击 OK - Apply - OK 即可。
杂谈——Python代码写得丑怎么办?autopep8来帮你_第8张图片
到这里就已经设置完毕了,使用方法也比较简单,将鼠标在文件编辑器中 - 点击右键 - External Tools - autopep8,这样就会自动运行 autopep8,帮你规范化代码。
杂谈——Python代码写得丑怎么办?autopep8来帮你_第9张图片

展示

使用 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
              
                )
              
            
          

使用 autopep8 后:
在这里插入图片描述

            
              
                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 了。


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论