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:
目标 3.2:
目标 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(如果你只想去改变导入的话)。