python格式化代码工具_python 代码格式化工具:YAPF
背景
現(xiàn)在的大多數(shù) Python 代碼格式化工具(比如:autopep8 和 pep8ify)是可以移除代碼中的 lint 錯誤。這顯然有些局限性。比如:遵循 PEP 8 指導(dǎo)的代碼可能就不會被格式化了,但這并不說明代碼看起來就舒服。
但 YAPF 獨辟蹊徑。它脫胎于由 Daniel Jasper 開發(fā)的 clang-format。大體上來說,這個算法獲取代碼,然后把初始代碼重新編排,即便初始代碼并沒有違背規(guī)范,也可使其達(dá)到遵循代碼規(guī)范的最佳格式。這個理念和 Go 語言中的 gofmt 工具相似,終結(jié)關(guān)于格式的各種“圣戰(zhàn)”。如果一個項目的代碼庫,無論何時修改,通過 YAPF 優(yōu)化后,代碼風(fēng)格可統(tǒng)一,在每次 code review 中,也就沒有必要爭論風(fēng)格了。
YAPF 的終極目標(biāo)是生成和遵循代碼規(guī)范的程序員寫出的一樣的代碼。可幫你減少維護(hù)代碼的苦差事。
YAPF 支持 Python 2.7 和 3.4+。
安裝
#pip install yapf
用法
usage: yapf [-h] [-v] [-d | -i] [-r | -l START-END] [-e PATTERN]
[--style STYLE] [--style-help] [--no-local-style]
[--verify]
[files [files ...]]
FormatterforPython code.
positional arguments:
files
optional arguments:-h, --help show this help message and exit-v, --version show version number and exit-d, --diff print the diff forthe fixed source-i, --in-place make changes to files inplace-r, --recursive run recursively over directories-l START-END, --lines START-END
range of lines to reformat, one-based-e PATTERN, --exclude PATTERN
patternsforfiles to exclude from formatting--style STYLE specify formatting style: either a style name (forexample"pep8" or "google"), or the name of a filewith style settings. The default is pep8 unless a
.style.yapf or setup.cfgfile located inone of the
parent directories of the sourcefile(or current
directoryforstdin)--style-help show style settings and exit--no-local-style don't search for local style definition (.style.yapf)
--verify try to verify reformatted code for syntax errors
具體用法
使用yapf的兩種方式是: FormatCode 和 FormatFile
FormatCode的參數(shù)為代碼內(nèi)容。
>>> from yapf.yapf_api import FormatCode #reformat a string of code
>>> FormatCode("f ( a = 1, b = 2 )")'f(a=1, b=2)\n'
我這邊 from yapf.yapf_api import FormatCode 會提示“from yapf.yapf_api import FormatCode”錯誤,但是“import yapf"是好的
本人的使用方式:
>>>importyapf>>>yapf.yapf_api.FormatCode("f ( a = 1, b = 2 )")
('f(a=1, b=2)\n',True)
style_config參數(shù):使用特定的style
style_config的值可以是一個格式樣式設(shè)置的文件路徑,也可以是一個樣式名。
如果不進(jìn)行定義的話,使用style.DEFAULT_STYLE_FACTORY設(shè)定的默認(rèn)樣式。
>>> FormatCode("def g():\n return True", style_config='pep8')'def g():\n return True\n'
lines參數(shù):設(shè)定要應(yīng)用樣式的特定行
>>> FormatCode("def g( ):\n a=1\n b = 2\n return a==b", lines=[(1, 1), (2, 3)])'def g():\n a = 1\n b = 2\n return a==b\n'
print_diff參數(shù):返回源文件和更改后的文件之間的diff
感覺跟linux的diff很像,表示不習(xí)慣。
>>> print(FormatCode("a==b", filename="foo.py", print_diff=True))---foo.py (original)+++foo.py (reformatted)
@@-1 +1@@-a==b+a == b
FormatFile 的參數(shù)是文件。
>>> from yapf.yapf_api import FormatFile
假設(shè)需要更改的源文件是”foo.py”。
>>> print(open("foo.py").read()) #查看文件內(nèi)容
x= { 'a':37,'b':42,'c':927}
y= 'hello''world'z= 'hello'+'world'a= 'hello {}'.format('world')classfoo ( object ):deff (self ):return 37*-+2
def g(self, x,y=42):returnydeff ( a ) :return 37+-+a[42-x : y**3]>>> FormatFile("foo.py")
('a == b\n', 'utf-8')
in_place參數(shù):如果值為True的話,會直接用更改好的內(nèi)容替代源文件
>>> FormatFile("foo.py", in_place=True)
(None,'utf-8')
總結(jié)
以上是生活随笔為你收集整理的python格式化代码工具_python 代码格式化工具:YAPF的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 晶方科技是龙头股吗 半导体封测龙头股
- 下一篇: 上海银行信用卡如何进行二手车分期 这样有