在Pycharm中配置autopep8自动规范编排你的Python代码

什么是Autopep8
在python开发中, 大家都知道,python编码规范是PEP8,但是在市级开发中有的公司严格要求PEP8规范开发, 有的公司不会在乎那些,在我的理解中,程序员如果想走的更高,或者更远,干任何事情必须得专业化(本人理解方式), 不要求很多东西都是精通,但最少得有一门精通的语言,小弟在此在大佬面前装逼了, 忘看过的大牛不要揭穿, 留下你懂的我不懂的知识,大家一起学习,一起进步。 谢谢。
Autopep8是一个将python代码自动编排的一个工具,它使用pep8工具来决定代码中的那部分需要被排版,Autopep8可以修复大部分pep8工具中报告的排版问题。很多人都知道 Ctrl+Alt+L 也可以排版, 但是我要告诉你,快捷键只是可以简单的排版。跟Autopep8是无法相比的。
PEP 8,Style Guide for Python Code,是Python官方推出的Python编码风格的约定,虽然这不是硬性的规定,但是如果Python程序员都尽量遵循这个文档,那么编码风格的统一会让代码的可读性大大提升。
在Pycharm里边默认也是有进行PEP8的检测,强迫症的人表示,看到代码中有黄色波浪线,就一定得先改好它。

关于autopep8
官网的描述是:
autopep8 automatically formats Python code to conform to the PEP 8 style guide. It uses the pep8 utility to determine what parts of the code needs to be formatted. autopep8 is capable of fixing most of the formatting issues that can be reported by pep8.

通过它,可以修复大部分PEP8工具中报告的代码排版问题。举个官网的例子:

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)

这是一个比较极端情况的例子,在使用了autopep8自动修复后:

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)

是不是看起来焕然一新了?

Pycharm中使用autopep8作为扩展工具

  • 安装autopep8
pip install autopep8
  • Pycharm进行设置
Settings–>Tools–>External Tools 点击添加按钮
Name:autopep8(可以自定义)
Tools settings:
Programs:autopep8(不能修改)
Parameters:--in-place --aggressive --aggressive $FilePath$
Working directory:$ProjectFileDir$
点击Output Files
点击添加,名称可以任意填写
Regular expression to match output:$FILE_PATH$\:$LINE$\:$COLUMN$\:.*
实际使用
在右击上代码–>External Tool–>autopep8

Pycharm自动调用了autopep8对当前文件进行PEP8优化。
  • autopep8的一些设置点
在上边说到,在Parameters的设置是: --in-place --aggressive --aggressive $FilePath$
–in-place 代表会直接修改源文件
–aggressive autopep8默认只修复空白,对齐相关的PEP8问题,加入--aggressive设置,会增加修复如 x == None 修复为 x is None,{“a”: 1, “b”: 2}.has_key(‘a’) 修复为’a’ in {“a”: 1, “b”: 2}
–ignore 忽略PEP8检查项
因为我只打算用autopep8来修复空格,空行这一类的排版问题,同时要忽略每一行长度过长的检测(E501 - Try to make lines fit within –max-line-length characters.),
所以最终设置是:--in-place --ignore=E501 $FilePath$
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值