tessl install tessl/pypi-wtfpython@3.0.0Educational collection of surprising Python code snippets that demonstrate counter-intuitive behaviors and language internals
Agent Success
Agent success rate when using this tile
93%
Improvement
Agent success rate improvement when using this tile compared to baseline
1.06x
Baseline
Agent success rate without this tile
88%
Create a tool that analyzes Python code snippets to determine which compile-time optimizations are applied and produces a detailed report of the bytecode transformations.
Your tool should analyze Python code and report on the following optimization behaviors:
Build a function that determines whether a given Python expression will be optimized through constant folding at compile-time. The function should:
Create a function that analyzes the bytecode of Python code snippets and extracts:
Implement functionality that compares different versions of semantically equivalent code to demonstrate optimization differences. The report should show:
"hello" * 3, the analyzer detects constant folding and identifies the optimized value as "hellohellohello" @test24 * 60 * 60, the analyzer detects constant folding and identifies the optimized value as 86400 @testx * 3 (where x is a variable), the analyzer reports that constant folding does NOT occur @test(1, 2, 3, 4, 5), the bytecode analysis shows a single LOAD_CONST instruction for the entire tuple @test@generates
def check_constant_folding(code_snippet: str) -> dict:
"""
Analyzes whether constant folding occurs for the given code snippet.
Args:
code_snippet: A Python expression as a string (e.g., "2 + 3")
Returns:
A dictionary with keys:
- 'folded': bool indicating if constant folding occurred
- 'value': the optimized constant value if folding occurred, None otherwise
- 'instruction_count': number of bytecode instructions generated
"""
pass
def analyze_bytecode(code_snippet: str) -> dict:
"""
Analyzes the bytecode instructions for the given code snippet.
Args:
code_snippet: Python code as a string
Returns:
A dictionary with keys:
- 'instructions': list of tuples (opname, argument) for each instruction
- 'has_load_const': bool indicating if LOAD_CONST instructions are present
- 'constant_values': list of constant values loaded
"""
pass
def compare_optimizations(code_variants: list[str]) -> dict:
"""
Compares bytecode optimization across multiple code variants.
Args:
code_variants: list of Python code snippets to compare
Returns:
A dictionary with keys:
- 'variants': list of dicts, each containing 'code', 'instruction_count', and 'bytecode'
- 'most_optimized_index': index of the most optimized variant
"""
passThe Python dis module for disassembling bytecode and inspecting optimization behavior.
@satisfied-by