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正则表达式修复网站文章字体不统一的解决方法
Feb 21 Python
使用70行Python代码实现一个递归下降解析器的教程
Apr 17 Python
python 3.6 tkinter+urllib+json实现火车车次信息查询功能
Dec 20 Python
Python基于辗转相除法求解最大公约数的方法示例
Apr 04 Python
Python smtplib实现发送邮件功能
May 22 Python
Python面向对象程序设计类变量与成员变量、类方法与成员方法用法分析
Apr 12 Python
Python中使用双下划线防止类属性被覆盖问题
Jun 27 Python
使用Python中的reduce()函数求积的实例
Jun 28 Python
python全栈知识点总结
Jul 01 Python
Python concurrent.futures模块使用实例
Dec 24 Python
python链表类中获取元素实例方法
Feb 23 Python
详解Django中的FBV和CBV对比分析
Mar 01 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的数据库接口和技术
2016/12/09 PHP
PHP7如何开启Opcode打造强悍性能详解
2018/05/11 PHP
PHP call_user_func和call_user_func_array函数的简单理解与应用分析
2019/11/25 PHP
JavaScript splice()方法详解
2020/09/22 Javascript
javascript生成随机颜色示例代码
2014/05/05 Javascript
js使用for循环与innerHTML获取选中tr下td值
2014/09/26 Javascript
jQuery中noconflict函数的实现原理分解
2015/02/03 Javascript
Jquery跨浏览器文本复制插件Zero Clipboard的使用方法
2016/02/28 Javascript
JavaScript计时器用法分析【setTimeout和clearTimeout】
2017/01/18 Javascript
vue实现验证码输入框组件
2017/12/14 Javascript
angularjs实现对表单输入改变的监控(ng-change和watch两种方式)
2018/08/29 Javascript
浅谈针对Vue相同路由不同参数的刷新问题
2018/09/29 Javascript
微信小程序自定义导航栏实例代码
2019/04/05 Javascript
vue-cli点击实现全屏功能
2020/03/07 Javascript
原生js实现html手机端城市列表索引选择城市
2020/06/24 Javascript
vue2和vue3的v-if与v-for优先级对比学习
2020/10/10 Javascript
javascript实现搜索筛选功能实例代码
2020/11/12 Javascript
Python自定义主从分布式架构实例分析
2016/09/19 Python
Python序列操作之进阶篇
2016/12/08 Python
利用Python写一个爬妹子的爬虫
2018/06/08 Python
解决python opencv无法显示图片的问题
2018/10/28 Python
对Python 语音识别框架详解
2018/12/24 Python
Python字符串格式化输出代码实例
2019/11/22 Python
Python进程的通信Queue、Pipe实例分析
2020/03/30 Python
使用pytorch实现论文中的unet网络
2020/06/24 Python
python基于selenium爬取斗鱼弹幕
2021/02/20 Python
英国泽西岛植物:Jersey Plants Direct
2019/08/07 全球购物
会计专业毕业生推荐信
2013/11/05 职场文书
新闻发布会主持词
2014/03/28 职场文书
教师爱岗敬业演讲稿
2014/05/05 职场文书
学校清明节活动总结
2014/07/04 职场文书
安全检查汇报材料
2014/12/26 职场文书
辩论赛主持人开场白
2015/05/29 职场文书
社区服务活动感想
2015/08/11 职场文书
2016年公司新年寄语
2015/08/17 职场文书
win11开机发生死循环重启怎么办?win11开机发生死循环重启解决方法
2022/08/05 数码科技