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基于DES算法加密解密实例
Jun 03 Python
使用Python保存网页上的图片或者保存页面为截图
Mar 05 Python
Python利用IPython提高开发效率
Aug 10 Python
python  Django中的apps.py的目的是什么
Oct 15 Python
python中将正则过滤的内容输出写入到文件中的实例
Oct 21 Python
Python离线安装PIL 模块的方法
Jan 08 Python
详解Python解决抓取内容乱码问题(decode和encode解码)
Mar 29 Python
浅析Windows 嵌入python解释器的过程
Jul 26 Python
基于Django ORM、一对一、一对多、多对多的全面讲解
Jul 26 Python
keras-siamese用自己的数据集实现详解
Jun 10 Python
Python调用REST API接口的几种方式汇总
Oct 19 Python
python opencv通过按键采集图片源码
May 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+mysql开源XNA 聚合程序发布 下载
2007/07/13 PHP
PHP函数addslashes和mysql_real_escape_string的区别
2014/04/22 PHP
php微信公众平台开发(三)订阅事件处理
2016/12/06 PHP
php从身份证获取性别和出生年月
2017/02/09 PHP
yii2.0整合阿里云oss删除单个文件的方法
2017/09/19 PHP
JS 密码强度验证(兼容IE,火狐,谷歌)
2010/03/15 Javascript
JavaScript toFixed() 方法
2010/04/15 Javascript
前台js对象在后台转化java对象的问题探讨
2013/12/20 Javascript
jQuery实现类似淘宝网图片放大效果的方法
2015/07/08 Javascript
js图片翻书效果代码分享
2015/08/20 Javascript
Jquery 自定义事件实现发布/订阅的简单实例
2016/06/12 Javascript
基于CSS3和jQuery实现跟随鼠标方位的Hover特效
2016/07/25 Javascript
Vue.js学习示例分享
2017/02/05 Javascript
jQuery页面弹出框实现文件上传
2017/02/09 Javascript
Angularjs 事件指令详细整理
2017/07/27 Javascript
引入JavaScript时alert弹出框显示中文乱码问题
2017/09/16 Javascript
Node.js爬取豆瓣数据实例分析
2018/03/05 Javascript
简单的vuex 的使用案例笔记
2018/04/13 Javascript
vue动画之点击按钮往上渐渐显示出来的实例
2018/09/29 Javascript
详解vue后台系统登录态管理
2019/04/02 Javascript
解决layui checkbox 提交多个值的问题
2019/09/02 Javascript
[01:02:46]VGJ.S vs NB 2018国际邀请赛小组赛BO2 第二场 8.18
2018/08/19 DOTA
Python实现控制台中的进度条功能代码
2017/12/22 Python
selenium + python 获取table数据的示例讲解
2018/10/13 Python
python对列进行平移变换的方法(shift)
2019/01/10 Python
详解python3 + Scrapy爬虫学习之创建项目
2019/04/12 Python
Python中remove漏删和索引越界问题的解决
2020/03/18 Python
python中tkinter窗口位置\坐标\大小等实现示例
2020/07/09 Python
python 如何快速复制序列
2020/09/07 Python
Python爬虫scrapy框架Cookie池(微博Cookie池)的使用
2021/01/13 Python
ProBikeKit英国:在线公路自行车之家
2017/02/10 全球购物
澳大利亚儿童鞋在线:The Trybe
2019/07/16 全球购物
优质飞蝇钓和渔具:RiverBum
2020/05/10 全球购物
文明单位创建材料
2014/12/24 职场文书
2015年乡镇科普工作总结
2015/05/13 职场文书
CSS中calc(100%-100px)不加空格不生效
2023/05/07 HTML / CSS