File size: 3,609 Bytes
a03bf1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff9e41b
a03bf1f
 
 
 
ff9e41b
a03bf1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff9e41b
a03bf1f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import os
import importlib
import ast


def discover_templates(root_dir="data/templates/branches"):
    """
    Recursively scans the directory structure under `root_dir`
    to find all Python files that define functions starting with 'template_'.

    Works with nested folders (e.g., branches/chemical_engineering/reaction_kinetics/...).

    Returns:
        dict: {
            "chemical_engineering": {
                "chemical_engineering/reaction_kinetics/mole_balances.py": ["template_mole_balance", ...],
                ...
            },
            "transport_phenomena": {...},
        }
    """
    discovered = {}

    if not os.path.isdir(root_dir):
        print(f"Error: Directory '{root_dir}' not found.")
        return discovered

    # Iterate through top-level domain directories
    for domain in sorted(os.listdir(root_dir)):
        domain_path = os.path.join(root_dir, domain)
        if os.path.isdir(domain_path):
            domain_templates = {}

            # Recursively walk through subdirectories
            for dirpath, _, filenames in os.walk(domain_path):
                for filename in sorted(filenames):
                    if filename.endswith(".py"):
                        file_path = os.path.join(dirpath, filename)
                        try:
                            with open(file_path, "r", encoding="utf-8") as f:
                                file_content = f.read()
                            tree = ast.parse(file_content)

                            # Collect template function names
                            template_functions = [
                                node.name
                                for node in ast.walk(tree)
                                if isinstance(node, ast.FunctionDef)
                                and node.name.startswith("template_")
                            ]

                            if template_functions:
                                # store relative path (used later for import)
                                rel_path = os.path.relpath(file_path, root_dir)
                                domain_templates[rel_path] = sorted(template_functions)
                        except Exception as e:
                            print(f"Error parsing {file_path}: {e}")

            if domain_templates:
                discovered[domain] = domain_templates

    return discovered

def generate_examples(branch, domain, filename, template_name):
    """
    Dynamically imports and runs a selected template function multiple times
    to generate a list of {question, solution} objects.
    """
    if not all([branch, domain, filename, template_name]):
        print("Missing arguments to generate_examples()")
        return []

    try:
        # Clean filename for import path
        filename_no_ext = filename.replace(".py", "").replace(os.sep, ".")
        module_path = f"data.templates.branches.{filename_no_ext}"

        module = importlib.import_module(module_path)
        template_function = getattr(module, template_name)

        examples = []
        for _ in range(10):
            result = template_function()
            if isinstance(result, tuple) and len(result) == 2:
                question, solution = result
                examples.append({"question": question, "solution": solution})
            else:
                print(f"Warning: {template_name} did not return (question, solution) tuple.")
        return examples

    except Exception as e:
        print(f"Error running template '{template_name}' from {branch}/{domain}/{filename}: {e}")
        return []