Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ast-refactor-0.1.0.tar.gz (7.5 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page