shairubin / fun_with_ast

let's have fun manipulating ASTs in Python
Apache License 2.0
0 stars 1 forks source link

Fun with AST

Provides developers with a programmatic tool to change our own code. This repository contains a library to analyze and manipulate python code using Abstract Systax Tress manipulation.

Using the library

See the test-fun-with-ast project for examples of using the fun-with-ast library.

Why Fun-with-AST

  1. Here is a talk I gave in Pycon 2023. It explains the capabilities of fun-with-ast.
  2. It is a great learning personal development experience.
  3. Enables smart and complex manipulations

Examples: AST Parse vs. AST Unparse vs. fun-with-ast Source Code Preserver

The examples below show an original program that first was unparsed with python ast module, and then was unparsed using the fun-with-ast library. The actual code that generates these example can be found in the test-fun-with-ast library.

  1. Parse-Unparse Challenge Examples

Potential usages:

How to Contribute

  1. Follow the steps in Contribute to projects.
  2. You can chose an existing open issue or open a new one.
  3. Start working ....
  4. Before submitting a pull request make sure tests are passing.

How to Run Tests

  1. In fun-with-ast we use pytest.
  2. Use your IDE to run all tests in tests directory.
  3. OR, use command line:
    1. cd <your path to fun-with-ast fork>/fun_with_ast/tests
    2. pytest --version, should be at least 7.2.2
    3. run pytest
    4. No tests should fail - some tests would be skipped / xfail.

Limitations

  1. The library is not yet mature.
  2. Determining the the of quote (i.e., ' or ") for each string is done at the module or node level. If a node contains both print('fun-with-ast') and print("fun-with-ast") only one of them will be preserved, the first one in the module/node. (See method guess_default_quote_for_node, if you want to work on something interesting)