python Autopep8实现按PEP8风格自动排版Python代码


Posted in Python onMarch 02, 2021

Autopep8是一个将Python代码自动排版为PEP8风格的小工具。它使用pep8工具来决定代码中的哪部分需要被排版。Autopep8可以修复大部分pep8工具中报告的排版问题。

参考网址:

https://www.python.org/dev/peps/pep-0008/

https://pypi.python.org/pypi/autopep8/

(1)安装步骤如下:

localhost:~ a6$ sudo pip install autopep8
Password:
The directory '/Users/a6/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/a6/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting autopep8
Collecting pycodestyle>=2.3 (from autopep8)
 Downloading pycodestyle-2.3.1-py2.py3-none-any.whl (45kB)
  100% |????????????????????????????????| 51kB 324kB/s
Installing collected packages: pycodestyle, autopep8
Successfully installed autopep8-1.3.3 pycodestyle-2.3.1
localhost:~ a6$ sudo pip install autopep8
The directory '/Users/a6/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/a6/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Requirement already satisfied: autopep8 in /Library/Python/2.7/site-packages
Requirement already satisfied: pycodestyle>=2.3 in /Library/Python/2.7/site-packages (from autopep8)

(2)示例代码:

1)运行命令前代码的排版 (保存在test_autopep8.py)

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)

2)运行命令

bogon:AB a6$ autopep8 --in-place --aggressive --aggressive test_autopep8.py

3)运行命令后代码的排版

import math
import 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 ('' 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)

4)参考网址:
https://github.com/hhatto/autopep8

到此这篇关于python Autopep8实现按PEP8风格自动排版Python代码的文章就介绍到这了,更多相关python Autopep8自动排版内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python格式化压缩后的JS文件的方法
Mar 05 Python
Python有序查找算法之二分法实例分析
Dec 11 Python
Python文本统计功能之西游记用字统计操作示例
May 07 Python
Pycharm 操作Django Model的简单运用方法
May 23 Python
python实现多人聊天室
Mar 31 Python
Python 给定的经纬度标注在地图上的实现方法
Jul 05 Python
python导包的几种方法(自定义包的生成以及导入详解)
Jul 15 Python
Python enumerate函数遍历数据对象组合过程解析
Dec 11 Python
python 对任意数据和曲线进行拟合并求出函数表达式的三种解决方案
Feb 18 Python
用 Python 制作地球仪的方法
Apr 24 Python
python 利用zmail库发送邮件
Sep 11 Python
Python识别验证码的实现示例
Sep 30 Python
pycharm配置安装autopep8自动规范代码的实现
Mar 02 #Python
Python实现我的世界小游戏源代码
Mar 02 #Python
VSCode中autopep8无法运行问题解决方案(提示Error: Command failed,usage)
Mar 02 #Python
python 基于pygame实现俄罗斯方块
Mar 02 #Python
使用Python快速打开一个百万行级别的超大Excel文件的方法
Mar 02 #Python
Autopep8的使用(python自动编排工具)
Mar 02 #Python
python 将Excel转Word的示例
Mar 02 #Python
You might like
将PHP作为Shell脚本语言使用
2006/10/09 PHP
PHP中break及continue两个流程控制指令区别分析
2011/04/18 PHP
PHP读取ACCESS数据到MYSQL的代码
2011/05/11 PHP
PHP中文件读、写、删的操作(PHP中对文件和目录操作)
2012/03/06 PHP
php开启openssl的方法
2014/05/15 PHP
PHP中一些可以替代正则表达式函数的字符串操作函数
2014/11/17 PHP
php实现微信扫码自动登陆与注册功能
2016/09/22 PHP
VSCode+PHPstudy配置PHP开发环境的步骤详解
2020/08/20 PHP
js三种排序算法分享
2012/08/16 Javascript
浅析ajax请求json数据并用js解析(示例分析)
2013/07/13 Javascript
javascript实现动态模态绑定grid过程代码
2014/09/22 Javascript
2014 HTML5/CSS3热门动画特效TOP10
2014/12/07 Javascript
JS+CSS实现实用的单击输入框弹出选择框的方法
2015/02/28 Javascript
实例详解jQuery Mockjax 插件模拟 Ajax 请求
2016/01/12 Javascript
Bootstrap框架下下拉框select搜索功能
2020/03/26 Javascript
详解处理bootstrap4不支持远程静态框问题
2018/07/20 Javascript
jQuery实现表格隔行换色
2018/09/01 jQuery
利用Vue构造器创建Form组件的通用解决方法
2018/12/03 Javascript
Koa 中的错误处理解析
2019/04/09 Javascript
vue webpack重写cookie路径的方法
2019/07/10 Javascript
el-form 多层级表单的实现示例
2020/09/10 Javascript
pygame播放音乐的方法
2015/05/19 Python
总结网络IO模型与select模型的Python实例讲解
2016/06/27 Python
python 实现网上商城,转账,存取款等功能的信用卡系统
2016/07/15 Python
Python实现一个转存纯真IP数据库的脚本分享
2017/05/21 Python
Django集成CAS单点登录的方法示例
2019/06/10 Python
numpy.array 操作使用简单总结
2019/11/08 Python
经理职责范文
2013/11/08 职场文书
俞敏洪励志演讲稿
2014/04/29 职场文书
物业品质提升方案
2014/06/08 职场文书
会展策划与管理专业求职信
2014/06/09 职场文书
大学生年度个人总结
2015/02/15 职场文书
律师催款函范文
2015/06/24 职场文书
女方家长婚礼致辞
2015/07/27 职场文书
纯CSS实现酷炫的霓虹灯效果
2021/04/13 HTML / CSS
写一个Python脚本下载哔哩哔哩舞蹈区的所有视频
2021/05/31 Python