日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python格式化代码工具_python 代码格式化工具:YAPF

發布時間:2024/10/8 python 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python格式化代码工具_python 代码格式化工具:YAPF 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

背景

現在的大多數 Python 代碼格式化工具(比如:autopep8 和 pep8ify)是可以移除代碼中的 lint 錯誤。這顯然有些局限性。比如:遵循 PEP 8 指導的代碼可能就不會被格式化了,但這并不說明代碼看起來就舒服。

但 YAPF 獨辟蹊徑。它脫胎于由 Daniel Jasper 開發的 clang-format。大體上來說,這個算法獲取代碼,然后把初始代碼重新編排,即便初始代碼并沒有違背規范,也可使其達到遵循代碼規范的最佳格式。這個理念和 Go 語言中的 gofmt 工具相似,終結關于格式的各種“圣戰”。如果一個項目的代碼庫,無論何時修改,通過 YAPF 優化后,代碼風格可統一,在每次 code review 中,也就沒有必要爭論風格了。

YAPF 的終極目標是生成和遵循代碼規范的程序員寫出的一樣的代碼。可幫你減少維護代碼的苦差事。

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的參數為代碼內容。

>>> 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參數:使用特定的style

style_config的值可以是一個格式樣式設置的文件路徑,也可以是一個樣式名。

如果不進行定義的話,使用style.DEFAULT_STYLE_FACTORY設定的默認樣式。

>>> FormatCode("def g():\n return True", style_config='pep8')'def g():\n return True\n'

lines參數:設定要應用樣式的特定行

>>> 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參數:返回源文件和更改后的文件之間的diff

感覺跟linux的diff很像,表示不習慣。

>>> print(FormatCode("a==b", filename="foo.py", print_diff=True))---foo.py (original)+++foo.py (reformatted)

@@-1 +1@@-a==b+a == b

FormatFile 的參數是文件。

>>> from yapf.yapf_api import FormatFile

假設需要更改的源文件是”foo.py”。

>>> print(open("foo.py").read()) #查看文件內容

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參數:如果值為True的話,會直接用更改好的內容替代源文件

>>> FormatFile("foo.py", in_place=True)

(None,'utf-8')

總結

以上是生活随笔為你收集整理的python格式化代码工具_python 代码格式化工具:YAPF的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。