关于PEP 8

PEP 8,Style Guide forPythonCode,是Python官方推出编码约定,主要是为了保证 Python 编码的风格一致,提高代码的可读性。

官网地址:https://www.python.org/dev/peps/pep-0008/

关于Autopep8

Autopep8是自动将Python代码格式化为符合PEP 8风格的工具。它使用pycodestyle工具来确定代码的哪些部分需要被格式化。Autopep8能够修复大部分pycodestyle检测的格式问题。

github地址:https://github.com/hhatto/autopep8

下载安装Autopep8

pip install autopep8

使用Autopep8

命令行使用方式如下

$ autopep8 --in-place --aggressive --aggressive <filename>
$ autopep8 --in-place --aggressive --aggressive Student.py

Pycharm配置Autopep8方法

1)具体流程:选择菜单「File」–>「Settings」–>「Tools」–>「External Tools」–>设置相关配置 -> 点击加号添加工具

填写如下配置项,点击「OK」保存

Settings–>Tools–>External Tools 点击添加按钮Name:autopep8(可以自定义)

Tools settings:

Programs:autopep8(不能修改)
Parameters:--in-place --aggressive --aggressive $FilePath$
Working directory:$ProjectFileDir$

Advanced Options:在output filters添加:$FILE_PATH$\:$LINE$\:$COLUMN$\:.*

3) 使用autopep8自动格式化你的python代码

import math


def example1():
  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
    else:
      some_string = """
            Indentation in multiline strings should not be touched.Only actual code should be reindented.
"""

第一种方式:

编写完代码后,右键选择「Extern Tools」–>「autopep8」

Pycharm配置autopep8实现流程解析

第二种方式:

选择菜单「Tool」–>「Extern Tools」–>「autopep8」即可

Pycharm配置autopep8实现流程解析

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?