The AST and Me

Emily Morehouse

Co-Founder, Director of Engineering
@ Cuttlesoft

We're going to cover:

  • Life cycle of a piece of Python code

We're going to cover:

  • Life cycle of a piece of Python code
  • Interacting with your code at various stages

We're going to cover:

  • Life cycle of a piece of Python code
  • Interacting with your code at various stages
  • Current Python optimizations

We're going to cover:

  • Life cycle of a piece of Python code
  • Interacting with your code at various stages
  • Current Python optimizations
  • Practical applications
Should you care about language internals?
Should you care about language internals?

A Peek Under The Hood

From Source Code To Execution:

From Source Code To Execution:

Q: Interpreted or compiled?

From Source Code To Execution:

A: Both!

Compiler → Generates Bytecode

Compiler → Generates Bytecode

Interpreter → Executes Bytecode

Life Cycle of a Piece of Python Code

Life cycle of a Piece of Python Code
Life cycle of a Piece of Python Code
Life cycle of a Piece of Python Code
Life cycle of a Piece of Python Code
Life cycle of a Piece of Python Code
Life cycle of a Piece of Python Code
Life cycle of a Piece of Python Code
Life cycle of a Piece of Python Code
Life cycle of a Piece of Python Code
Life cycle of a Piece of Python Code
Life cycle of a Piece of Python Code
Life cycle of a Piece of Python Code

What is an Abstract Syntax Tree?

What is an Abstract Syntax Tree?

Tree representing the structure of your source code.

What is... a tree?

What is... a tree?
What is... a tree?
What is... a tree?
What is... a tree?
What is... a tree?
What is... a tree?
What is... a tree?
What is... a tree?
What is... a tree?
What is... a tree?
What is... a tree?
What is... a tree?
Parse Tree vs AST
(1 + 2) * 3
Parse Tree vs AST
(1 + 2) * 3
Parse Tree vs AST
(1 + 2) * 3

Let's dig in.

Primary Tools:

Primary Tools:
  • ast module
Primary Tools:
  • ast module
  • dis module

Secondary Libraries:

Secondary Libraries:
  • astor
Secondary Libraries:
  • astor
  • meta
Secondary Libraries:
  • astor
  • meta
  • codegen

Code Objects:

Code objects:
  • Contains instructions and information needed to run the code.
Code objects:
  • Contains instructions and information needed to run the code.
  • Internal representation of a piece of Python code.
Code objects:
  • co_name
Code objects:
  • co_name
  • co_varnames
Code objects:
  • co_name
  • co_varnames
  • co_stacksize
Code objects:
  • co_name
  • co_varnames
  • co_stacksize
  • co_consts
Code objects:
  • co_name
  • co_varnames
  • co_stacksize
  • co_consts
  • co_argcount
Code objects:
  • co_name
  • co_varnames
  • co_stacksize
  • co_consts
  • co_argcount
  • co_code
print("may the force be with you")
if a == 23:
    print("may the force be with you")
a = 32
if a == 23:
    print("may the force be with you")

Current Optimizations

Current Optimizations
  • Python's compiler is purposefully simple
Current Optimizations
  • Python's compiler is purposefully simple
  • Peephole Optimizer
Current Optimizations
  • Python's compiler is purposefully simple
  • Peephole Optimizer
  • Few AST optimizations besides constant folding

Peephole Optimizations

Peephole Optimizations

Looking around without moving your head.

Peephole Optimizations
x = 1
y = x + 2
Peephole Optimizations
y = 1 + 2
Peephole Optimizations

Constant Folding

Constant Folding

Evaluating constant expressions at compile time.

a = 2 * 3
a = 6

How The AST Affects Your Code

How The AST Affects Your Code
  • Bytecode is created using an AST
How The AST Affects Your Code
  • Bytecode is created using an AST
  • Bytecode is stored in *.pyc files
How The AST Affects Your Code
  • Bytecode is created using an AST
  • Bytecode is stored in *.pyc files
  • VERY micro speed-ups

Example Applications / Projects

Example Applications / Projects:
  • Code generators
Example Applications / Projects:
  • Code generators
    • Hypothesis (test generation)
Example Applications / Projects:
  • Code generators
    • Hypothesis (test generation)
    • Cog (file/text generation)
