| """ | |
| Python Code Generating Wizard | |
| Deterministic, extensible, Codex-compatible | |
| """ | |
| from typing import List | |
| class CodeWizard: | |
| def __init__(self): | |
| self.spec = {} | |
| def ask(self, prompt: str, options: List[str] | None = None) -> str: | |
| print("\n" + prompt) | |
| if options: | |
| for i, opt in enumerate(options, 1): | |
| print(f" {i}. {opt}") | |
| choice = int(input("Select option: ")) - 1 | |
| return options[choice] | |
| return input("> ") | |
| def collect_intent(self): | |
| self.spec["program_type"] = self.ask( | |
| "What type of program do you want?", | |
| ["function", "class", "script"] | |
| ) | |
| self.spec["task"] = self.ask( | |
| "Describe what the program should do (one sentence):" | |
| ) | |
| self.spec["inputs"] = self.ask( | |
| "What inputs does it take? (comma separated, or 'none')" | |
| ) | |
| self.spec["outputs"] = self.ask( | |
| "What should it return or produce?" | |
| ) | |
| self.spec["logic"] = self.ask( | |
| "Select a logic pattern:", | |
| ["calculation", "iteration", "condition", "data processing"] | |
| ) | |
| def generate_code(self) -> str: | |
| name = "generated_function" | |
| inputs = self.spec["inputs"] | |
| params = "" if inputs == "none" else inputs | |
| body = " pass\n" | |
| if self.spec["logic"] == "calculation": | |
| body = " result = 0\n return result\n" | |
| elif self.spec["logic"] == "iteration": | |
| body = " for item in []:\n pass\n" | |
| elif self.spec["logic"] == "condition": | |
| body = " if True:\n pass\n" | |
| elif self.spec["logic"] == "data processing": | |
| body = " data = []\n return data\n" | |
| if self.spec["program_type"] == "function": | |
| return f"""def {name}({params}): | |
| \"\"\"{self.spec['task']}\"\"\" | |
| {body}""" | |
| if self.spec["program_type"] == "class": | |
| return f"""class GeneratedClass: | |
| \"\"\"{self.spec['task']}\"\"\" | |
| def __init__(self): | |
| pass | |
| """ | |
| return f"""# {self.spec['task']} | |
| def main(): | |
| {body} | |
| if __name__ == "__main__": | |
| main() | |
| """ | |
| def run(self): | |
| print("🧙 Python Code Generating Wizard") | |
| self.collect_intent() | |
| code = self.generate_code() | |
| print("\n=== GENERATED CODE ===\n") | |
| print(code) | |
| return code | |
| if __name__ == "__main__": | |
| CodeWizard().run() |