or run

tessl search
Log in

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/wtfpython@3.0.x
tile.json

tessl/pypi-wtfpython

tessl install tessl/pypi-wtfpython@3.0.0

Educational 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%

task.mdevals/scenario-3/

Python Bytecode Optimization Analyzer

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.

Requirements

Your tool should analyze Python code and report on the following optimization behaviors:

Constant Folding Detection

Build a function that determines whether a given Python expression will be optimized through constant folding at compile-time. The function should:

  • Accept a Python code snippet as a string
  • Return whether constant folding occurs for that expression
  • Identify the optimized constant value if folding happens

Bytecode Instruction Analysis

Create a function that analyzes the bytecode of Python code snippets and extracts:

  • The sequence of bytecode instructions generated
  • The number of instructions before and after optimization
  • Specific optimization patterns (e.g., LOAD_CONST replacing multiple operations)

Optimization Comparison Report

Implement functionality that compares different versions of semantically equivalent code to demonstrate optimization differences. The report should show:

  • Side-by-side bytecode comparisons
  • Instruction count differences
  • Which version is more optimized and why

Test Cases

  • Given the expression "hello" * 3, the analyzer detects constant folding and identifies the optimized value as "hellohellohello" @test
  • Given the expression 24 * 60 * 60, the analyzer detects constant folding and identifies the optimized value as 86400 @test
  • Given the expression x * 3 (where x is a variable), the analyzer reports that constant folding does NOT occur @test
  • Given a code snippet with (1, 2, 3, 4, 5), the bytecode analysis shows a single LOAD_CONST instruction for the entire tuple @test

Implementation

@generates

API

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
    """
    pass

Dependencies { .dependencies }

dis { .dependency }

The Python dis module for disassembling bytecode and inspecting optimization behavior.

@satisfied-by