Autopep8的使用(python自動(dòng)編排工具)
什么是Autopep8
在python開發(fā)中, 大家都知道,python編碼規(guī)范是PEP8,但是在市級開發(fā)中有的公司嚴(yán)格要求PEP8規(guī)范開發(fā), 有的公司不會(huì)在乎那些,在我的理解中,程序員如果想走的更高,或者更遠(yuǎn),干任何事情必須得專業(yè)化(本人理解方式), 不要求很多東西都是精通,但最少得有一門精通的語言,小弟在此在大佬面前裝逼了, 忘看過的大牛不要揭穿, 留下你懂的我不懂的知識(shí),大家一起學(xué)習(xí),一起進(jìn)步。 謝謝。
Autopep8是一個(gè)將python代碼自動(dòng)編排的一個(gè)工具,它使用pep8工具來決定代碼中的那部分需要被排版,Autopep8可以修復(fù)大部分pep8工具中報(bào)告的排版問題。很多人都知道 Ctrl+Alt+L 也可以排版, 但是我要告訴你,快捷鍵只是可以簡單的排版。跟Autopep8是無法相比的。
安裝Autopep8:
pip install autopep8
安裝完成之后,import導(dǎo)入一下,測試是否安裝成功。
Aytopep8的使用
安裝完成之后,打開pycharm,創(chuàng)建一個(gè)新的python文件, demo.py 將一下代碼放入文件中。
def example1():
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
else:
some_string = """
Indentation in multiline strings should not be touched.Only actual code should be reindented.
"""
這幾行代碼看上去是不是很亂, 接下來就要使用:Autopep8模塊了
打開cmd找到demo.py的文件的上級目錄,
然后輸入以下命令:
autopep8 --in-place --aggressive --aggressive file.py
file.py 是你的demo.py
輸入命令,按回車執(zhí)行成功是不返回的, 執(zhí)行完成之后就可以了,在次打開文件就可以看到變化了。
import math
import sys
def example1():
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
else:
some_string = """
Indentation in multiline strings should not be touched.Only actual code should be reindented.
"""
執(zhí)行完Autopep8之后代碼是不是看上去簡潔多了。
有人會(huì)說,沒寫一個(gè)函數(shù)就執(zhí)行一遍命令, 是不是有點(diǎn)麻煩啊, 是的, 有有點(diǎn)麻煩, 但是pycharm是可以配置的, 配置過程如下:
1: File ---> Settings ---> Tools ---> External Tools
打開之后,可以看見窗體左上角有一個(gè) + 號(hào), 點(diǎn)擊+號(hào)添加。

Name: 名稱可以隨意
Program: autopep8 # 前提必須先安裝
Arguments: --in-place --aggressive --aggressive $FilePath$
Working directory: $ProjectFileDir$
Advanced Options
---- Outputfilters:
$FILE_PATH$\:$LINE$\:$COLUMN$\:.*
以上配置完成之后點(diǎn)擊 OK 保存即可。
快捷使用:

Tools ---> External Tools ---> Autopep8 鼠標(biāo)點(diǎn)擊一下即可。

到此這篇關(guān)于Autopep8的使用(python自動(dòng)編排工具)的文章就介紹到這了,更多相關(guān)Autopep8 使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用PyTorch構(gòu)建神經(jīng)網(wǎng)絡(luò)的操作指南
PyTorch 是一個(gè)在研究領(lǐng)域廣泛使用的深度學(xué)習(xí)框架,提供了大量的靈活性和效率,本文將向你介紹如何使用 PyTorch 構(gòu)建你的第一個(gè)神經(jīng)網(wǎng)絡(luò),感興趣的小伙伴可以參考閱讀2023-07-07
淺談python數(shù)據(jù)結(jié)構(gòu)之動(dòng)態(tài)規(guī)劃
這篇文章主要介紹了淺談python數(shù)據(jù)結(jié)構(gòu)之動(dòng)態(tài)規(guī)劃,可能很多小伙伴會(huì)覺得這個(gè)詞很陌生,覺得這是一種很復(fù)雜的思想,學(xué)習(xí)起來很困難,其實(shí)并不是這樣,動(dòng)態(tài)規(guī)劃所講述的知識(shí)與動(dòng)態(tài)與規(guī)劃并無太大關(guān)聯(lián),需要的朋友可以參考下2023-07-07
Python存儲(chǔ)與讀寫二進(jìn)制文件的示例代碼
本文介紹了如何在Python中使用Numpy將數(shù)組轉(zhuǎn)存為二進(jìn)制文件,并通過內(nèi)存映射的方式讀取,提供了一個(gè)高效處理大量數(shù)據(jù)的方法,示例代碼展示了完整的存儲(chǔ)和讀取過程,解釋了頁大小和數(shù)據(jù)存儲(chǔ)的關(guān)系,適用于需要高性能計(jì)算和數(shù)據(jù)壓縮的場景,感興趣的朋友跟隨小編一起看看吧2024-09-09
如何使用python轉(zhuǎn)移mysql數(shù)據(jù)庫中的全部數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了如何使用python轉(zhuǎn)移mysql數(shù)據(jù)庫中的全部數(shù)據(jù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解下2024-11-11
Python實(shí)現(xiàn)可獲取網(wǎng)易頁面所有文本信息的網(wǎng)易網(wǎng)絡(luò)爬蟲功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)可獲取網(wǎng)易頁面所有文本信息的網(wǎng)易網(wǎng)絡(luò)爬蟲功能,涉及Python針對網(wǎng)頁的獲取、字符串正則判定等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01

