Transform code using the power of the Python AST
Project description
ast-refactor
Apply structured migrations to existing python source code easily.
This is intended to be used to migrate from older code patterns to modern supported ones.
For library authors this can assist in providing migration tools so that users can migrate to newer versions of your library more easily.
ast-refactor will modify your source code if you run it, so be sure to run it on a repository that doesn't have code that is not checked in.
Why?
When dealing with large codebases it is common to encounter code that is using libraries in ways that are deprecated (or removed) in newer versions of those libraries.
For example you have the following code that uses an older version of 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")
)
The second statement uses two pandas functions with deprecated keyword arguments. You can obviously fix this by hand, but if you have lots of code this is tedious and error prone.
ast-refactor
provides you the tools to automatically convert that second statement into something that will work for modern versions of pandas.
table = (pd
.pivot_table(
df,
index=['A', 'B'],
columns=['C'],
values='D',
aggfunc=np.sum)
.sort_values("large")
)
This is intended as a tool to help library author and owners of large codebases migrate source code easily.
Usage
For detailed usage documentation see usage docs and writing a migrator and
as a python cli
The easiest way to install this tool is using pipx.
$ pipx install \
ast-refactor
$ ast-refactor run some/path/or/file
as a docker container
Alternatively you can run it from a docker container
$ docker run \
-e UID=$(id -u) \
-e GID=$(id -u) \
-v /some/path/or/file:/work \
flatironhealth/ast-refactor \
run \
/work
Building
locally
$ pip install .
$ ast-refactor some/path/or/file
docker
docker build -t ast-refactor .
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file ast-refactor-0.1.0.tar.gz
.
File metadata
- Download URL: ast-refactor-0.1.0.tar.gz
- Upload date:
- Size: 7.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 729722b83e9af55a7b0b99f8f01abd4357b5ec5842cbe615d74ce32326cfa301 |
|
MD5 | 90b81e41932b466d4c15f3f0767376d3 |
|
BLAKE2b-256 | 0203a5b219d1b248d409da3cb914a3d71c7a75196f0aabc82f36fac50ee0b411 |