采用的autopep8库,该库的安装命令:
pip install autopep8现在我们有一个python的文件,可以用该库来一键美化自己的代码,提高代码可读性。
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)
该python文件在我的D盘中zzz文件夹,名为1.py,通过执行下面命令来查看修改的结果(只打印不修改文件)
autopep8 --aggressive --aggressive 1.py执行结果如下:
感觉是不是变得好看多了,如果感觉不错可以使用其他命令直接把上面结果写入原文件中。执行该命令后会直接把源代码修改为PEP8编码风格的代码。
autopep8 --in-place --aggressive --aggressive 1.py
其他高级用法可以查看autopep8库的官网:https://pypi.org/project/autopep8/#more-advanced-usage
皇领蜂鸟
皇陵蜂鸟?:鸟蜂的英文一种颜色鲜艳的小型鸟,也是世界最小的鸟类。它体长3〜5厘米,重约20克,多数生活在茂密的森林中。皇领蜂鸟就是其中一种,有着独特的生活方式。
它们分布于南美洲(包括哥伦比亚,委内瑞拉,圭亚那,苏里南,厄瓜多尔,秘鲁,玻利维亚,巴拉圭,巴西,智利,阿根廷,乌拉圭以及马尔维纳斯群岛(也称福克兰群岛))。
保护级度:
EN濒危物种。