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线程中对join方法的运用的教程
Apr 09 Python
Python自定义线程类简单示例
Mar 23 Python
Python判断对象是否为文件对象(file object)的三种方法示例
Apr 26 Python
selenium获取当前页面的url、源码、title的方法
Jun 12 Python
基于python分析你的上网行为 看看你平时上网都在干嘛
Aug 13 Python
python爬虫-模拟微博登录功能
Sep 12 Python
在vscode中配置python环境过程解析
Sep 28 Python
python中的split()函数和os.path.split()函数使用详解
Dec 21 Python
在python3中实现更新界面
Feb 21 Python
Python绘图之二维图与三维图详解
Aug 04 Python
Python爬虫代理池搭建的方法步骤
Sep 28 Python
Python求区间正整数内所有素数之和的方法实例
Oct 13 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
萌王史莱姆”萌王性别尴尬!那“萌战”归女组还是男?
2018/12/17 日漫
1.PHP简介
2006/10/09 PHP
php5.3中连接sqlserver2000的两种方法(com与ODBC)
2012/12/29 PHP
关于php内存不够用的快速解决方法
2013/10/26 PHP
PHP代码维护,重构变困难的4种原因分析
2016/01/25 PHP
PHP中关键字interface和implements详解
2017/06/14 PHP
基于php双引号中访问数组元素报错的解决方法
2018/02/01 PHP
JS动画效果代码3
2008/04/03 Javascript
表头固定(利用jquery实现原理介绍)
2012/11/08 Javascript
jQuery随便控制任意div隐藏的方法
2013/06/28 Javascript
PHP实现的各种中文编码转换类分享
2015/01/23 Javascript
jQuery实现的placeholder效果完整实例
2016/08/02 Javascript
jQuery ajax MD5实现用户注册即时验证功能
2016/10/11 Javascript
jQuery插件FusionCharts绘制的2D双柱状图效果示例【附demo源码】
2017/05/13 jQuery
Vue filter格式化时间戳时间成标准日期格式的方法
2018/09/16 Javascript
一步一步的了解webpack4的splitChunk插件(小结)
2018/09/17 Javascript
微信小程序日历组件使用方法详解
2018/12/29 Javascript
vue实现简单跑马灯效果
2020/05/25 Javascript
js获取url页面id,也就是最后的数字文件名
2020/09/25 Javascript
浅谈python中截取字符函数strip,lstrip,rstrip
2015/07/17 Python
Python用imghdr模块识别图片格式实例解析
2018/01/11 Python
python opencv实现切变换 不裁减图片
2018/07/26 Python
深入研究HTML5实现图片压缩上传功能
2016/03/25 HTML / CSS
html5应用缓存_动力节点Java学院整理
2017/07/13 HTML / CSS
史泰博(Staples)中国官方网站:办公用品一站式采购
2016/09/05 全球购物
澳大利亚冒险体验:Adrenaline(跳伞、V8赛车、热气球等)
2017/09/18 全球购物
测控技术与通信工程毕业生自荐信范文
2013/12/28 职场文书
大堂副理的岗位职责范文
2014/02/17 职场文书
应届毕业生自荐信例文
2014/02/26 职场文书
2014企业领导班子四风对照检查材料思想汇报
2014/09/17 职场文书
吃空饷专项整治方案
2014/10/27 职场文书
2014党的群众路线教育实践活动总结报告
2014/10/31 职场文书
校运会加油稿大全
2015/07/22 职场文书
2016年国陪研修感言
2015/11/18 职场文书
考教师资格证不要错过的4个最佳时机
2019/07/17 职场文书
Laravel中获取IP的真实地理位置
2021/04/01 PHP