Skip to main content

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 .

Metadata

Release files for ast-refactor 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ast-refactor 0.1.0
File Size Uploaded
ast-refactor-0.1.0.tar.gz 7.5 kB Details

Release files / ast-refactor-0.1.0.tar.gz

Download URL ast-refactor-0.1.0.tar.gz
Size 7.5 kB
Tags Source
SHA-256 checksum
How to use checksums
729722b83e9af55a7b0b99f8f01abd4357b5ec5842cbe615d74ce32326cfa301
BLAKE2b-256 checksum
How to use checksums
0203a5b219d1b248d409da3cb914a3d71c7a75196f0aabc82f36fac50ee0b411
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.1.0 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page