-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test #5
Comments
[TOC] Python 2.x 和 Python3.x 的区别前言future 模块Python 3 介绍的 一些 Python 2 不兼容的关键字和特性可以通过在 Python 2 的内置 from __future__ import division 官方参考文档编码源码文件编码在 python 2.x 版本,python 源码文件默认使用 而 python 3.x 源码文件默认以 字符串编码早期 Unicode 标准并未发布时,python 的字符串 到了 python 3 以后, 标准输入输出输出删除了 python 2 的 print "fish"
print ("fish") # 注意print后面有个空格
print("fish") # print()不能带有任何其它参数 如果 Python 2.x 版本想使用使用 Python 3.x 的 print 函数,可以导入 from __future__ import print_function
print("fish", "panda", sep=', ') 输入在 python 2 中常用两个函数进行输入:
而 python 3 只有 运算符除法python 2 中除法
python 3 中 不等运算符Python 2.x 中不等于有两种写法 八进制的表示方式Python 2 可以用0开头或者0o开头表示八进制,python 3 只能用0o开头表示。 数据类型删除 long 类型python 3 删除了 新增 bytes 类型python 3 新增了二进制序列类型 bytes。bytes 对象是由单个字节构成的不可变序列,即字符经过编码的以八位二进制为单位的序列。 另外,在 Python 2.x 系列中,允许 8 位字符串( 2.x 所提供的最接近内置二进制数据类型的对象)与 Unicode 字符串进行各种隐式转换。 这是为了实现向下兼容的变通做法,以适应 Python 最初只支持 8 位文本而 Unicode 文本是后来才被加入这一事实。 在 Python 3.x 中,这些隐式转换已被取消 —— 8 位二进制数据与 Unicode 文本间的转换必须显式地进行,bytes 与字符串对象的比较结果将总是不相等。 返回迭代器map、filter 和 reduce 等高阶函数返回值的类型由列表改为迭代器,另外 reduce 函数在 Python 3.x 中已经不属于 built-in 了,被挪到 functools 模块当中。 返回视图对象字典的 关于类经典类与新式类python 2 允许创建一个经典类,即不继承任何类,包括 python 3 不再支持经典类,若定义时不显式继承任何类,则默认继承 特殊函数 superpython 2 使用 class C(B):
def method(self, arg):
# super().父类对应方法(父类的参数)
super().method(arg)
# 上一句写法在Python3等价于如下语句
# super(C, self).method(arg) 删除 repr 表达式Python 2.x 中反引号``相当于 Python 3.x 中去掉了这种写法,只允许使用 range 与 xrangepython 2.x 中 python 3.x 中只保留了 另外,在 3.3 版更改: 定义 '==' 和 '!=' 以根据 range 对象所定义的值序列来进行比较(而不是根据对象的标识)。也就是说,如果两个 range 对象表示相同的值序列就认为它们是相等的。请注意比较结果相等的两个 range 对象可能会具有不同的 start,stop 和 step 属性,例如 异常在 Python 3 中处理异常也轻微的改变了,在 Python 3 中我们现在使用 as 作为关键词。 捕获异常的语法由 使用语法
布尔值
# python 2.x
print True
print False
True = 123
False = 321
print True
print False
# 输出结果如下:
# True
# False
# 123
# 321 Python 3 修正了这个缺陷,True 和 False 变为两个关键字,永远指向两个固定的对象,不允许再被重新赋值。 新增 nonlocal 语句
回收列表推导式的变量在 python 2中,如下列表推导式中 for 语句的变量 x 在后面的代码中可被使用: y = [x for x in xrange(10)]
print x
print y
# 输出如下:
# 9
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 而 python 3,变量 x 作为局部变量在列表推导式执行结束后就被回收了,后续无法访问。 y = [x for x in range(10)]
print(x)
print(y)
# 输出:
# Traceback (most recent call last):
# File "<input>", line 2, in <module>
# NameError: name 'x' is not defined 不可排序类型的报错当对不可排序类型做比较的时候,python 2 会返回 # python 2.x
print [1, 2, 3] > 'abc'
# False
# python 3.x
print([1, 2, 3] > 'abc')
# Traceback (most recent call last):
# File "E:/CS_Practice/python/2021/main.py", line 4, in <module>
# print([1, 2, 3] > 'abc')
# TypeError: '>' not supported between instances of 'list' and 'str' |
gh-md-tocgh-md-toc — is for you if you want to generate TOC (Table Of Content) for a README.md or It's my try to fix a problem: gh-md-toc is able to process:
gh-md-toc tested on Ubuntu, and macOS High Sierra (gh-md-toc release 0.4.9). If you want it on Windows, you It's more solid, reliable and with ability of a parallel processing. And Table of contentsInstallationLinux (manual installation) $ wget https://raw.githubusercontent.com/ekalinin/github-markdown-toc/master/gh-md-toc
$ chmod a+x gh-md-toc MacOS (manual installation) $ curl https://raw.githubusercontent.com/ekalinin/github-markdown-toc/master/gh-md-toc -o gh-md-toc
$ chmod a+x gh-md-toc Linux or MacOS (using Basher) $ basher install ekalinin/github-markdown-toc
# `gh-md-toc` will automatically be available in the PATH UsageSTDINHere's an example of TOC creating for markdown from STDIN: ➥ cat ~/projects/Dockerfile.vim/README.md | ./gh-md-toc -
* [Dockerfile.vim](#dockerfilevim)
* [Screenshot](#screenshot)
* [Installation](#installation)
* [OR using Pathogen:](#or-using-pathogen)
* [OR using Vundle:](#or-using-vundle)
* [License](#license) Local filesHere's an example of TOC creating for a local README.md: ➥ ./gh-md-toc ~/projects/Dockerfile.vim/README.md
Table of Contents
=================
* [Dockerfile.vim](#dockerfilevim)
* [Screenshot](#screenshot)
* [Installation](#installation)
* [OR using Pathogen:](#or-using-pathogen)
* [OR using Vundle:](#or-using-vundle)
* [License](#license) Remote filesAnd here's an example, when you have a README.md like this: And you want to generate TOC for it. There is nothing easier: ➥ ./gh-md-toc https://github.com/ekalinin/envirius/blob/master/README.md
Table of Contents
=================
* [envirius](#envirius)
* [Idea](#idea)
* [Features](#features)
* [Installation](#installation)
* [Uninstallation](#uninstallation)
* [Available plugins](#available-plugins)
* [Usage](#usage)
* [Check available plugins](#check-available-plugins)
* [Check available versions for each plugin](#check-available-versions-for-each-plugin)
* [Create an environment](#create-an-environment)
* [Activate/deactivate environment](#activatedeactivate-environment)
* [Activating in a new shell](#activating-in-a-new-shell)
* [Activating in the same shell](#activating-in-the-same-shell)
* [Get list of environments](#get-list-of-environments)
* [Get current activated environment](#get-current-activated-environment)
* [Do something in environment without enabling it](#do-something-in-environment-without-enabling-it)
* [Get help](#get-help)
* [Get help for a command](#get-help-for-a-command)
* [How to add a plugin?](#how-to-add-a-plugin)
* [Mandatory elements](#mandatory-elements)
* [plug_list_versions](#plug_list_versions)
* [plug_url_for_download](#plug_url_for_download)
* [plug_build](#plug_build)
* [Optional elements](#optional-elements)
* [Variables](#variables)
* [Functions](#functions)
* [Examples](#examples)
* [Example of the usage](#example-of-the-usage)
* [Dependencies](#dependencies)
* [Supported OS](#supported-os)
* [Tests](#tests)
* [Version History](#version-history)
* [License](#license)
* [README in another language](#readme-in-another-language) That's all! Now all you need — is copy/paste result from console into original If you do not want to copy from console you can add And here is a result: Moreover, it's able to work with GitHub's wiki pages: ➥ ./gh-md-toc https://github.com/ekalinin/nodeenv/wiki/Who-Uses-Nodeenv
Table of Contents
=================
* [Who Uses Nodeenv?](#who-uses-nodeenv)
* [OpenStack](#openstack)
* [pre-commit.com](#pre-commitcom) Multiple filesIt supports multiple files as well: ➥ ./gh-md-toc \
https://github.com/aminb/rust-for-c/blob/master/hello_world/README.md \
https://github.com/aminb/rust-for-c/blob/master/control_flow/README.md \
https://github.com/aminb/rust-for-c/blob/master/primitive_types_and_operators/README.md \
https://github.com/aminb/rust-for-c/blob/master/unique_pointers/README.md
* [Hello world](https://github.com/aminb/rust-for-c/blob/master/hello_world/README.md#hello-world)
* [Control Flow](https://github.com/aminb/rust-for-c/blob/master/control_flow/README.md#control-flow)
* [If](https://github.com/aminb/rust-for-c/blob/master/control_flow/README.md#if)
* [Loops](https://github.com/aminb/rust-for-c/blob/master/control_flow/README.md#loops)
* [For loops](https://github.com/aminb/rust-for-c/blob/master/control_flow/README.md#for-loops)
* [Switch/Match](https://github.com/aminb/rust-for-c/blob/master/control_flow/README.md#switchmatch)
* [Method call](https://github.com/aminb/rust-for-c/blob/master/control_flow/README.md#method-call)
* [Primitive Types and Operators](https://github.com/aminb/rust-for-c/blob/master/primitive_types_and_operators/README.md#primitive-types-and-operators)
* [Unique Pointers](https://github.com/aminb/rust-for-c/blob/master/unique_pointers/README.md#unique-pointers) ComboYou can easily combine both ways: ➥ ./gh-md-toc \
~/projects/Dockerfile.vim/README.md \
https://github.com/ekalinin/sitemap.s/blob/master/README.md
* [Dockerfile.vim](~/projects/Dockerfile.vim/README.md#dockerfilevim)
* [Screenshot](~/projects/Dockerfile.vim/README.md#screenshot)
* [Installation](~/projects/Dockerfile.vim/README.md#installation)
* [OR using Pathogen:](~/projects/Dockerfile.vim/README.md#or-using-pathogen)
* [OR using Vundle:](~/projects/Dockerfile.vim/README.md#or-using-vundle)
* [License](~/projects/Dockerfile.vim/README.md#license)
* [sitemap.js](https://github.com/ekalinin/sitemap.js/blob/master/README.md#sitemapjs)
* [Installation](https://github.com/ekalinin/sitemap.js/blob/master/README.md#installation)
* [Usage](https://github.com/ekalinin/sitemap.js/blob/master/README.md#usage)
* [License](https://github.com/ekalinin/sitemap.js/blob/master/README.md#license)
Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc) Auto insert and update TOCJust put into a file these two lines:
And run: $ ./gh-md-toc --insert README.test.md
Table of Contents
=================
* [gh-md-toc](#gh-md-toc)
* [Installation](#installation)
* [Usage](#usage)
* [STDIN](#stdin)
* [Local files](#local-files)
* [Remote files](#remote-files)
* [Multiple files](#multiple-files)
* [Combo](#combo)
* [Tests](#tests)
* [Dependency](#dependency)
!! TOC was added into: 'README.test.md'
!! Origin version of the file: 'README.test.md.orig.2018-02-04_192655'
!! TOC added into a separate file: 'README.test.md.toc.2018-02-04_192655'
Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc) Now check the same file: ➜ grep -A15 "<\!\-\-ts" README.test.md
<!--ts-->
* [gh-md-toc](#gh-md-toc)
* [Table of contents](#table-of-contents)
* [Installation](#installation)
* [Usage](#usage)
* [STDIN](#stdin)
* [Local files](#local-files)
* [Remote files](#remote-files)
* [Multiple files](#multiple-files)
* [Combo](#combo)
* [Auto insert and update TOC](#auto-insert-and-update-toc)
* [Tests](#tests)
* [Dependency](#dependency)
<!-- Added by: <your-user>, at: 2018-02-04T19:38+03:00 -->
<!--te--> Next time when your file will be changed just repeat the command ( GitHub tokenAll your tokens are here. You will need them if you get an error like this:
A token can be used as an env variable: ➥ GH_TOC_TOKEN=2a2dab...563 ./gh-md-toc README.md
Table of Contents
=================
* [github\-markdown\-toc](#github-markdown-toc)
* [Table of Contents](#table-of-contents)
* [Installation](#installation)
* [Tests](#tests)
* [Usage](#usage)
* [LICENSE](#license) Or from a file: ➥ echo "2a2dab...563" > ./token.txt
➥ ./gh-md-toc README.md
Table of Contents
=================
* [github\-markdown\-toc](#github-markdown-toc)
* [Table of Contents](#table-of-contents)
* [Installation](#installation)
* [Tests](#tests)
* [Usage](#usage)
* [LICENSE](#license) TOC generation with Github ActionsConfig: on:
push:
branches: [main]
paths: ['foo.md']
jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@v2
- run: |
curl https://raw.githubusercontent.com/ekalinin/github-markdown-toc/master/gh-md-toc -o gh-md-toc
chmod a+x gh-md-toc
./gh-md-toc --insert --no-backup foo.md
- uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Auto update markdown TOC TestsDone with bats.
How to run tests: ➥ make test
✓ TOC for local README.md
✓ TOC for remote README.md
✓ TOC for mixed README.md (remote/local)
✓ TOC for markdown from stdin
✓ --help
✓ --version
6 tests, 0 failures Dependency
Tested on Ubuntu 14.04/14.10 in bash/zsh. DockerLocal
$ docker build -t markdown-toc-generator .
$ docker run -it markdown-toc-generator https://github.com/ekalinin/envirius/blob/master/README.md
$ docker run -it -v /data/ekalinin/envirius:/data markdown-toc-generator /data/README.md Public$ docker pull evkalinin/gh-md-toc:0.7.0
$ docker images | grep toc
evkalinin/gh-md-toc 0.7.0 0b8db6aed298 11 minutes ago 147MB
$ docker run -it evkalinin/gh-md-toc:0.7.0 \
https://github.com/ekalinin/envirius/blob/master/README.md |
[TOC]
Python 2.x 和 Python3.x 的区别
前言
future 模块
Python 3 介绍的 一些 Python 2 不兼容的关键字和特性可以通过在 Python 2 的内置
__future__
模块导入。如果你计划让你的代码支持 Python 3,建议你使用__future__
模块导入。例如,如果我想要 在Python 2 中表现 Python 3 中的整除,我们可以通过如下导入。官方参考文档
Python 2.0 到 3.9 的全部新变化
编码
源码文件编码
在 python 2.x 版本,python 源码文件默认使用
Ascii
编码,所以无法直接显示中文。需要在源码文件头部通过声明# -*- encoding: utf-8 -*-
来告诉解释器使用utf-8
编码处理源文件。而 python 3.x 源码文件默认以
UTF-8
编码方式处理。字符串编码
早期 Unicode 标准并未发布时,python 的字符串
str
默认使用ASCII
进行编码。后来python2.0
提供了一种新的存储文本数据的数据类型:Unicode
对象,添加了对 Unicode 的支持。它可以用来存储和操作Unicode
数据。所以 python 2 同时有Ascii
编码的str
和Unicode
字符串。到了 python 3 以后,
python
删除了Unicode
对象类型,使字符串均由Unicode
编码。标准输入输出
输出
删除了 python 2 的
print
语句 ,取而代之的是print()
函数。 Python 2.6 与 Python 2.7 部分地支持这种形式的print
语法。在 Python 2.6 与Python 2.7 里面,以下三种形式是等价的:如果 Python 2.x 版本想使用使用 Python 3.x 的 print 函数,可以导入
__future__
包,该包禁用 Python2.x 的print
语句,采用 Python 3.x 的print
函数输入
在 python 2 中常用两个函数进行输入:
input()
把用户的输入按照代码进行解释,得到解释后的值,例如:
raw_input()
将所有的输入识别为字符串。
而 python 3 只有
input()
函数且等价于 python 2 的raw_input()
函数。运算符
除法
python 2 中除法
a/b
:a//b
,无论a、b为何种数字类型结果都向下取整,只是a、b有浮点数时结果取整后以浮点数形式显示(5.0//2=2.0)。python 3 中
a/b
默认结果总为浮点数,a//b
与python2一致。不等运算符
Python 2.x 中不等于有两种写法
!=
和<>
,Python 3.x 中去掉了<>
, 只有!=
一种写法。八进制的表示方式
Python 2 可以用0开头或者0o开头表示八进制,python 3 只能用0o开头表示。
数据类型
删除 long 类型
python 3 删除了
long
类型,现在只有一种整型——int
,但它的行为就像 2.x 版本的long
。新增 bytes 类型
python 3 新增了二进制序列类型 bytes。bytes 对象是由单个字节构成的不可变序列,即字符经过编码的以八位二进制为单位的序列。
另外,在 Python 2.x 系列中,允许 8 位字符串( 2.x 所提供的最接近内置二进制数据类型的对象)与 Unicode 字符串进行各种隐式转换。 这是为了实现向下兼容的变通做法,以适应 Python 最初只支持 8 位文本而 Unicode 文本是后来才被加入这一事实。 在 Python 3.x 中,这些隐式转换已被取消 —— 8 位二进制数据与 Unicode 文本间的转换必须显式地进行,bytes 与字符串对象的比较结果将总是不相等。
返回迭代器
map、filter 和 reduce 等高阶函数返回值的类型由列表改为迭代器,另外 reduce 函数在 Python 3.x 中已经不属于 built-in 了,被挪到 functools 模块当中。
返回视图对象
字典的
keys()
、values()
、items()
方法在 python 2 中返回列表,在 python 3 中返回可迭代的视图对象。关于类
经典类与新式类
python 2 允许创建一个经典类,即不继承任何类,包括
object
类。创建新式类要显式继承父类。python 3 不再支持经典类,若定义时不显式继承任何类,则默认继承
object
类。特殊函数 super
python 2 使用
super()
在子类方法关联父类对应方法时必须要传递父类名和子类的self
给super()
作为参数,而在 python 3 可以省略super()
的参数。删除 repr 表达式
Python 2.x 中反引号``相当于
repr
函数的作用,返回一个字符串对象。Python 3.x 中去掉了这种写法,只允许使用
repr
函数。range 与 xrange
python 2.x 中
range
函数返回一个数字列表,而xrange
函数返回 xrange 对象,它表示一个不可变的数字序列。xrange 对象相比常规 list 或 tuple 的优势在于一个 xrange 对象总是占用固定数量的(较小)内存,不论其所表示的范围有多大(因为它只保存了 start, stop 和 step 值,并会根据需要计算具体单项或子范围的值)。这一点与生成器的惰性计算有点相似,它是可迭代对象但并不是生成器或者迭代器。python 3.x 中只保留了
range
函数,它与xrange
十分相似,但是支持了更多功能,如 3.2 支持了切片操作。另外,在 3.3 版更改: 定义 '==' 和 '!=' 以根据 range 对象所定义的值序列来进行比较(而不是根据对象的标识)。也就是说,如果两个 range 对象表示相同的值序列就认为它们是相等的。请注意比较结果相等的两个 range 对象可能会具有不同的 start,stop 和 step 属性,例如
range(0) == range(2, 1, 3)
而range(0, 3, 2) == range(0, 4, 2)
。异常
在 Python 3 中处理异常也轻微的改变了,在 Python 3 中我们现在使用 as 作为关键词。
捕获异常的语法由
except exc, var
改为except exc as var
。使用语法
except (exc1, exc2) as var
可以同时捕获多种类别的异常。 Python 2.6 已经支持这两种语法。BaseException
的对象才可以被抛出。raise
语句使用逗号将抛出对象类型和参数分开,3.x 取消了这种奇葩的写法,直接调用构造函数抛出对象即可。如raise TypeError('MyRange expected 1 arguments, got 0')
。布尔值
True
和False
在 Python 2 中是两个全局变量,既然是变量,那么他们就可以指向其它对象,例如:Python 3 修正了这个缺陷,True 和 False 变为两个关键字,永远指向两个固定的对象,不允许再被重新赋值。
新增 nonlocal 语句
nonlocal
语句会使得所列出的名称指向之前在最近的包含作用域中绑定的除全局变量以外的变量。例如内层函数的局部变量n绑定外层函数的局部变量n。 这种功能很重要,因为绑定的默认行为是先搜索局部命名空间。 这个语句允许被封装的代码重新绑定局部作用域以外且非全局(模块)作用域当中的变量。回收列表推导式的变量
在 python 2中,如下列表推导式中 for 语句的变量 x 在后面的代码中可被使用:
而 python 3,变量 x 作为局部变量在列表推导式执行结束后就被回收了,后续无法访问。
不可排序类型的报错
当对不可排序类型做比较的时候,python 2 会返回
False
,python 3 会抛出一个类型错误:The text was updated successfully, but these errors were encountered: