使用Python AST的强大功能转换代码
项目描述
ast-refactor
轻松应用结构化迁移到现有的Python源代码。
此软件旨在用于从较老的代码模式迁移到现代支持的代码模式。
对于库作者,这可以帮助提供迁移工具,以便用户可以更轻松地迁移到库的新版本。
ast-refactor 将修改您的源代码,如果您运行它,请确保在未提交代码的仓库上运行。
为什么?
处理大型代码库时,常见的情况是遇到使用已弃用(或已删除)的库的方式。
例如,以下代码使用了pandas的旧版本(<0.17)
df = pd.DataFrame({"A": ["foo", "foo", "foo", "foo", "foo",
"bar", "bar", "bar", "bar"],
"B": ["one", "one", "one", "two", "two",
"one", "one", "two", "two"],
"C": ["small", "large", "large", "small",
"small", "large", "small", "small",
"large"],
"D": [1, 2, 2, 3, 3, 4, 5, 6, 7],
"E": [2, 4, 5, 5, 6, 6, 8, 9, 9]})
table = (pd
.pivot_table(
df,
rows=['A', 'B'],
cols=['C'],
values='D',
aggfunc=np.sum)
.sort("large")
)
第二个语句使用了两个已弃用的关键字参数的pandas函数。您显然可以手动修复它,但如果您有很多代码,这将是一件乏味且容易出错的事情。
ast-refactor
为您提供将第二个语句自动转换为适用于现代pandas版本的工具。
table = (pd
.pivot_table(
df,
index=['A', 'B'],
columns=['C'],
values='D',
aggfunc=np.sum)
.sort_values("large")
)
这是一个旨在帮助库作者和大型代码库所有者轻松迁移源代码的工具。
使用方法
作为python cli
安装此工具最简单的方法是使用pipx。
$ pipx install \
ast-refactor
$ ast-refactor run some/path/or/file
作为docker容器
或者您可以从docker容器中运行它
$ docker run \
-e UID=$(id -u) \
-e GID=$(id -u) \
-v /some/path/or/file:/work \
flatironhealth/ast-refactor \
run \
/work
构建
本地
$ pip install .
$ ast-refactor some/path/or/file
docker
docker build -t ast-refactor .
项目详情
关闭
ast-refactor-0.1.0.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 729722b83e9af55a7b0b99f8f01abd4357b5ec5842cbe615d74ce32326cfa301 |
|
MD5 | 90b81e41932b466d4c15f3f0767376d3 |
|
BLAKE2b-256 | 0203a5b219d1b248d409da3cb914a3d71c7a75196f0aabc82f36fac50ee0b411 |