我们从Python开源项目中,提取了以下46个代码示例,用于说明如何使用ast.Expression()。
def generic_visit(self, node): # Fallback when we don't have a special implementation. if _is_ast_expr(node): mod = ast.Expression(node) co = self._compile(mod) try: result = self.frame.eval(co) except Exception: raise Failure() explanation = self.frame.repr(result) return explanation, result elif _is_ast_stmt(node): mod = ast.Module([node]) co = self._compile(mod, "exec") try: self.frame.exec_(co) except Exception: raise Failure() return None, None else: raise AssertionError("can't handle %s" %(node,))
def fold(self, node): self._has_names.append(False) node = self.generic_visit(node) if not self._has_names.pop(): try: node = self.convert(node) except TypeError as exc: if not isinstance(node, ast.Expression) \ and str(exc).startswith('expected Expression node, got'): try: node = self.convert(ast.Expression(body=node)) except: pass except: pass return node
def safe_eval_gpr(expr, conf_genes): """Internal function to evaluate a gene-protein rule in an injection-safe manner (hopefully). """ if isinstance(expr, Expression): return safe_eval_gpr(expr.body, conf_genes) elif isinstance(expr, Name): fgid = format_gid(expr.id) if fgid not in conf_genes: return 0 return conf_genes[fgid] elif isinstance(expr, BoolOp): op = expr.op if isinstance(op, Or): return max(safe_eval_gpr(i, conf_genes) for i in expr.values) elif isinstance(op, And): return min(safe_eval_gpr(i, conf_genes) for i in expr.values) else: raise TypeError("unsupported operation " + op.__class__.__name__) elif expr is None: return 0 else: raise TypeError("unsupported operation " + repr(expr))
def test_increment_lineno(self): src = ast.parse('1 + 1', mode='eval') self.assertEqual(ast.increment_lineno(src, n=3), src) self.assertEqual(ast.dump(src, include_attributes=True), 'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), ' 'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, ' 'col_offset=0))' ) # issue10869: do not increment lineno of root twice src = ast.parse('1 + 1', mode='eval') self.assertEqual(ast.increment_lineno(src.body, n=3), src.body) self.assertEqual(ast.dump(src, include_attributes=True), 'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), ' 'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, ' 'col_offset=0))' )
def generic_visit(self, node): if self.last_node is not None and hasattr(node, 'col_offset'): enode = ast.Expression(self.last_node) lambda_code = compile(enode, '<unused>', 'eval') lines = self.lines[self.last_node.lineno-1:node.lineno] lines[-1] = lines[-1][:node.col_offset] lines[0] = lines[0][self.last_node.col_offset:] lambda_body_text = ' '.join(l.rstrip(' \t\\').strip() for l in lines) while lambda_body_text: try: code = compile(lambda_body_text, '<unused>', 'eval') if len(code.co_code) == len(lambda_code.co_code): break except SyntaxError: pass lambda_body_text = lambda_body_text[:-1] self.lambdas.append((lambda_code, lambda_body_text.strip())) self.last_node = None super().generic_visit(node)
def run_in_context(code, context, defs={}): ast_ = ast.parse(code, '<code>', 'exec') last_expr = None last_def_name = None for field_ in ast.iter_fields(ast_): if 'body' != field_[0]: continue if len(field_[1]) > 0: le = field_[1][-1] if isinstance(le, ast.Expr): last_expr = ast.Expression() last_expr.body = field_[1].pop().value elif isinstance(le, (ast.FunctionDef, ast.ClassDef)): last_def_name = le.name exec(compile(ast_, '<hbi-code>', 'exec'), context, defs) if last_expr is not None: return eval(compile(last_expr, '<hbi-code>', 'eval'), context, defs) elif last_def_name is not None: return defs[last_def_name] return None
def extract_option_from_arg_list(options, optname, default_value): if not options: return default_value, options try: args = list(ast.iter_fields(ast.parse(f"f({options})", mode='eval')))[0][1].keywords for idx,field in enumerate(args): if field.arg == optname: try: value = eval(compile(ast.Expression(body=field.value), filename="<ast>", mode="eval")) new_options = ','.join([x for x in options.split(',') if not x.strip().startswith(optname)]) return value, new_options.strip() except: raise ValueError(f"A constant value is expected for option {optname}: {options} provided.") return default_value, options except SyntaxError as e: raise ValueError(f"Expect a list of keyword arguments: {options} provided")
def convert(self, node): code = compile(node, '<string>', mode = 'eval') value = eval(code) new_node = ast.parse(str(value), mode = 'eval') if isinstance(new_node, ast.Expression): new_node = new_node.body new_node = self.generic_visit(new_node) node = ast.copy_location(new_node, node) node = ast.fix_missing_locations(node) return node
def isStatement(a): """Determine whether the given node is a statement (vs an expression)""" return type(a) in [ ast.Module, ast.Interactive, ast.Expression, ast.Suite, ast.FunctionDef, ast.ClassDef, ast.Return, ast.Delete, ast.Assign, ast.AugAssign, ast.For, ast.While, ast.If, ast.With, ast.Raise, ast.Try, ast.Assert, ast.Import, ast.ImportFrom, ast.Global, ast.Expr, ast.Pass, ast.Break, ast.Continue ]
def evaluateFalse(s): """ Replaces operators with the SymPy equivalent and sets evaluate=False. """ node = ast.parse(s) node = EvaluateFalseTransformer().visit(node) # node is a Module, we want an Expression node = ast.Expression(node.body[0].value) return ast.fix_missing_locations(node)
def test_copy_location(self): src = ast.parse('1 + 1', mode='eval') src.body.right = ast.copy_location(ast.Num(2), src.body.right) self.assertEqual(ast.dump(src, include_attributes=True), 'Expression(body=BinOp(left=Num(n=1, lineno=1, col_offset=0), ' 'op=Add(), right=Num(n=2, lineno=1, col_offset=4), lineno=1, ' 'col_offset=0))' )
def test_module(self): m = ast.Interactive([ast.Expr(ast.Name("x", ast.Store()))]) self.mod(m, "must have Load context", "single") m = ast.Expression(ast.Name("x", ast.Store())) self.mod(m, "must have Load context", "eval")
def _make_list_func(tree, field_names_dict, filename=None, add_args=None): filename = filename or '<generated>' list_name = 'data' rewriter = RewriteName(list_name, field_names_dict) lambda_args = [ast.Name(id=list_name, ctx=ast.Param(), lineno=0, col_offset=0)] for arg in (add_args or []): lambda_args.append( ast.Name(id=arg, ctx=ast.Param(), lineno=0, col_offset=0) ) tree = rewriter.visit(tree) tree = ast.Expression( body = ast.Lambda( lineno = 0, col_offset = 0, body = tree.body, args = ast.arguments( args = lambda_args, vararg = None, kwarg = None, defaults = [], ) ), ) code = compile(tree, filename, 'eval') func = eval(code) return func
def Expression(body): return body
def whereeval(str_, get=None): """Evaluate a set operation string, where each Name is fetched""" if get is None: import redbiom config = redbiom.get_config() get = redbiom._requests.make_get(config) # Load is subject to indirection to simplify testing globals()['Load'] = make_Load(get) formed = ast.parse(str_, mode='eval') node_types = (ast.Compare, ast.In, ast.NotIn, ast.BoolOp, ast.And, ast.Name, ast.Or, ast.Eq, ast.Lt, ast.LtE, ast.Gt, ast.GtE, ast.NotEq, ast.Str, ast.Num, ast.Load, ast.Expression, ast.Tuple, ast.Is, ast.IsNot) for node in ast.walk(formed): if not isinstance(node, node_types): raise TypeError("Unsupported node type: %s" % ast.dump(node)) result = eval(ast.dump(formed)) # clean up global Load del Load return result
def execute_code(expr, file): ipy = Yuuno.instance().environment.ipython expr = ipy.input_transformer_manager.transform_cell(expr) expr_ast = ipy.compile.ast_parse(expr) expr_ast = ipy.transform_ast(expr_ast) code = ipy.compile(ast.Expression(expr_ast.body[0].value), file, 'eval') return eval(code, ipy.user_ns, {})
def context_env_update(context_list, env): es = ExitStack() for item in context_list: # create context manager and enter tmp_name = '__pw_cm' cm_code = compile(ast.Expression(item.context_expr), '<context_eval>', 'eval') env[tmp_name] = es.enter_context(eval(cm_code, env)) # assign to its optional_vars in separte dict if item.optional_vars: code = assign_from_ast(item.optional_vars, tmp_name) exec(code, env) return es
def SoS_exec(script, _dict=None, return_result=False): '''Execute a statement.''' if _dict is None: _dict = env.sos_dict._dict if not return_result: exec(script, _dict) return None try: stmts = list(ast.iter_child_nodes(ast.parse(script))) if not stmts: return if isinstance(stmts[-1], ast.Expr): # the last one is an expression and we will try to return the results # so we first execute the previous statements if len(stmts) > 1: exec(compile(ast.Module(body=stmts[:-1]), filename="<ast>", mode="exec"), _dict) # then we eval the last one res = eval(compile(ast.Expression(body=stmts[-1].value), filename="<ast>", mode="eval"), _dict) else: # otherwise we just execute the entire code exec(script, _dict) res = None except SyntaxError as e: raise SyntaxError(f"Invalid code {script}: {e}") #if check_readonly: # env.sos_dict.check_readonly_vars() return res # # dynamic expression that cannot be resolved during parsing # at prepare mode etc, and has to be resolved at run time. #
def safe_eval(node_or_string, env): """ Safely evaluate an expression node or a string containing a Python expression. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None. """ _safe_names = {'None': None, 'True': True, 'False': False} _safe_names.update(env) if isinstance(node_or_string, basestring): node_or_string = ast.parse(node_or_string, mode='eval') if isinstance(node_or_string, ast.Expression): node_or_string = node_or_string.body def _convert(node): if isinstance(node, ast.Str): return node.s elif isinstance(node, ast.Num): return node.n 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 \ isinstance(node.right, ast.Num) and \ isinstance(node.right.n, complex) and \ isinstance(node.left, ast.Num) and \ isinstance(node.left.n, (int, long, float)): left = node.left.n right = node.right.n if isinstance(node.op, ast.Add): return left + right else: return left - right raise ValueError('malformed string') return _convert(node_or_string)
def safe_eval(node_or_string, env): """ Safely evaluate an expression node or a string containing a Python expression. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None. """ _safe_names = {'None': None, 'True': True, 'False': False} _safe_names.update(env) if isinstance(node_or_string, basestring): node_or_string = ast_parse(node_or_string, mode='eval') if isinstance(node_or_string, ast.Expression): node_or_string = node_or_string.body def _convert(node): if isinstance(node, ast.Str): return node.s elif isinstance(node, ast.Num): return node.n 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, (Add, Sub)) and \ isinstance(node.right, Num) and \ isinstance(node.right.n, complex) and \ isinstance(node.left, Num) and \ isinstance(node.left.n, (int, long, float)): left = node.left.n right = node.right.n if isinstance(node.op, Add): return left + right else: return left - right raise ValueError('malformed string') return _convert(node_or_string)
def make_grp_list_func(expr_str, field_names_dict, global_names_dict=None, filename=None): """ >>> func = make_grp_list_func('sum(a)/cnt()', {'a': 0}) >>> func([(1,), (2,), (3,)]) 2 >>> func = make_grp_list_func('1+sum(a)+sum(b**2)', {'a':0, 'b':1}) >>> func([(10, 1), (20, 2)]) 36 >>> func = make_grp_list_func('b+sum(a)/sum(c)', {'a': 0, 'c': 1}, {'b': 0}) >>> func([(10, 1), (20, 2), (30, 3)], [1]) 11 >>> func = make_grp_list_func('sum(a)', {'a': 0}) >>> func([(1,), (2,), (3,)]) 6 """ filename = filename or '<generated>' list_name = 'data' lambda_args = [ast.Name(id=list_name, ctx=ast.Param(), lineno=0, col_offset=0)] tree = ast.parse(expr_str, filename, 'eval') tree = RewriteCntFuncs().visit(tree) tree = RewriteGrpFuncs(field_names_dict).visit(tree) if global_names_dict: lambda_args.append(ast.Name(id='globaldata', ctx=ast.Param(), lineno=0, col_offset=0)) tree = RewriteName('globaldata', global_names_dict).visit(tree) tree = ast.Expression( body = ast.Lambda( body=tree.body, args=ast.arguments( args=lambda_args, vararg=None, kwarg=None, defaults=[], ), lineno=0, col_offset=0, ), ) #print ast.dump(tree) code = compile(tree, filename, 'eval') func = eval(code) return func
def seteval(str_, get=None, stemmer=None, target=None): """Evaluate a set operation string, where each Name is fetched Parameters ---------- str_ : str The query to evaluate get : function, optional A getting method, defaults to instatiating one from _requests stemmer : function, optional A method to stem a query Name. If None, defaults to passthrough. target : str, optional A subcontext to query against. If None, defaults to text-search. """ if get is None: import redbiom config = redbiom.get_config() get = redbiom._requests.make_get(config) if stemmer is None: stemmer = passthrough if target is None: target = 'text-search' # Load is subject to indirection to simplify testing globals()['Load'] = make_Load(get) # this seems right now to be the easiest way to inject parameters # into Name globals()['stemmer'] = stemmer globals()['target'] = target formed = ast.parse(str_, mode='eval') node_types = (ast.BitAnd, ast.BitOr, ast.BitXor, ast.Name, ast.Sub, ast.Expression, ast.BinOp, ast.Load) for node in ast.walk(formed): if not isinstance(node, node_types): raise TypeError("Unsupported node type: %s" % ast.dump(node)) result = eval(ast.dump(formed)) # clean up global Load del Load del stemmer del target return result
def Exec(self, source): self.execution_count += 1 try: nodes = ast.parse(source, self.filename) except IndentationError as e: raise ParseError(e) except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError) as e: raise ParseError(e) stdout = StringIO.StringIO() stderr = StringIO.StringIO() prev_stdout = sys.stdout prev_stderr = sys.stderr sys.stdout = stdout sys.stderr = stderr try: if isinstance(nodes.body[-1], ast.Expr): exec_nodes = nodes.body[:-1] interactive_nodes = nodes.body[-1:] else: exec_nodes, interactive_nodes = nodes.body, [] for node in exec_nodes: mod = ast.Module([node]) code = compile(mod, self.filename, "exec") exec(code, self.global_context, self.local_context) result = None for node in interactive_nodes: source = codegen.to_source(node) new_node = ast.parse(source, self.filename, mode="eval") mod = ast.Expression(new_node.body) code = compile(mod, self.filename, "eval") result = eval(code, self.global_context, self.local_context) sys.stdout = prev_stdout sys.stderr = prev_stderr return stdout.getvalue(), stderr.getvalue(), result except Exception as e: raise ExecError(stdout.getvalue(), stderr.getvalue(), e) finally: sys.stdout = prev_stdout sys.stderr = prev_stderr
def _gclient_eval(node_or_string, global_scope, filename='<unknown>'): """Safely evaluates a single expression. Returns the result.""" _allowed_names = {'None': None, 'True': True, 'False': False} if isinstance(node_or_string, basestring): node_or_string = ast.parse(node_or_string, filename=filename, mode='eval') if isinstance(node_or_string, ast.Expression): node_or_string = node_or_string.body def _convert(node): if isinstance(node, ast.Str): return node.s elif isinstance(node, ast.Num): return node.n 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 collections.OrderedDict( (_convert(k), _convert(v)) for k, v in zip(node.keys, node.values)) elif isinstance(node, ast.Name): if node.id not in _allowed_names: raise ValueError( 'invalid name %r (file %r, line %s)' % ( node.id, filename, getattr(node, 'lineno', '<unknown>'))) return _allowed_names[node.id] elif isinstance(node, ast.Call): if not isinstance(node.func, ast.Name): raise ValueError( 'invalid call: func should be a name (file %r, line %s)' % ( filename, getattr(node, 'lineno', '<unknown>'))) if node.keywords or node.starargs or node.kwargs: raise ValueError( 'invalid call: use only regular args (file %r, line %s)' % ( filename, getattr(node, 'lineno', '<unknown>'))) args = map(_convert, node.args) return global_scope[node.func.id](*args) elif isinstance(node, ast.BinOp) and isinstance(node.op, ast.Add): return _convert(node.left) + _convert(node.right) elif isinstance(node, ast.BinOp) and isinstance(node.op, ast.Mod): return _convert(node.left) % _convert(node.right) else: raise ValueError( 'unexpected AST node: %s %s (file %r, line %s)' % ( node, ast.dump(node), filename, getattr(node, 'lineno', '<unknown>'))) return _convert(node_or_string)
def Exec(content, global_scope, local_scope, filename='<unknown>'): """Safely execs a set of assignments. Mutates |local_scope|.""" node_or_string = ast.parse(content, filename=filename, mode='exec') if isinstance(node_or_string, ast.Expression): node_or_string = node_or_string.body def _visit_in_module(node): if isinstance(node, ast.Assign): if len(node.targets) != 1: raise ValueError( 'invalid assignment: use exactly one target (file %r, line %s)' % ( filename, getattr(node, 'lineno', '<unknown>'))) target = node.targets[0] if not isinstance(target, ast.Name): raise ValueError( 'invalid assignment: target should be a name (file %r, line %s)' % ( filename, getattr(node, 'lineno', '<unknown>'))) value = _gclient_eval(node.value, global_scope, filename=filename) if target.id in local_scope: raise ValueError( 'invalid assignment: overrides var %r (file %r, line %s)' % ( target.id, filename, getattr(node, 'lineno', '<unknown>'))) local_scope[target.id] = value else: raise ValueError( 'unexpected AST node: %s %s (file %r, line %s)' % ( node, ast.dump(node), filename, getattr(node, 'lineno', '<unknown>'))) if isinstance(node_or_string, ast.Module): for stmt in node_or_string.body: _visit_in_module(stmt) else: raise ValueError( 'unexpected AST node: %s %s (file %r, line %s)' % ( node_or_string, ast.dump(node_or_string), filename, getattr(node_or_string, 'lineno', '<unknown>'))) _GCLIENT_SCHEMA.validate(local_scope)