"""Runs example requests through Odyssey_PyPro_10M, prints the generated code, and executes it on a sample list. python examples.py """ import os import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from odyssey_pypro import REJECT, generate, load # noqa: E402 EXAMPLES = [ ('Write a function named "top" with parameter "xs". Keep numbers at least 3, then sort them from largest to smallest.', [5, 1, 3, 9, 2, 3]), ('Create a function called "clean" that takes a list "vals". Remove duplicate numbers, then return how many numbers there are.', [4, 4, 2, 7, 2]), ('Given a list called "data", write a function named "shift" that does this: Add -2 to each number.', [1, 2, 3]), ('Square each number, then return the sum of the numbers. Name the function "sq_sum" and its parameter "nums".', [1, -2, 3]), ('Using the input list "seq", implement the function "head3". Take the first 3 values.', [9, 8, 7, 6, 5]), ('Define "low", a function whose argument is "arr". Keep only values below 0, then find the minimum value.', [3, -4, 0, -1]), ('Write a function named "med" with parameter "xs". Compute the median of the numbers.', [1, 2, 3]), # unsupported ] if __name__ == "__main__": model = load() for text, sample in EXAMPLES: src = generate(model, text) print("REQUEST:", text) print(src, end="") if src != REJECT: ns = {} exec(src, ns) # the network's output is ordinary Python; run it only if you trust this example list fn = [v for k, v in ns.items() if callable(v) and not k.startswith("__")][0] print(f"# {fn.__name__}({sample}) -> {fn(list(sample))}") print()