Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _visit_BoolOperator(opcls):
def _visit(self, node):
values = [self.visit(n) for n in node.nodes]
return self._new(_ast.BoolOp, opcls(), values)
return _visit
visit_And = _visit_BoolOperator(_ast.And)
visit_Or = _visit_BoolOperator(_ast.Or)
del _visit_BoolOperator
cmp_operators = {
'==': _ast.Eq,
'!=': _ast.NotEq,
'<': _ast.Lt,
'<=': _ast.LtE,
'>': _ast.Gt,
'>=': _ast.GtE,
'is': _ast.Is,
'is not': _ast.IsNot,
'in': _ast.In,
'not in': _ast.NotIn,
}
def visit_Compare(self, node):
left = self.visit(node.expr)
ops = []
comparators = []
for optype, expr in node.ops:
ops.append(self.cmp_operators[optype]())
comparators.append(self.visit(expr))
return self._new(_ast.Compare, left, ops, comparators)
def visit_Lambda(self, node):
values = [self.visit(n) for n in node.nodes]
return self._new(_ast.BoolOp, opcls(), values)
return _visit
visit_And = _visit_BoolOperator(_ast.And)
visit_Or = _visit_BoolOperator(_ast.Or)
del _visit_BoolOperator
cmp_operators = {
'==': _ast.Eq,
'!=': _ast.NotEq,
'<': _ast.Lt,
'<=': _ast.LtE,
'>': _ast.Gt,
'>=': _ast.GtE,
'is': _ast.Is,
'is not': _ast.IsNot,
'in': _ast.In,
'not in': _ast.NotIn,
}
def visit_Compare(self, node):
left = self.visit(node.expr)
ops = []
comparators = []
for optype, expr in node.ops:
ops.append(self.cmp_operators[optype]())
comparators.append(self.visit(expr))
return self._new(_ast.Compare, left, ops, comparators)
def visit_Lambda(self, node):
args = self._extract_args(node)
body = self.visit(node.code)
visit_Continue = _map_class(_ast.Continue)
def _visit_BinOperator(opcls):
def _visit(self, node):
return self._new(_ast.BinOp, self.visit(node.left),
opcls(), self.visit(node.right))
return _visit
visit_Add = _visit_BinOperator(_ast.Add)
visit_Div = _visit_BinOperator(_ast.Div)
visit_FloorDiv = _visit_BinOperator(_ast.FloorDiv)
visit_LeftShift = _visit_BinOperator(_ast.LShift)
visit_Mod = _visit_BinOperator(_ast.Mod)
visit_Mul = _visit_BinOperator(_ast.Mult)
visit_Power = _visit_BinOperator(_ast.Pow)
visit_RightShift = _visit_BinOperator(_ast.RShift)
visit_Sub = _visit_BinOperator(_ast.Sub)
del _visit_BinOperator
def _visit_BitOperator(opcls):
def _visit(self, node):
def _make(nodes):
if len(nodes) == 1:
return self.visit(nodes[0])
left = _make(nodes[:-1])
right = self.visit(nodes[-1])
return self._new(_ast.BinOp, left, opcls(), right)
return _make(node.nodes)
return _visit
visit_Bitand = _visit_BitOperator(_ast.BitAnd)
visit_Bitor = _visit_BitOperator(_ast.BitOr)
visit_Bitxor = _visit_BitOperator(_ast.BitXor)
del _visit_BitOperator
def _visit(self, node):
return self._new(to)
return _visit
visit_Pass = _map_class(_ast.Pass)
visit_Break = _map_class(_ast.Break)
visit_Continue = _map_class(_ast.Continue)
def _visit_BinOperator(opcls):
def _visit(self, node):
return self._new(_ast.BinOp, self.visit(node.left),
opcls(), self.visit(node.right))
return _visit
visit_Add = _visit_BinOperator(_ast.Add)
visit_Div = _visit_BinOperator(_ast.Div)
visit_FloorDiv = _visit_BinOperator(_ast.FloorDiv)
visit_LeftShift = _visit_BinOperator(_ast.LShift)
visit_Mod = _visit_BinOperator(_ast.Mod)
visit_Mul = _visit_BinOperator(_ast.Mult)
visit_Power = _visit_BinOperator(_ast.Pow)
visit_RightShift = _visit_BinOperator(_ast.RShift)
visit_Sub = _visit_BinOperator(_ast.Sub)
del _visit_BinOperator
def _visit_BitOperator(opcls):
def _visit(self, node):
def _make(nodes):
if len(nodes) == 1:
return self.visit(nodes[0])
left = _make(nodes[:-1])
right = self.visit(nodes[-1])
return self._new(_ast.BinOp, left, opcls(), right)
def _map_class(to):
def _visit(self, node):
return self._new(to)
return _visit
visit_Pass = _map_class(_ast.Pass)
visit_Break = _map_class(_ast.Break)
visit_Continue = _map_class(_ast.Continue)
def _visit_BinOperator(opcls):
def _visit(self, node):
return self._new(_ast.BinOp, self.visit(node.left),
opcls(), self.visit(node.right))
return _visit
visit_Add = _visit_BinOperator(_ast.Add)
visit_Div = _visit_BinOperator(_ast.Div)
visit_FloorDiv = _visit_BinOperator(_ast.FloorDiv)
visit_LeftShift = _visit_BinOperator(_ast.LShift)
visit_Mod = _visit_BinOperator(_ast.Mod)
visit_Mul = _visit_BinOperator(_ast.Mult)
visit_Power = _visit_BinOperator(_ast.Pow)
visit_RightShift = _visit_BinOperator(_ast.RShift)
visit_Sub = _visit_BinOperator(_ast.Sub)
del _visit_BinOperator
def _visit_BitOperator(opcls):
def _visit(self, node):
def _make(nodes):
if len(nodes) == 1:
return self.visit(nodes[0])
left = _make(nodes[:-1])
def visit_Assign(self, node):
#self.name_types.append(_ast.Store)
targets = [self.visit(t) for t in node.nodes]
#self.name_types.pop()
return self._new(_ast.Assign, targets, self.visit(node.expr))
aug_operators = {
'+=': _ast.Add,
'/=': _ast.Div,
'//=': _ast.FloorDiv,
'<<=': _ast.LShift,
'%=': _ast.Mod,
'*=': _ast.Mult,
'**=': _ast.Pow,
'>>=': _ast.RShift,
'-=': _ast.Sub,
}
def visit_AugAssign(self, node):
target = self.visit(node.node)
# Because it's AugAssign target can't be list nor tuple
# so we only have to change context of one node
target.ctx = _ast.Store()
op = self.aug_operators[node.op]()
return self._new(_ast.AugAssign, target, op, self.visit(node.expr))
def _visit_Print(nl):
def _visit(self, node):
values = [self.visit(v) for v in node.nodes]
return self._new(_ast.Print, self.visit(node.dest), values, nl)
def _visit(self, node):
return self._new(_ast.UnaryOp, opcls(), self.visit(node.expr))
return _visit
right = self.visit(nodes[-1])
return self._new(_ast.BinOp, left, opcls(), right)
return _make(node.nodes)
return _visit
visit_Bitand = _visit_BitOperator(_ast.BitAnd)
visit_Bitor = _visit_BitOperator(_ast.BitOr)
visit_Bitxor = _visit_BitOperator(_ast.BitXor)
del _visit_BitOperator
def _visit_UnaryOperator(opcls):
def _visit(self, node):
return self._new(_ast.UnaryOp, opcls(), self.visit(node.expr))
return _visit
visit_Invert = _visit_UnaryOperator(_ast.Invert)
visit_Not = _visit_UnaryOperator(_ast.Not)
visit_UnaryAdd = _visit_UnaryOperator(_ast.UAdd)
visit_UnarySub = _visit_UnaryOperator(_ast.USub)
del _visit_UnaryOperator
def _visit_BoolOperator(opcls):
def _visit(self, node):
values = [self.visit(n) for n in node.nodes]
return self._new(_ast.BoolOp, opcls(), values)
return _visit
visit_And = _visit_BoolOperator(_ast.And)
visit_Or = _visit_BoolOperator(_ast.Or)
del _visit_BoolOperator
cmp_operators = {
'==': _ast.Eq,
'!=': _ast.NotEq,
body = self.visit(node.code)
if node.doc:
body = [self._new(_ast.Expr, self._new(_ast.Str, node.doc))] + body
return self._new(_ast.ClassDef, node.name, bases, body)
def visit_Return(self, node):
return self._new(_ast.Return, self.visit(node.value))
def visit_Assign(self, node):
#self.name_types.append(_ast.Store)
targets = [self.visit(t) for t in node.nodes]
#self.name_types.pop()
return self._new(_ast.Assign, targets, self.visit(node.expr))
aug_operators = {
'+=': _ast.Add,
'/=': _ast.Div,
'//=': _ast.FloorDiv,
'<<=': _ast.LShift,
'%=': _ast.Mod,
'*=': _ast.Mult,
'**=': _ast.Pow,
'>>=': _ast.RShift,
'-=': _ast.Sub,
}
def visit_AugAssign(self, node):
target = self.visit(node.node)
# Because it's AugAssign target can't be list nor tuple
# so we only have to change context of one node
target.ctx = _ast.Store()
del _visit_UnaryOperator
def _visit_BoolOperator(opcls):
def _visit(self, node):
values = [self.visit(n) for n in node.nodes]
return self._new(_ast.BoolOp, opcls(), values)
return _visit
visit_And = _visit_BoolOperator(_ast.And)
visit_Or = _visit_BoolOperator(_ast.Or)
del _visit_BoolOperator
cmp_operators = {
'==': _ast.Eq,
'!=': _ast.NotEq,
'<': _ast.Lt,
'<=': _ast.LtE,
'>': _ast.Gt,
'>=': _ast.GtE,
'is': _ast.Is,
'is not': _ast.IsNot,
'in': _ast.In,
'not in': _ast.NotIn,
}
def visit_Compare(self, node):
left = self.visit(node.expr)
ops = []
comparators = []
for optype, expr in node.ops:
ops.append(self.cmp_operators[optype]())
comparators.append(self.visit(expr))
return self._new(_ast.Compare, left, ops, comparators)