Spaces:
Sleeping
Sleeping
| import importlib | |
| def generate_examples(domain, filename, template_name): | |
| """ | |
| Dynamically imports and runs a selected template function 10 times | |
| to generate a list of {question, solution} objects. | |
| Args: | |
| domain (str): The name of the domain directory (e.g., 'corporate_finance'). | |
| filename (str): The name of the Python file (e.g., 'wacc.py'). | |
| template_name (str): The name of the function to run (e.g., 'template_wacc_basic'). | |
| Returns: | |
| list[dict]: A list of objects, each with 'question' and 'solution' keys. | |
| """ | |
| if not all([domain, filename, template_name]): | |
| return [] | |
| try: | |
| module_path = f"data.templates.{domain}.{filename.replace('.py', '')}" | |
| module = importlib.import_module(module_path) | |
| template_function = getattr(module, template_name) | |
| examples = [] | |
| for _ in range(10): | |
| question, solution = template_function() | |
| examples.append({ | |
| "question": question, | |
| "solution": solution | |
| }) | |
| return examples | |
| except ImportError: | |
| return [] | |
| except AttributeError: | |
| return [] | |
| except Exception: | |
| return [] | |