专栏名称: Python程序员
最专业的Python社区,有每日推送,免费电子书,真人辅导,资源下载,各类工具。我已委托“维权骑士”(rightknights.com)为我的文章进行维权行动
目录
相关文章推荐
Python爱好者社区  ·  这才是最适合新手的python教程,640页超详细 ·  5 天前  
Python爱好者社区  ·  神仙公司名单北京篇 ·  4 天前  
Python开发者  ·  深度学习六十年简史 ·  5 天前  
Python中文社区  ·  重磅!上交所更改交易时间,明天起执行 ·  1 周前  
Python爱好者社区  ·  国家奖学金,最有用的一次。。。 ·  1 周前  
51好读  ›  专栏  ›  Python程序员

Python到python的编译器允许你在老版本中使用Python 3.6的一些功能

Python程序员  · 公众号  · Python  · 2017-05-09 08:15

正文

Python部落(python.freelycode.com)组织翻译,禁止转载,欢迎转发。

Py-backwards

Python到python的编译器允许你在老版本中使用Python 3.6的一些功能,你可以通过这个在线样例(https://py-backwards.herokuapp.com/)试用它。

需要Python 3.3+ 来运行,可以向下编译至Python 2.7。

支持的特性

目标 3.5:

  • 格式化字符串常量:如 f"hi {x}" (https://docs.python.org/3/whatsnew/3.6.html#pep-498-formatted-string-literals)

  • 变量注释:如 x: int = 10 和 x: int (https://docs.python.org/3/whatsnew/3.6.html#whatsnew36-pep526)

  • 数字常量中的下划线:如 1_000_000(自动进行) (https://docs.python.org/3/whatsnew/3.6.html#pep-515-underscores-in-numeric-literals)

目标 3.4:

  • 带星标的(元组)解包:如 [*range(1, 5), *range(10, 15)] 和 print(*[1, 2], 3, *[4, 5]) (https://docs.python.org/3/whatsnew/3.5.html#pep-448-additional-unpacking-generalizations)

  • 字典解包:如 {1: 2, **{3: 4}} (https://docs.python.org/3/whatsnew/3.5.html#pep-448-additional-unpacking-generalizations)

目标 3.3:

  • 导入pathlib2而不是pathlib (https://pypi.python.org/pypi/pathlib2/)

目标 3.2:

  • yield from (https://docs.python.org/3/whatsnew/3.3.html#pep-380)

  • return from generator (https://docs.python.org/3/whatsnew/3.3.html#pep-380)

目标 2.7:

  • 函数注释:如 def fn(a: int) -> str (https://www.python.org/dev/peps/pep-3107/)

  • 从__future__导入 (https://docs.python.org/3/howto/pyporting.html#prevent-compatibility-regressions)

  • 无参数的super (https://www.python.org/dev/peps/pep-3135/)

  • 无基类的类:如 class A: pass

  • 从 six moves 导入 (https://pythonhosted.org/six/#module-six.moves)

  • 元类

举个例子,如果你有一些python 3.6的代码,如下:

你可以用以下命令把它编译成Python2.7:

得到了一些丑丑的代码,但是它确实可用:

用法

安装:

编译代码:

为了用支持的每一个python版本测试编译好的代码, 你可以使用tox(https://tox.readthedocs.io/en/latest/)和tox-py-backwards(https://github.com/nvbn/tox-py-backwards)。你需要安装它们:

填写tox.ini(在testenv部分,设置py_backwards = true来启用py-backwards), 如下:

用tox运行测试:

开发

设置:

运行测试:

在没有docker的系统上运行测试:

来写代码转换器

首先,你需要继承 BaseTransformer, BaseNodeTransformer(如果你想使用NodeTransformer(https://docs.python.org/3/library/ast.html#ast.NodeTransformer) 接口的话),或者 BaseImportRewrite(如果你只想去改变导入的话)。

如果你要使用BaseTransformer的话,重写类方法def transform(cls, tree: ast.AST) -> TransformationResult,如下:

如果你要使用BaseNodeTransformer的话, 重写visit_*方法,为简化起见,这个类在self._tree中有完整的树结构。 如果这个树被修改,你也应该设置self._tree_changed = True:

如果你要使用BaseImportRewrite的话,只重写rewrites就可以了,如下:

之后,你需要把你的转换器添加到transformers.__init__.transformers。

在AST中写代码很难,正因为如此,我们有snippets(https://github.com/nvbn/py-backwards/blob/master/py_backwards/utils/snippet.py#L102):

而且你能够使用以下代码很容易地得到snippet的内容:

也请看看tree utils(https://github.com/nvbn/py-backwards/blob/master/py_backwards/utils/tree.py),它包含非常有用的函数,如find,get_parent等等。



英文原文:https://github.com/nvbn/py-backwards
译者:Xiaogang


推荐文章
Python爱好者社区  ·  这才是最适合新手的python教程,640页超详细
5 天前
Python爱好者社区  ·  神仙公司名单北京篇
4 天前
Python开发者  ·  深度学习六十年简史
5 天前
Python中文社区  ·  重磅!上交所更改交易时间,明天起执行
1 周前
Python爱好者社区  ·  国家奖学金,最有用的一次。。。
1 周前
腾讯汽车  ·  中国女司机要过亿!男人们泪奔吧
7 年前
趣味漫画  ·  躲酒妙方
7 年前