Process, Analyze, and Transform Python Code with ASTs
Stefanie Molin
Transcription
This tutorial will be a roughly 50/50 split of lecture and exercises. Attendees will get hands-on experience working with ASTs in Python, using only the standard library. By recreating common code-quality checks from scratch, attendees will both learn how common tools work under the hood and how to work with the AST in an easy-to-understand fashion.
Topics covered:
- Introduction to the term and concept of Abstract Syntax Trees (ASTs)
- Some of the ways ASTs are used by Python itself and by popular tools
- Parsing code into an AST and inspecting it
- Walking the tree: ast.iter_fields(), ast.iter_child_nodes(), ast.walk()
- Modifying the code before running it
- Converting an AST into source code again with ast.unparse() and its caveats
- Finding missing docstrings
- ast.NodeVisitor and ast.NodeTransformer
- generic_visit() method — what it does and why we need it using animation
- 4 exercise breaks spread throughout accounting for ~45 minutes