日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

雜談——Python代碼寫得丑怎么辦?autopep8來幫你

系統(tǒng) 2076 0

目錄

      • 官網(wǎng)
      • 前言
      • 安裝
      • 配置
      • 展示

官網(wǎng)

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

前言

Python 編程語言需要遵循 PEP8 規(guī)范,但是很多人在編寫代碼時(shí)往往記不住這個(gè)規(guī)范,代碼寫得比較丑。這怎么辦呢?別擔(dān)心,autopep8 來幫你。

autopep8 可以自動(dòng)格式化 Python 代碼以符合 PEP8 規(guī)范。它使用 pycodestyle 實(shí)用程序來確定需要格式化代碼的是哪些部分。autopep8 能夠修復(fù) pycodestyle 可以報(bào)告的大多數(shù)格式問題。只需要使用 autopep8,麻麻再也不用擔(dān)心我的代碼不規(guī)范了。

安裝

使用 pip 進(jìn)行安裝即可,不會(huì)安裝 pip 的可以看一下這個(gè)博客——python中pip安裝、升級(jí)、升級(jí)指定的包。安裝 pip 之后,運(yùn)行 cmd 命令窗。
雜談——Python代碼寫得丑怎么辦?autopep8來幫你_第1張圖片
雜談——Python代碼寫得丑怎么辦?autopep8來幫你_第2張圖片
雜談——Python代碼寫得丑怎么辦?autopep8來幫你_第3張圖片
使用如下命令安裝 autopep8 即可。

pip install autopep8

配置

文件(File)- 設(shè)置(Settings)- 工具(Tools)- 外部工具(External Tools)- 添加(+)
雜談——Python代碼寫得丑怎么辦?autopep8來幫你_第4張圖片
雜談——Python代碼寫得丑怎么辦?autopep8來幫你_第5張圖片
然后會(huì)出現(xiàn)這個(gè)界面,下面就可以設(shè)置配置參數(shù)了。
雜談——Python代碼寫得丑怎么辦?autopep8來幫你_第6張圖片
Name:

            
              autopep8

            
          

Program:

            
              autopep8

            
          

Arguments:

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

            
          

Working directory:

            
              $ProjectFileDir$

            
          

Output filters:

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

            
          

把上面的配置參數(shù)按照對(duì)應(yīng)的名字填寫就可以了,具體配置如下圖:
雜談——Python代碼寫得丑怎么辦?autopep8來幫你_第7張圖片
然后點(diǎn)擊 OK - Apply - OK 即可。
雜談——Python代碼寫得丑怎么辦?autopep8來幫你_第8張圖片
到這里就已經(jīng)設(shè)置完畢了,使用方法也比較簡(jiǎn)單,將鼠標(biāo)在文件編輯器中 - 點(diǎn)擊右鍵 - External Tools - autopep8,這樣就會(huì)自動(dòng)運(yùn)行 autopep8,幫你規(guī)范化代碼。
雜談——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
              
                )
              
            
          

是不是比原來更優(yōu)美了呢?哈哈哈,到這里就 OK 了。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦?。?!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 思茅市| 海原县| 柏乡县| 浠水县| 赤峰市| 枝江市| 孟连| 静安区| 游戏| 塔城市| 龙山县| 芜湖市| 潢川县| 牡丹江市| 沁水县| 梁平县| 宝山区| 铜鼓县| 彩票| 山西省| 兴仁县| 宁城县| 鹿泉市| 乐清市| 西林县| 西贡区| 高阳县| 崇仁县| 汾阳市| 宜君县| 乐山市| 聂拉木县| 抚州市| 武城县| 新泰市| 海盐县| 夹江县| 衢州市| 龙门县| 如东县| 吴旗县|