Example Applications / Projects:
  • Code generators
    • Hypothesis (test generation)
    • Cog (file/text generation)
  • Alternative interpreters
Example Applications / Projects:
  • Code generators
    • Hypothesis (test generation)
    • Cog (file/text generation)
  • Alternative interpreters
  • Transpiling / Alternative Compilation
Example Applications / Projects:
  • Code generators
    • Hypothesis (test generation)
    • Cog (file/text generation)
  • Alternative interpreters
  • Transpiling / Alternative Compilation
    • VOC (PyBee)
Example Applications / Projects:
  • Code generators
    • Hypothesis (test generation)
    • Cog (file/text generation)
  • Alternative interpreters
  • Transpiling / Alternative Compilation
    • VOC (PyBee)
    • Transcrypt (Python to JS)
Example Applications / Projects:
  • Code generators
    • Hypothesis (test generation)
    • Cog (file/text generation)
  • Alternative interpreters
  • Transpiling / Alternative Compilation
    • VOC (PyBee)
    • Transcrypt (Python to JS)
  • Code transformations
Example Applications / Projects:
  • Code generators
    • Hypothesis (test generation)
    • Cog (file/text generation)
  • Alternative interpreters
  • Transpiling / Alternative Compilation
    • VOC (PyBee)
    • Transcrypt (Python to JS)
  • Code transformations
    • Jinja (templating engine)
Example Applications / Projects:
Example Applications / Projects:
  • Code analysis
Example Applications / Projects:
  • Code analysis
    • Python Security's PyT
Example Applications / Projects:
  • Code analysis
    • Python Security's PyT
  • Code linting
Example Applications / Projects:
  • Code analysis
    • Python Security's PyT
  • Code linting
  • Code formatters
Example Applications / Projects:
  • Code analysis
    • Python Security's PyT
  • Code linting
  • Code formatters
    • YAPF
Example Applications / Projects:
  • Code analysis
    • Python Security's PyT
  • Code linting
  • Code formatters
    • YAPF
    • Black
Example Applications / Projects:
  • Code analysis
    • Python Security's PyT
  • Code linting
  • Code formatters
    • YAPF
    • Black
    • Prettier
Example Applications / Projects:
  • Code analysis
    • Python Security's PyT
  • Code linting
  • Code formatters
    • YAPF
    • Black
    • Prettier
  • Code transformations
Example Applications / Projects:
  • Code analysis
    • Python Security's PyT
  • Code linting
  • Code formatters
    • YAPF
    • Black
    • Prettier
  • Code transformations
    • Post-CSS
Example Applications / Projects:
  • Code analysis
    • Python Security's PyT
  • Code linting
  • Code formatters
    • YAPF
    • Black
    • Prettier
  • Code transformations
    • Post-CSS
    • Babel (ESNext -> ESNow)
Example Applications / Projects:
  • Code analysis
    • Python Security's PyT
  • Code linting
  • Code formatters
    • YAPF
    • Black
    • Prettier
  • Code transformations
    • Post-CSS
    • Babel (ESNext -> ESNow)
    • Migrations (e.g. Python 2 -> 3)
Example Applications / Projects:
  • Code analysis
    • Python Security's PyT
  • Code linting
  • Code formatters
    • YAPF
    • Black
    • Prettier
  • Code transformations
    • Post-CSS
    • Babel (ESNext -> ESNow)
    • Migrations (e.g. Python 2 -> 3)
    • React Native
Practical Applications:
Practical Applications:
  • Improve and speed up your code
Practical Applications:
  • Improve and speed up your code (🤷🏻‍♀️)
Practical Applications:
  • Improve and speed up your code (🤷🏻‍♀️)
  • Debug errors/oddities
Practical Applications:
  • Improve and speed up your code (🤷🏻‍♀️)
  • Debug errors/oddities
  • Change the Python grammar
Practical Applications:
  • Improve and speed up your code (🤷🏻‍♀️)
  • Debug errors/oddities
  • Change the Python grammar
  • Round-Tripping

Round-Tripping:

Round-tripping:

Comparing the AST from your code before and after making changes.

Go forth and build awesome tools!

Resources

The end!