def visit_Subscript(self, node: astc.Subscript): if valid_unq_sub.match(node): code = compile(astc.unparse(node.slice.value), self.filename, "eval") v = eval(code, self.global_dict, self.local_dict) assert isinstance(v, Expr) return getattr(v, '_repr') return self.generic_visit(node)
def __str__(self): return astc.unparse(self._repr)
def __repr__(self): if isinstance(self.string, ast.expr): return astc.unparse(self.string) return self.string
from writepy import * import ast import ast_compat as astc import typing def f(base: str): seq: typing.List = [] for each in range(5): lhs = expr_as_is(int, base + str(each)) rhs = expr_from_repr(each) call = Q[base + 1](lhs, rhs) with CG >> seq: UNQ[call] UNQ[lhs] = (UNQ[rhs], UNQ[expr_from_repr(base)]) return seq mk_cg(f) seq_ = f("base") print(astc.unparse(ast.Module(seq_)))
def mk_cg(f): nd = ast.parse(getsource(f)) nd2 = CodeTransform().visit(nd) exec(astc.unparse(nd2), f.__globals__)