Astunparse: A Python Library for Unparsing AST Back to Code
The astunparse library is a powerful Python module that helps developers convert Python abstract syntax trees (AST) back into Python source code strings. It is particularly useful for those working on code analysis, transformation, or dynamic code generation. In this article, we’ll explore the features of the astunparse
library through examples of its APIs and demonstrate its application within a real-world project.
What Is The astunparse
Library?
In Python, an abstract syntax tree (AST) is an intermediate representation of source code. The ast
module allows you to parse Python code into an AST, but to convert AST back into readable Python code, you can use astunparse
. It’s a must-have library for tasks like program analysis, code transformation, and metaprogramming in Python.
Installation
To install the library, you can use pip
:
pip install astunparse
Core Astunparse APIs With Examples
1. unparse
: Converting AST Back to Source Code
The unparse
method takes an AST node and generates human-readable Python source code from it:
import ast import astunparse source_code = """ a = 10 b = 20 c = a + b """ # Parse source code into AST parsed_ast = ast.parse(source_code) # Convert the AST back into source code generated_code = astunparse.unparse(parsed_ast) print(generated_code)
2. dump
: Debugging ASTs
The dump
function helps you inspect the structure of an AST:
# Dump the AST structure ast_dump = astunparse.dump(parsed_ast) print(ast_dump)
3. Formatting Options
You can customize the formatting of the output code using options in the Printer
class:
from astunparse import Printer class CustomPrinter(Printer): def format_Str(self, tree): return f'"{tree.s.upper()}"' modified_printer = CustomPrinter() formatted_code = modified_printer.visit(parsed_ast) print(formatted_code)
4. Combining With AST Transformations
You can manipulate ASTs and use astunparse
to generate updated source code:
# Create an AST transformation to add 1 to all numbers class AddOneTransformer(ast.NodeTransformer): def visit_Num(self, node): return ast.Constant(value=node.n + 1) transformer = AddOneTransformer() transformed_ast = transformer.visit(parsed_ast) updated_code = astunparse.unparse(transformed_ast) print(updated_code)
Building an Application With Astunparse
Here’s an example of an application that automatically formats and updates numerical values in Python scripts.
App Example: Code Formatter and Transformer
import ast import astunparse class CodeFormatter: def __init__(self, source_code): self.parsed_ast = ast.parse(source_code) def transform_numbers(self, transform_fn): class NumberTransformer(ast.NodeTransformer): def visit_Num(self, node): return ast.Constant(value=transform_fn(node.n)) transformer = NumberTransformer() self.parsed_ast = transformer.visit(self.parsed_ast) def save_transformed_code(self, file_path): with open(file_path, "w") as f: f.write(astunparse.unparse(self.parsed_ast)) # Original source code source_code = """ x = 5 y = 10 result = x * y """ formatter = CodeFormatter(source_code) formatter.transform_numbers(lambda n: n * 2) # Double all numbers formatter.save_transformed_code("output.py")
This script takes Python code, applies transformations on all numbers (doubling them in this case), and saves the updated code into a file.
Final Thoughts
The astunparse
library is an indispensable tool for developers dealing with AST manipulation and code generation. By enabling the conversion of abstract syntax trees back to readable Python source code, it bridges the gap between program analysis and program synthesis effortlessly.