主頁(yè) > 知識(shí)庫(kù) > python Autopep8實(shí)現(xiàn)按PEP8風(fēng)格自動(dòng)排版Python代碼

python Autopep8實(shí)現(xiàn)按PEP8風(fēng)格自動(dòng)排版Python代碼

熱門(mén)標(biāo)簽:正安縣地圖標(biāo)注app 電銷(xiāo)機(jī)器人系統(tǒng)廠家鄭州 遼寧智能外呼系統(tǒng)需要多少錢(qián) qt百度地圖標(biāo)注 400電話申請(qǐng)資格 螳螂科技外呼系統(tǒng)怎么用 地圖地圖標(biāo)注有嘆號(hào) 阿里電話機(jī)器人對(duì)話 舉辦過(guò)冬奧會(huì)的城市地圖標(biāo)注

Autopep8是一個(gè)將Python代碼自動(dòng)排版為PEP8風(fēng)格的小工具。它使用pep8工具來(lái)決定代碼中的哪部分需要被排版。Autopep8可以修復(fù)大部分pep8工具中報(bào)告的排版問(wèn)題。

參考網(wǎng)址:

https://www.python.org/dev/peps/pep-0008/

https://pypi.python.org/pypi/autopep8/

(1)安裝步驟如下:

localhost:~ a6$ sudo pip install autopep8
Password:
The directory '/Users/a6/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/a6/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting autopep8
Collecting pycodestyle>=2.3 (from autopep8)
 Downloading pycodestyle-2.3.1-py2.py3-none-any.whl (45kB)
  100% |████████████████████████████████| 51kB 324kB/s
Installing collected packages: pycodestyle, autopep8
Successfully installed autopep8-1.3.3 pycodestyle-2.3.1
localhost:~ a6$ sudo pip install autopep8
The directory '/Users/a6/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/a6/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Requirement already satisfied: autopep8 in /Library/Python/2.7/site-packages
Requirement already satisfied: pycodestyle>=2.3 in /Library/Python/2.7/site-packages (from autopep8)

(2)示例代碼:

1)運(yùn)行命令前代碼的排版 (保存在test_autopep8.py)

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)

2)運(yùn)行命令

bogon:AB a6$ autopep8 --in-place --aggressive --aggressive test_autopep8.py

3)運(yùn)行命令后代碼的排版

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)

4)參考網(wǎng)址:
https://github.com/hhatto/autopep8

到此這篇關(guān)于python Autopep8實(shí)現(xiàn)按PEP8風(fēng)格自動(dòng)排版Python代碼的文章就介紹到這了,更多相關(guān)python Autopep8自動(dòng)排版內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Autopep8的使用(python自動(dòng)編排工具)
  • pycharm配置安裝autopep8自動(dòng)規(guī)范代碼的實(shí)現(xiàn)
  • Pycharm配置autopep8實(shí)現(xiàn)流程解析

標(biāo)簽:濟(jì)源 合肥 阜新 淘寶好評(píng)回訪 隨州 興安盟 信陽(yáng) 昭通

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python Autopep8實(shí)現(xiàn)按PEP8風(fēng)格自動(dòng)排版Python代碼》,本文關(guān)鍵詞  python,Autopep8,實(shí)現(xiàn),按,PEP8,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《python Autopep8實(shí)現(xiàn)按PEP8風(fēng)格自動(dòng)排版Python代碼》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于python Autopep8實(shí)現(xiàn)按PEP8風(fēng)格自動(dòng)排版Python代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章