def visit_Name(self, node): """Replace bound vars with AST subtrees registered by quote_bind""" if node.id in self.bound_vars: name = self.bound_vars[node.id] from metacontext.template import rhs, lhs if isinstance(name, ast.Call) and isinstance(name.func, ast.Name) and name.func.id == 'rhs': return rhs(self.evaluate(name.args[0])) if isinstance(name, ast.Call) and isinstance(name.func, ast.Name) and name.func.id == 'lhs': return lhs(self.evaluate(name.args[0])) return self.evaluate(name) return node
def template(self, translator, body, args, var): logging = [k.value for k in args.keywords if k.arg == 'log'] if logging: logging = logging[0] with quote() as log: with unquote_bind(logging, rhs(var)) as (_logging, res): if _logging: print "Took", res else: log = [] time_module = ast.Name(translator.gensym(), ast.Load()) with quote() as q: with unquote_bind(var, time_module) as (res, _time): import time as _time _start = _time.time() unquote_stmts(body) res = _time.time() - _start unquote_stmts(log) return q