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求众数问题实例
Sep 26 Python
python通过colorama模块在控制台输出彩色文字的方法
Mar 19 Python
Python单元测试框架unittest使用方法讲解
Apr 13 Python
Python浅拷贝与深拷贝用法实例
May 09 Python
Python设置默认编码为utf8的方法
Jul 01 Python
Python函数式编程
Jul 20 Python
python去掉 unicode 字符串前面的u方法
Oct 21 Python
pyinstaller打包程序exe踩过的坑
Nov 19 Python
Python figure参数及subplot子图绘制代码
Apr 18 Python
Python3安装模块报错Microsoft Visual C++ 14.0 is required的解决方法
Jul 28 Python
如何通过安装HomeBrew来安装Python3
Dec 23 Python
python之django路由和视图案例教程
Jul 26 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
SSO单点登录的PHP实现方法(Laravel框架)
2016/03/23 PHP
JavaScript基本对象
2007/01/11 Javascript
JQuery扩展插件Validate 5添加自定义验证方法
2011/09/05 Javascript
JS中类或对象的定义说明
2014/03/10 Javascript
jquery数组封装使用方法分享(jquery数组遍历)
2014/03/25 Javascript
JavaScript中的setUTCDate()方法使用详解
2015/06/11 Javascript
Angular.js如何从PHP读取后台数据
2016/03/24 Javascript
JS实现重新加载当前页面或者父页面的几种方法
2016/11/30 Javascript
JavaScript中Math对象的方法介绍
2017/01/05 Javascript
js实现简单的网页换肤效果
2017/01/18 Javascript
JS Select下拉框(支持输入模糊查询)
2017/02/04 Javascript
jquery中关于bind()方法的使用技巧分享
2017/03/30 jQuery
node跨域请求方法小结
2017/08/25 Javascript
JavaScript实现为事件句柄绑定监听函数的方法分析
2017/11/14 Javascript
利用jquery和BootStrap实现动态滚动条效果
2018/12/03 jQuery
js根据需要计算数组中重复出现某个元素的个数
2019/01/18 Javascript
JS实现判断有效的数独算法示例
2019/02/25 Javascript
mpvue实现微信小程序快递单号查询代码
2020/04/03 Javascript
Vue文本模糊匹配功能如何实现
2020/07/30 Javascript
vue实现按钮切换图片
2021/01/20 Vue.js
[01:01:51]EG vs VG Supermajor小组赛B组 BO3 第二场 6.2
2018/06/03 DOTA
python3使用scrapy生成csv文件代码示例
2017/12/28 Python
Python lambda函数基本用法实例分析
2018/03/16 Python
python把1变成01的步骤总结
2019/02/27 Python
对python中url参数编码与解码的实例详解
2019/07/25 Python
Python操作Sqlite正确实现方法解析
2020/02/05 Python
解决pytorch下出现multi-target not supported at的一种可能原因
2021/02/06 Python
英国网上香水店:Fragrance Direct
2016/07/20 全球购物
给市场的环保建议书
2014/05/14 职场文书
社团活动总结格式
2014/08/29 职场文书
中小学生学籍证明
2014/10/25 职场文书
工人先锋号申报材料
2014/12/29 职场文书
大学生学年个人总结
2015/02/15 职场文书
2015年推普周活动方案
2015/05/06 职场文书
中学语文教学反思
2016/02/16 职场文书
Linux中Nginx的防盗链和优化的实现代码
2021/06/20 Servers