将fstrings回滚到python<3.6
项目描述
future-fstrings
将fstrings回滚到python<3.6。
安装
pip install future-fstrings
用法
在文件顶部包含以下编码cookie(如果已经存在utf-8 cookie,则替换它)
# -*- coding: future_fstrings -*-
然后像往常一样编写python3.6 fstring代码!
# -*- coding: future_fstrings -*-
thing = 'world'
print(f'hello {thing}')
$ python2.7 main.py
hello world
显示转换后的源代码
future-fstrings
还包括一个cli来显示转换后的源代码。
$ future-fstrings-show main.py
# -*- coding: future_fstrings -*-
thing = 'world'
print('hello {}'.format((thing)))
转换micropython源代码
可以使用future-fstrings-show
命令在分发之前转换源代码。这可以让你编写f-string代码,但目标是那些不支持f-strings的平台,例如micropython。
要在现代版本的python上使用它,请使用以下命令安装
pip install future-fstrings[rewrite]
然后使用如上所示的future-fstrings-show
。
例如
future-fstrings-show code.py > code_rewritten.py
这是如何工作的?
future-fstrings
有两个部分
- A utf-8兼容的
codec
,执行源操作codec
首先使用UTF-8 codec解码源字节codec
然后利用tokenize-rt重写f-strings。
- 一个
.pth
文件,在解释器启动时注册codec。