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创建XML文档
Mar 01 Python
Python 多线程抓取图片效率对比
Feb 27 Python
EM算法的python实现的方法步骤
Jan 02 Python
Python如何应用cx_Oracle获取oracle中的clob字段问题
Aug 27 Python
python requests抓取one推送文字和图片代码实例
Nov 04 Python
python groupby 函数 as_index详解
Dec 16 Python
python tkinter GUI绘制,以及点击更新显示图片代码
Mar 14 Python
完美解决pyinstaller打包报错找不到依赖pypiwin32或pywin32-ctypes的错误
Apr 01 Python
Django REST framwork的权限验证实例
Apr 02 Python
Pytorch高阶OP操作where,gather原理
Apr 30 Python
Python如何对XML 解析
Jun 28 Python
python迷宫问题深度优先遍历实例
Jun 20 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 中的类
2006/10/09 PHP
php中变量及部分适用方法
2008/03/27 PHP
比较strtr, str_replace和preg_replace三个函数的效率
2013/06/26 PHP
分享下PHP register_globals 值为on与off的理解
2013/09/26 PHP
基于PHP的简单采集数据入库程序【续篇】
2014/07/30 PHP
php中实现记住密码下次自动登录的例子
2014/11/06 PHP
两款万能的php分页类
2015/11/12 PHP
PHP页面间传递值和保持值的方法
2016/08/24 PHP
php微信公众平台交互与接口详解
2016/11/28 PHP
JSONP获取Twitter和Facebook文章数的具体步骤
2014/02/24 Javascript
论Bootstrap3和Foundation5网格系统的异同
2016/05/16 Javascript
Angular设置title信息解决SEO方面存在问题
2016/08/19 Javascript
Node.js开启Https的实践详解
2016/10/25 Javascript
bootstrap配合Masonry插件实现瀑布式布局
2017/01/18 Javascript
JS匹配日期和时间的正则表达式示例
2017/05/12 Javascript
vue2.0+vuex+localStorage代办事项应用实现详解
2018/05/31 Javascript
Antd下拉选择,自动匹配功能的实现
2020/10/24 Javascript
在python的WEB框架Flask中使用多个配置文件的解决方法
2014/04/18 Python
Python字典操作简明总结
2015/04/13 Python
Python实现统计英文单词个数及字符串分割代码
2015/05/28 Python
python文件与目录操作实例详解
2016/02/22 Python
numpy 进行数组拼接,分别在行和列上合并的实例
2018/05/08 Python
Python高级用法总结
2018/05/26 Python
Python面向对象程序设计类的封装与继承用法示例
2019/04/12 Python
Django集成celery发送异步邮件实例
2019/12/17 Python
pycharm第三方库安装失败的问题及解决经验分享
2020/05/09 Python
python使用for...else跳出双层嵌套循环的方法实例
2020/05/17 Python
实例教程 一款纯css3实现的数字统计游戏
2014/11/10 HTML / CSS
进修护士自我鉴定
2013/10/14 职场文书
优秀导游先进事迹材料
2014/01/25 职场文书
酒店中秋节活动方案
2014/01/31 职场文书
大国崛起观后感
2015/06/02 职场文书
捐款仪式主持词
2015/07/04 职场文书
2016先进集体事迹材料范文
2016/02/25 职场文书
解除租赁合同协议书
2016/03/21 职场文书
Python OpenCV 彩色与灰度图像的转换实现
2021/06/05 Python