Fun with AST
The Fun with AST library is driven by the aspiration to enhance the productivity and quality of a software engineer's work. Its main goal is to enable developers to focus on implementing the core business values needed to be achieved, rather than wasting time on repetitive and routine tasks that can be done automatically.
A Hybrid Programming Model with Source-to-Source Transformations
We envision a hybrid programming model that combines human and machine elements. Figure 1 visualizes the concept of hybrid programming.
(1) A developer writes the business code. Then, the developer also provides instructions (e.g., declarative configuration) regarding the nature of code they want to add consistently and repetitively. The Fun with AST engine (3) automatically adds the required source code to the original source code, creating code that includes both the business value and the additional code for the repetitive task (4).
**Figure 1: A hybrid programming model**
Example of repetitive, trivial or automated tasks
When thinking of software engineering tasks, we can split them into two groups. The first group requires the engineer to implement specific and typically innovative capabilities. Examples might include developing a new computer game or creating a new algorithm for financial trading. In the second group we find, tasks that are common to every software, regardless of the business domain. We provide some examples of such common tasks below.
-
Removing common code from
if/elseblocks is a common optimization to increase code readability (link). -
Other
if/elseoptimizations for readability and simplicity are suggested here. For example, reducing redundancy of code in nested if statements." -
Optimizing while loops into collection-based operations is suggested in this article.
-
Here is a more challenging problem, which represents the next level of control flow optimization: automatically convert a non-tail recursion into a tail recursion. While a few compilers do that, engineers are typically not aware of this possibility. Having a tool for source-to-source transformations can help engineers understand this optimization and use it safely, without introducing errors.
-
Refactor long functions
-
testing - complete asserts , creating mocks for all dependencies
While we're not there yet, the fun-with-ast library enables easier code analysis and transformation to identify when such optimizations are possible.
Challenges of Source-to-Source Transformations
Source-to-source transformations aim to produce live code that engineers can continue working with after the transformation is performed. Such transformations must be executed without breaking the code and should preserve all original source-code characteristics, like indentation, comments, spacing, and readability based on standards like PEP8.
Source-to-Source transformation example: AST Parse vs. AST Unparse
Python allows you to perform source-to-source transformations using the AST module. You can parse source code into an Abstract Syntax Tree (AST), apply necessary modifications based on the desired transformation, and then unparse the tree back into source code. However, the unparsing process doesn't retain essential elements from the original code, including comments, spacing, indentation, quote types, line length, line breaks, and more. I'll provide some examples of these issues below.
Using the library
Why Fun-with-AST
- Here is a talk I gave in Pycon 2023. It explains the capabilities of fun-with-ast.
- It is a great learning personal development experience.
- Enables smart and complex manipulations
Potential usages:
- Fun #1: Keep source to source transformations
- Fun #2: switch
else/ifbodies - Fun #3: mutation testing switch
<into<= - Fun #4: Switch
FortoWhile - Fun #5: for loop into tail recursion.
- Fun #7: Add node AND comment.
How to Contribute
- Follow the steps in Contribute to projects.
- You can chose an existing open issue or open a new one.
- Start working ....
- Before submitting a pull request make sure tests are passing.
How to Run Tests
- In
fun-with-astwe use pytest. - Use your IDE to run all tests in
testsdirectory. - OR, use command line:
cd <your path to fun-with-ast fork>/fun_with_ast/testspytest --version, should be at least7.2.2- run
pytest - No tests should fail - some tests would be skipped / xfail.
Limitations
- The library is not yet mature.
- 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')andprint("fun-with-ast")only one of them will be preserved, the first one in the module/node. (See methodguess_default_quote_for_node, if you want to work on something interesting)
Release files for fun-with-ast 0.2.118
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| fun_with_ast-0.2.118.tar.gz | 48.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fun_with_ast-0.2.118-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 112.9 kB
Release files / fun_with_ast-0.2.118.tar.gz
| Download URL | fun_with_ast-0.2.118.tar.gz |
|---|---|
| Size | 48.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
024ca782a2e3e475b4510ca5fa7e1aff2de350ae8485f97874a0a3da040013b2
|
|
BLAKE2b-256 checksum How to use checksums |
d91f2b7bf5cd38ba6b94be801d87339f838330ffa4c1c731766bf5e61b276cc4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.5 CPython/3.10.15 Linux/6.5.0-1025-azure
|
Release files / fun_with_ast-0.2.118-py3-none-any.whl
| Download URL | fun_with_ast-0.2.118-py3-none-any.whl |
|---|---|
| Size | 64.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
136480fd15a7f539047b989a24198c76fc98be9494f548b33b19a67276dd8d17
|
|
BLAKE2b-256 checksum How to use checksums |
ef59981cfe557f7cfedeab5b6d9a85d9290456c6c50f65ae3c28038e4667716b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.5 CPython/3.10.15 Linux/6.5.0-1025-azure
|