interplane.Get Omega
Read in your language

omega-bridge

Omega-first language interoperability: checked conversion backends and native language bindings

Brought by drakestapleton

branch main Branches Tags History

omega-bridge / evidence / deepinfer / select_candidates.py

43 lines · 2870 bytes Raw Blame History
1"""Inventory every Python function in a pinned DeepInfer tree and pick the ones whose shape
2could fall in an existing integer import profile (single `return <expr>` over parameters and
3integer literals, arithmetic/comparison/conditional only). Writes candidates as standalone
4sources for the real adapters; it decides nothing about acceptance itself."""
5import ast, json, sys
6from pathlib import Path
7
8root = Path(sys.argv[1]); out = Path(sys.argv[2]); out.mkdir(parents=True, exist_ok=True)
9ARITH = (ast.Add, ast.Sub, ast.Mult, ast.FloorDiv, ast.Mod, ast.LShift, ast.RShift,
10 ast.BitAnd, ast.BitOr, ast.BitXor, ast.Div, ast.Pow)
11
12def pure_int_expr(node, params):
13 if isinstance(node, ast.Name): return node.id in params
14 if isinstance(node, ast.Constant): return isinstance(node.value, int) and not isinstance(node.value, bool)
15 if isinstance(node, ast.BinOp): return isinstance(node.op, ARITH) and pure_int_expr(node.left, params) and pure_int_expr(node.right, params)
16 if isinstance(node, ast.UnaryOp): return isinstance(node.op, (ast.USub, ast.Invert, ast.UAdd)) and pure_int_expr(node.operand, params)
17 if isinstance(node, ast.Compare): return all(pure_int_expr(n, params) for n in [node.left, *node.comparators])
18 if isinstance(node, ast.IfExp): return all(pure_int_expr(n, params) for n in (node.test, node.body, node.orelse))
19 return False
20
21rows = []; total = 0; files = 0
22for f in sorted(root.rglob('*.py')):
23 rel = f.relative_to(root).as_posix(); files += 1
24 src = f.read_text(encoding='utf-8')
25 tree = ast.parse(src)
26 lines = src.splitlines()
27 for node in ast.walk(tree):
28 if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)): continue
29 total += 1
30 a = node.args
31 params = [p.arg for p in a.posonlyargs + a.args + a.kwonlyargs]
32 body = [s for s in node.body if not (isinstance(s, ast.Expr) and isinstance(s.value, ast.Constant) and isinstance(s.value.value, str))]
33 if not (len(body) == 1 and isinstance(body[0], ast.Return) and body[0].value is not None): continue
34 if a.vararg or a.kwarg or node.decorator_list or not params: continue
35 if not pure_int_expr(body[0].value, set(params)): continue
36 seg = '\n'.join(lines[node.lineno - 1: node.end_lineno])
37 name = f'{len(rows):03d}_{node.name}'
38 (out / f'{name}.py').write_text(ast.unparse(node) + '\n')
39 rows.append({'id': name, 'file': rel, 'line': node.lineno, 'name': node.name,
40 'params': params, 'annotated': any(p.annotation for p in a.args), 'source': seg})
41json.dump({'files': files, 'functions': total, 'candidates': rows}, open(out / 'candidates.json', 'w'), indent=1)
42print(f'files={files} functions={total} candidates={len(rows)}')
43for r in rows: print(f"{r['id']} {r['file']}:{r['line']} {r['source'].splitlines()[-1].strip()[:90]}")