Пример #1
0
 def _convert(node):
     if compat.is_ast_str(node) or compat.is_ast_num(node):
         return compat.get_ast_const(node)
     elif isinstance(node, ast.Tuple):
         return tuple(map(_convert, node.elts))
     elif isinstance(node, ast.List):
         return list(map(_convert, node.elts))
     elif isinstance(node, ast.Dict):
         return dict((_convert(k), _convert(v))
                     for k, v in zip(node.keys, node.values))
     elif isinstance(node, ast.Name):
         if node.id in _safe_names:
             return _safe_names[node.id]
     elif (isinstance(node, ast.BinOp)
           and isinstance(node.op, (ast.Add, ast.Sub))
           and compat.is_ast_num(node.right)
           and isinstance(compat.get_ast_const(node.right), complex)
           and compat.is_ast_num(node.left)
           and isinstance(compat.get_ast_const(node.left),
                          (int, float))):  # TODO: long,
         left = compat.get_ast_const(node.left)
         right = compat.get_ast_const(node.right)
         if isinstance(node.op, ast.Add):
             return left + right
         else:
             return left - right
     raise ValueError('malformed string')
Пример #2
0
 def transform(tree, **kw):
     if compat.is_ast_num(tree):
         if compat.PY38:
             return ast.Constant(value=str(compat.get_ast_const(tree)))
         else:
             return ast.Str(s=str(tree.n))
     if compat.is_ast_str(tree):
         if compat.PY38:
             return ast.Constant(value=int(compat.get_ast_const(tree)))
         else:
             return ast.Num(n=int(tree.s))
     if type(tree) is ast.BinOp and type(tree.op) is ast.Mult:
         return ast.BinOp(tree.left, ast.Add(), tree.right)
     if type(tree) is ast.BinOp and type(tree.op) is ast.Add:
         return ast.BinOp(tree.left, ast.Mult(), tree.right)
Пример #3
0
 def stopper(tree, stop, **kw):
     if compat.is_ast_num(tree):
         compat.set_ast_const(tree, 0)
     if type(tree) is ast.BinOp and type(tree.op) is ast.Mult:
         stop()
Пример #4
0
 def deepen(tree, ctx, set_ctx, **kw):
     if compat.is_ast_num(tree):
         tree.n = compat.get_ast_const(tree) + ctx
     else:
         return set_ctx(ctx=ctx + 1)
Пример #5
0
 def sum(tree, collect, **kw):
     if compat.is_ast_num(tree):
         total[0] = total[0] + compat.get_ast_const(tree)
         return collect(compat.get_ast_const(tree))