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中break语句的用法
May 14 Python
Python的Django框架中消息通知的计数器实现教程
Jun 13 Python
CentOS下使用yum安装python-pip失败的完美解决方法
Aug 16 Python
Python3.4实现从HTTP代理网站批量获取代理并筛选的方法示例
Sep 26 Python
Python enumerate索引迭代代码解析
Jan 19 Python
python图形工具turtle绘制国际象棋棋盘
May 23 Python
pyinstaller打包程序exe踩过的坑
Nov 19 Python
python实现横向拼接图片
Mar 23 Python
Python中Selenium模块的使用详解
Oct 09 Python
Python实现Kerberos用户的增删改查操作
Dec 14 Python
python中的被动信息搜集
Apr 29 Python
python数字转对应中文的方法总结
Aug 02 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 XPath对XML文件查找及修改实现代码
2011/07/27 PHP
教你识别简单的免查杀PHP后门
2015/09/13 PHP
php测试kafka项目示例
2020/02/06 PHP
Thinkphp框架使用list_to_tree 实现无限级分类列出所有节点示例
2020/04/04 PHP
JavaScript arguments 多参传值函数
2010/10/24 Javascript
JS跨域总结
2012/08/30 Javascript
JavaScript Array对象扩展indexOf()方法
2014/05/09 Javascript
基于jQuery实现仿淘宝套餐选择插件
2015/03/04 Javascript
javascript实现随机显示星星特效
2016/01/28 Javascript
JavaScript判断是否是微信浏览器
2016/06/13 Javascript
基于HTML+CSS+JS实现增加删除修改tab导航特效代码
2016/08/05 Javascript
jQuery焦点图轮播效果实现方法
2016/12/19 Javascript
详解Nodejs基于mongoose模块的增删改查的操作
2016/12/21 NodeJs
jQuery实现两个select控件的互移操作
2016/12/22 Javascript
nodejs中向HTTP响应传送进程的输出
2017/03/19 NodeJs
微信小程序商品详情页规格属性选择示例代码
2017/10/30 Javascript
JS实现的将html转为pdf功能【基于浏览器端插件jsPDF】
2018/02/06 Javascript
echarts同一页面中四个图表切换的js数据交互方法示例
2018/07/03 Javascript
使用Layui搭建后台管理界面的操作方法
2019/09/20 Javascript
python 图片验证码代码
2008/12/07 Python
python使用正则表达式分析网页中的图片并进行替换的方法
2015/03/26 Python
python中Genarator函数用法分析
2015/04/08 Python
把csv文件转化为数组及数组的切片方法
2018/07/04 Python
django项目搭建与Session使用详解
2018/10/10 Python
Python面向对象程序设计类变量与成员变量、类方法与成员方法用法分析
2019/04/12 Python
Django 简单实现分页与搜索功能的示例代码
2019/11/07 Python
解决python 找不到module的问题
2020/02/12 Python
Python虚拟环境的创建和使用详解
2020/09/07 Python
Pretty Green美国:英式摇滚服饰风格代表品牌之一
2019/01/23 全球购物
行政管理专业推荐信
2013/11/02 职场文书
企事业单位求职者的自我评价
2013/12/28 职场文书
幼儿园大班评语大全
2014/04/17 职场文书
本溪水洞导游词
2015/02/11 职场文书
2015年双拥工作总结
2015/04/08 职场文书
单身证明范本
2015/06/15 职场文书
学校运动会简讯
2015/07/20 职场文书