跳转到主要内容

将fstrings回滚到python<3.6

项目描述

Build Status Azure DevOps coverage

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有两个部分

  1. A utf-8兼容的codec,执行源操作
    • codec首先使用UTF-8 codec解码源字节
    • codec然后利用tokenize-rt重写f-strings。
  2. 一个.pth文件,在解释器启动时注册codec。

你可能还喜欢

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源代码分发

future_fstrings-1.2.0.tar.gz (5.8 kB 查看哈希值)

上传时间 源代码

构建分发

future_fstrings-1.2.0-py2.py3-none-any.whl (6.1 kB 查看哈希值)

上传时间 Python 2 Python 3

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面