国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

按PEP8風(fēng)格自動(dòng)排版Python代碼

2019-11-06 06:21:23
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

如果用編輯器寫代碼,很容易會(huì)怎么順手怎么來(lái),而每個(gè)人的寫代碼習(xí)慣有很大不同,這樣會(huì)導(dǎo)致可讀性不是很好,代碼維護(hù)起來(lái)比較困難,當(dāng)然各種編輯器一般都有各種用來(lái)自動(dòng)排版的插件,例如Sublime Text就有:Python PEP8 Autoformat,使用方法:

默認(rèn)快捷鍵:

Ctrl + Shift + R

不過(guò),python的一些install腳本還是很強(qiáng)大的,例如python的autopep8庫(kù): (鏈接:https://github.com/hhatto/autopep8)

$ pip install --upgrade autopep8

然后在命令行:

$ autopep8 --in-place --aggressive --aggressive <filename>

就可以對(duì)代碼進(jìn)行pep8自動(dòng)排版,對(duì)比了下效果,如下: 排版前

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 de 使用Sublime Text中的Python PEP8 Autoformat自動(dòng)排版

import mathimport sysdef 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自動(dòng)排版

import mathimport sysdef 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)

可以看到雖然都是PEP8風(fēng)格,但是效果還是略有不同,可以根據(jù)需要個(gè)人習(xí)慣進(jìn)行有選擇的使用。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 周宁县| 招远市| 酒泉市| 伊春市| 龙州县| 咸宁市| 陆川县| 贺州市| 荥阳市| 黑龙江省| 皋兰县| 化州市| 渭南市| 林西县| 监利县| 德江县| 普陀区| 汝南县| 威远县| 沧源| 城市| 澄城县| 隆回县| 苍南县| 凤台县| 台中县| 鹤山市| 六安市| 手机| 哈尔滨市| 同仁县| 星座| 平阳县| 金山区| 南平市| 齐河县| 嘉禾县| 沁水县| 兴隆县| 田阳县| 靖边县|