Finding and creating gambiarras with python ASTs
summary
PyASTrX finds messy code by converting Python ASTs to XML and letting XPath match patterns, so it detects issues like mutable defaults and recursion-in-class definitions and highlights exact locations. It demonstrates a metaprogramming workflow using ast.NodeTransformer to replace validate_data calls in sklearn pipelines, avoiding repeated validation during grid search and saving CPU. The tool also supports writing compact XPath rules and transforming AST back to source for automated fixes.
AI-written summary · about
chapters (19)
- 0:00Introduction to talk
- 1:06What is a gambiarra
- 2:34Language limits in code
- 4:46Meta programming basics
- 7:20Russian literature and code
- 10:13Code style guide flaws
- 13:30Bias Tweaks philosophy
- 15:17Part one overview
- 16:19Python compilation process
- 17:21Abstract syntax trees explained
- 20:54Fixing code with ASTs
- 23:49Transformer limitations
- 25:14Should Python be serious
- 25:59Part two overview
- 26:42Finding patterns with XPath
- 29:46AST to XML conversion
- 31:51Normalizing ASTs
- 32:36Future of code analysis
- 33:18Closing remarks
related talks
32mCode as Data_ A Practical Introduction to Python’s Abstract Syntax Tree
17mUnlocking Python's AST: The Metaprogramming Superpower You Didn't Know You Had
36mHacking the Python AST
24mThe AST and Me
30mDoing stupid things with ASTs
28mSafely eval Python syntax using the AST module
31mSupercharge your Python library using AST parsing
30mAdvanced parsing of structured data using Python's new match statement
