No project description provided
Project description
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)
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fun_with_ast-0.2.118.tar.gz.
File metadata
- Download URL: fun_with_ast-0.2.118.tar.gz
- Upload date:
- Size: 48.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.5 CPython/3.10.15 Linux/6.5.0-1025-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
024ca782a2e3e475b4510ca5fa7e1aff2de350ae8485f97874a0a3da040013b2
|
|
| MD5 |
477eb63108ce4ce51e006d0ac8460317
|
|
| BLAKE2b-256 |
d91f2b7bf5cd38ba6b94be801d87339f838330ffa4c1c731766bf5e61b276cc4
|
File details
Details for the file fun_with_ast-0.2.118-py3-none-any.whl.
File metadata
- Download URL: fun_with_ast-0.2.118-py3-none-any.whl
- Upload date:
- Size: 64.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.5 CPython/3.10.15 Linux/6.5.0-1025-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
136480fd15a7f539047b989a24198c76fc98be9494f548b33b19a67276dd8d17
|
|
| MD5 |
e0252f63f2b3fb00644dac09194f38c6
|
|
| BLAKE2b-256 |
ef59981cfe557f7cfedeab5b6d9a85d9290456c6c50f65ae3c28038e4667716b
|