def visit_Assert(self, ast, *a, **k): test = (ExpresionOut.visit(ast.test, *a, **k),) msg = (ExpresionOut.visit(ast.msg, *a, **k),) if ast.msg else () head = "assert %s" head %= ", ".join(chain(test, msg)) head = [head] return head
def visit_ExceptHandler(self, ast, *a, **k): type = (ExpresionOut.visit(ast.type, *a, **k),) if ast.type else () name = (ExpresionOut.visit(ast.name, *a, **k),) if ast.name else () head = "except %s:" head %= ", ".join(chain(type, name)) head = [head] body = StatementOut.handle_body(ast.body, *a, **k) return head + body
def visit_arguments(self, ast, *a, **k): args = [ExpresionOut.visit(arg, *a, **k) for arg in ast.args] defaults = (ExpresionOut.visit(default, *a, **k) for default in ast.defaults) defaults = ("%s=%s"% t for t in zip(args[-len(ast.defaults):],defaults)) args = args[:-len(ast.defaults)] vararg = ("*%s" % ast.vararg,) if ast.vararg else () kwarg = ("**%s" % ast.kwarg,) if ast.kwarg else () return ", ".join(chain(args, defaults, vararg, kwarg))
def visit_Raise(self, ast, *a, **k): type = (ExpresionOut.visit(ast.type, *a, **k),) if ast.type else () inst = (ExpresionOut.visit(ast.inst, *a, **k),) if ast.inst else () tback = (ExpresionOut.visit(ast.tback, *a, **k),) if ast.tback else () head = "raise %s" head %= ", ".join(chain(type, inst, tback)) head = [head] return head
def visit_Print (self, ast, *a, **k): values = (ExpresionOut.visit(value, *a, **k) for value in ast.values) dest = (ExpresionOut.visit(ast.dest, *a, **k), ) if ast.dest else () stmt = "print >> %s" if dest else "print %s" stmt %= ", ".join(chain(dest, values)) if ast.nl: return [stmt] else: return [stmt+","]
def visit_For (self, ast, *a, **k): target = ExpresionOut.visit(ast.target, *a, **k) iter = ExpresionOut.visit(ast.iter, *a ,**k) head = "for %s in %s:" head %= (target, iter) head = [head] body = self.handle_body(ast.body, *a, **k) if not ast.orelse: return head + body else: orelse = self.handle_body(ast.orelse, *a, **k) return head + body + ["else:"] + orelse
def visit_With(self, ast, *a, **k): context_expr = ExpresionOut.visit(ast.context_expr, *a, **k) if ast.optional_vars: optional_vars = ExpresionOut.visit(ast.context_expr, *a, **k) head = "with %s as %s:" head %= (context_expr, optional_vars) else: head = "with %s:" head %= context_expr head = [head] body = self.handle_body(ast.body) return head + body
def visit_While(self, ast, *a, **k): test = ExpresionOut.visit(ast.test, *a, **k) head = "while %s:" head %= test head = [head] body = self.handle_body(ast.body, *a, **k) if not ast.orelse: return head + body else: orelse = self.handle_body(ast.orelse, *a, **k) return head + body + ["else:"] + orelse
def visit_Exec(self, ast, *a, **k): if ast.globals: body = ExpresionOut.visit(ast.body, *a, **k) locals = ExpresionOut.visit(ast.locals, *a, **k) globals = ExpresionOut.visit(ast.globals, *a, **k) head = "exec %s in %s, %s" head %= (body, globals, locals) head = [head] elif ast.locals: body = ExpresionOut.visit(ast.body, *a, **k) locals = ExpresionOut.visit(ast.locals, *a, **k) head = "exec %s in %s" head %= (body, locals) head = [head] else: body = ExpresionOut.visit(ast.body, *a, **k) head = "exec %s" head %= body head = [head] return head
def visit_Slice (self, ast, *a, **k): from expr import ExpresionOut if ast.lower and ast.upper and ast.step: return "%s:%s:%s" % (ExpresionOut.visit(ast.lower), ExpresionOut.visit(ast.upper), ExpresionOut.visit(ast.step)) elif ast.lower and ast.upper: return "%s:%s" % (ExpresionOut.visit(ast.lower), ExpresionOut.visit(ast.upper)) elif ast.lower and ast.step: return "%s::%s" % (ExpresionOut.visit(ast.lower), ExpresionOut.visit(ast.step)) elif ast.upper and ast.step: return ":%s:%s" % (ExpresionOut.visit(ast.upper), ExpresionOut.visit(ast.step)) elif ast.lower: return "%s:" % ExpresionOut.visit(ast.lower) elif ast.upper: return ":%s" % ExpresionOut.visit(ast.upper) elif ast.step: return "::%s" % ExpresionOut.visit(ast.step) else: return ":"
def visit_Index (self, ast, *a, **k): from expr import ExpresionOut return ExpresionOut.visit(ast.value)
def visit_AugAssign (self, ast, *a, **k): target = ExpresionOut.visit(ast.target, *a, **k) op = OperatorOut.visit(ast.op, *a, **k) value = ExpresionOut.visit(ast.value, *a, **k) return ["%s %s= %s" % (target, op, value)]
def visit_Assign (self, ast, *a, **k): targets = ", ".join(ExpresionOut.visit(target, *a, **k) for target in ast.targets) value = ExpresionOut.visit(ast.value, *a, **k) return ["%s = %s" %(targets, value)]
def visit_Delete (self, ast, *a, **k): return ["del %s" % ", ".join(ExpresionOut.visit(target, *a, **k) for target in ast.targets)]
def visit_Return (self, ast, *a, **k): if ast.value: return ["return %s" % ExpresionOut.visit(ast.value, *a, **k)] else: return ["return"]
def visit_Expr(self, ast, *a, **k): return [ExpresionOut.visit(ast.value, *a, **k)]