Python ast.GeneratorExp() Examples
The following are 20
code examples of ast.GeneratorExp().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
ast
, or try the search function
.
Example #1
Source File: eval.py From vnpy_crypto with MIT License | 6 votes |
def ast_names(code): """Iterator that yields all the (ast) names in a Python expression. :arg code: A string containing a Python expression. """ # Syntax that allows new name bindings to be introduced is tricky to # handle here, so we just refuse to do so. disallowed_ast_nodes = (ast.Lambda, ast.ListComp, ast.GeneratorExp) if sys.version_info >= (2, 7): disallowed_ast_nodes += (ast.DictComp, ast.SetComp) for node in ast.walk(ast.parse(code)): if isinstance(node, disallowed_ast_nodes): raise PatsyError("Lambda, list/dict/set comprehension, generator " "expression in patsy formula not currently supported.") if isinstance(node, ast.Name): yield node.id
Example #2
Source File: pyupgrade.py From pyupgrade with MIT License | 6 votes |
def _process_set_literal( tokens: List[Token], start: int, arg: ast.expr, ) -> None: if _is_wtf('set', tokens, start): return gen = isinstance(arg, ast.GeneratorExp) set_victims = _victims(tokens, start + 1, arg, gen=gen) del set_victims.starts[0] end_index = set_victims.ends.pop() tokens[end_index] = Token('OP', '}') for index in reversed(set_victims.starts + set_victims.ends): _remove_brace(tokens, index) tokens[start:start + 2] = [Token('OP', '{')]
Example #3
Source File: pyupgrade.py From pyupgrade with MIT License | 6 votes |
def visit_Call(self, node: ast.Call) -> None: if ( isinstance(node.func, ast.Name) and node.func.id == 'set' and len(node.args) == 1 and not node.keywords and isinstance(node.args[0], SET_TRANSFORM) ): arg, = node.args key = _ast_to_offset(node.func) if isinstance(arg, (ast.List, ast.Tuple)) and not arg.elts: self.set_empty_literals[key] = arg else: self.sets[key] = arg elif ( isinstance(node.func, ast.Name) and node.func.id == 'dict' and len(node.args) == 1 and not node.keywords and isinstance(node.args[0], (ast.ListComp, ast.GeneratorExp)) and isinstance(node.args[0].elt, (ast.Tuple, ast.List)) and len(node.args[0].elt.elts) == 2 ): self.dicts[_ast_to_offset(node.func)] = node.args[0] self.generic_visit(node)
Example #4
Source File: eval.py From Splunking-Crime with GNU Affero General Public License v3.0 | 6 votes |
def ast_names(code): """Iterator that yields all the (ast) names in a Python expression. :arg code: A string containing a Python expression. """ # Syntax that allows new name bindings to be introduced is tricky to # handle here, so we just refuse to do so. disallowed_ast_nodes = (ast.Lambda, ast.ListComp, ast.GeneratorExp) if sys.version_info >= (2, 7): disallowed_ast_nodes += (ast.DictComp, ast.SetComp) for node in ast.walk(ast.parse(code)): if isinstance(node, disallowed_ast_nodes): raise PatsyError("Lambda, list/dict/set comprehension, generator " "expression in patsy formula not currently supported.") if isinstance(node, ast.Name): yield node.id
Example #5
Source File: add_trailing_comma.py From add-trailing-comma with MIT License | 5 votes |
def visit_Call(self, node: ast.Call) -> None: argnodes = [*node.args, *node.keywords] arg_offsets = set() has_starargs = False for argnode in argnodes: if isinstance(argnode, ast.Starred): has_starargs = True if isinstance(argnode, ast.keyword) and argnode.arg is None: has_starargs = True offset = _to_offset(argnode) # multiline strings have invalid position, ignore them if offset.utf8_byte_offset != -1: # pragma: no branch (cpy bug) arg_offsets.add(offset) # If the sole argument is a generator, don't add a trailing comma as # this breaks lib2to3 based tools only_a_generator = ( len(argnodes) == 1 and isinstance(argnodes[0], ast.GeneratorExp) ) if arg_offsets and not only_a_generator: key = _to_offset(node) self.calls[key].append(Node(has_starargs, arg_offsets)) self.generic_visit(node)
Example #6
Source File: token_generator.py From pasta with Apache License 2.0 | 5 votes |
def _scope_helper(node): """Get the closure of nodes that could begin a scope at this point. For instance, when encountering a `(` when parsing a BinOp node, this could indicate that the BinOp itself is parenthesized OR that the BinOp's left node could be parenthesized. E.g.: (a + b * c) or (a + b) * c or (a) + b * c ^ ^ ^ Arguments: node: (ast.AST) Node encountered when opening a scope. Returns: A closure of nodes which that scope might apply to. """ if isinstance(node, ast.Attribute): return (node,) + _scope_helper(node.value) if isinstance(node, ast.Subscript): return (node,) + _scope_helper(node.value) if isinstance(node, ast.Assign): return (node,) + _scope_helper(node.targets[0]) if isinstance(node, ast.AugAssign): return (node,) + _scope_helper(node.target) if isinstance(node, ast.Expr): return (node,) + _scope_helper(node.value) if isinstance(node, ast.Compare): return (node,) + _scope_helper(node.left) if isinstance(node, ast.BoolOp): return (node,) + _scope_helper(node.values[0]) if isinstance(node, ast.BinOp): return (node,) + _scope_helper(node.left) if isinstance(node, ast.Tuple) and node.elts: return (node,) + _scope_helper(node.elts[0]) if isinstance(node, ast.Call): return (node,) + _scope_helper(node.func) if isinstance(node, ast.GeneratorExp): return (node,) + _scope_helper(node.elt) if isinstance(node, ast.IfExp): return (node,) + _scope_helper(node.body) return (node,)
Example #7
Source File: analyzer.py From chalice with Apache License 2.0 | 5 votes |
def visit_GeneratorExp(self, node): # type: (ast.GeneratorExp) -> None # Generator expressions are an interesting case. # They create a new sub scope, but they're not # explicitly named. Python just creates a table # with the name "genexpr". self._handle_comprehension(node, 'genexpr')
Example #8
Source File: test_ast.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_generatorexp(self): self._simple_comp(ast.GeneratorExp)
Example #9
Source File: hgawk_grammar.py From histogrammar-python with Apache License 2.0 | 5 votes |
def p_argument_2(p): '''argument : test comp_for''' # 1 2 p[0] = ast.GeneratorExp(p[1], p[2], rule=inspect.currentframe().f_code.co_name) inherit_lineno(p[0], p[1])
Example #10
Source File: hgawk_grammar.py From histogrammar-python with Apache License 2.0 | 5 votes |
def p_testlist_comp_1(p): '''testlist_comp : test comp_for''' # 1 2 p[0] = ast.GeneratorExp(p[1], p[2], rule=inspect.currentframe().f_code.co_name) inherit_lineno(p[0], p[1])
Example #11
Source File: bird.py From executing with MIT License | 5 votes |
def _nodes_of_interest(self, traced_file, start_lineno, end_lineno): # type: (TracedFile, int, int) -> Iterator[Tuple[ast.AST, Tuple]] """ Nodes that may have a value, show up as a box in the UI, and lie within the given line range. """ for node in traced_file.nodes: classes = [] if (isinstance(node, (ast.While, ast.For, ast.comprehension)) and not isinstance(node.parent, ast.GeneratorExp)): classes.append('loop') if isinstance(node, ast.stmt): classes.append('stmt') if isinstance(node, ast.expr): if not node._is_interesting_expression: continue elif not classes: continue assert isinstance(node, ast.AST) # In particular FormattedValue is missing this if not hasattr(node, 'first_token'): continue if not start_lineno <= node.first_token.start[0] <= end_lineno: continue start, end = traced_file.tokens.get_text_range(node) # type: int, int if start == end == 0: continue yield node, (classes, start, end)
Example #12
Source File: _recompute.py From icontract with MIT License | 5 votes |
def visit_GeneratorExp(self, node: ast.GeneratorExp) -> Any: """Compile the generator expression as a function and call it.""" result = self._execute_comprehension(node=node) for generator in node.generators: self.visit(generator.iter) # Do not set the computed value of the node since its representation would be non-informative. return result
Example #13
Source File: _recompute.py From icontract with MIT License | 5 votes |
def _execute_comprehension(self, node: Union[ast.ListComp, ast.SetComp, ast.GeneratorExp, ast.DictComp]) -> Any: """Compile the generator or comprehension from the node and execute the compiled code.""" args = [ast.arg(arg=name) for name in sorted(self._name_to_value.keys())] if platform.python_version_tuple() < ('3', ): raise NotImplementedError("Python versions below not supported, got: {}".format(platform.python_version())) if platform.python_version_tuple() < ('3', '8'): func_def_node = ast.FunctionDef( name="generator_expr", args=ast.arguments(args=args, kwonlyargs=[], kw_defaults=[], defaults=[]), decorator_list=[], body=[ast.Return(node)]) module_node = ast.Module(body=[func_def_node]) else: func_def_node = ast.FunctionDef( name="generator_expr", args=ast.arguments(args=args, posonlyargs=[], kwonlyargs=[], kw_defaults=[], defaults=[]), decorator_list=[], body=[ast.Return(node)]) module_node = ast.Module(body=[func_def_node], type_ignores=[]) ast.fix_missing_locations(module_node) code = compile(source=module_node, filename='<ast>', mode='exec') module_locals = {} # type: Dict[str, Any] module_globals = {} # type: Dict[str, Any] exec(code, module_globals, module_locals) # pylint: disable=exec-used generator_expr_func = module_locals["generator_expr"] return generator_expr_func(**self._name_to_value)
Example #14
Source File: test_ast.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_generatorexp(self): self._simple_comp(ast.GeneratorExp)
Example #15
Source File: test_ast.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_generatorexp(self): self._simple_comp(ast.GeneratorExp)
Example #16
Source File: NodeVisitorBases.py From ufora with Apache License 2.0 | 5 votes |
def isScopeNode(pyAstNode): """Return true iff argument is a scoped node.""" if isinstance(pyAstNode, (ast.Module, ast.ClassDef, ast.FunctionDef, ast.Lambda, ast.GeneratorExp)): return True else: return False
Example #17
Source File: debug.py From python-devtools with MIT License | 5 votes |
def _get_offsets(func_ast): import ast for arg in func_ast.args: start_line, start_col = arg.lineno - 2, arg.col_offset - 1 # horrible hack for http://bugs.python.org/issue31241 if isinstance(arg, (ast.ListComp, ast.GeneratorExp)): start_col -= 1 yield start_line, start_col for kw in func_ast.keywords: yield kw.value.lineno - 2, kw.value.col_offset - 2 - (len(kw.arg) if kw.arg else 0)
Example #18
Source File: tracer.py From executing with MIT License | 4 votes |
def loops(node): # type: (ast.AST) -> Tuple[Loop, ...] """ Return all the 'enclosing loops' of a node, up to the innermost class or function definition. This also includes the 'for in' clauses in list/dict/set/generator comprehensions. So for example, in this code: for x in ...: def foo(): while True: print([z for y in ...]) The loops enclosing the node 'z' are 'while True' and 'for y in ...', in that order. """ result = [] while True: try: parent = node.parent except AttributeError: break if isinstance(parent, ast.FunctionDef): break is_containing_loop = (((isinstance(parent, ast.For) and parent.iter is not node or isinstance(parent, ast.While)) and node not in parent.orelse) or (isinstance(parent, ast.comprehension) and node in parent.ifs)) if is_containing_loop: result.append(parent) elif isinstance(parent, (ast.ListComp, ast.GeneratorExp, ast.DictComp, ast.SetComp)): generators = parent.generators if node in generators: generators = list(takewhile(lambda n: n != node, generators)) result.extend(reversed(generators)) node = parent result.reverse() return tuple(result)
Example #19
Source File: bird.py From executing with MIT License | 4 votes |
def after_expr(self, node, frame, value, exc_value, exc_tb): # type: (ast.expr, FrameType, Any, Optional[BaseException], Optional[TracebackType]) -> Optional[ChangeValue] if _tracing_recursively(frame): return None if frame.f_code not in self._code_infos: return None if node._is_interesting_expression: # If this is an expression statement and the last statement # in the body, the value is returned from the cell magic # to be displayed as usual if (self._code_infos[frame.f_code].traced_file.is_ipython_cell and isinstance(node.parent, ast.Expr) and node.parent is node.parent.parent.body[-1]): self._ipython_cell_value = value if is_obvious_builtin(node, self.stack[frame].expression_values[node]): return None frame_info = self.stack[frame] if exc_value: node_value = self._exception_value(node, frame, exc_value) else: node_value = NodeValue.expression( self.num_samples, value, level=max(1, 3 - len(node._loops) * (not self._is_first_loop_iteration(node, frame))), ) self._set_node_value(node, frame, node_value) self._check_inner_call(frame_info, node, node_value) # i.e. is `node` the `y` in `[f(x) for x in y]`, making `node.parent` the `for x in y` is_special_comprehension_iter = ( isinstance(node.parent, ast.comprehension) and node is node.parent.iter and # Generators execute in their own time and aren't directly attached to the parent frame not isinstance(node.parent.parent, ast.GeneratorExp)) if not is_special_comprehension_iter: return None # Mark `for x in y` as a bit that executed, so it doesn't show as grey self._set_node_value(node.parent, frame, NodeValue.covered()) if exc_value: return None # Track each iteration over `y` so that the 'loop' can be stepped through loops = node._loops + (node.parent,) # type: Tuple[Loop, ...] def comprehension_iter_proxy(): for item in value: self._add_iteration(loops, frame) yield item # This effectively changes to code to `for x in comprehension_iter_proxy()` return ChangeValue(comprehension_iter_proxy())
Example #20
Source File: debug.py From python-devtools with MIT License | 4 votes |
def _process_args(self, func_ast, code_lines, args, kwargs) -> 'Generator[DebugArgument, None, None]': # noqa: C901 import ast complex_nodes = ( ast.Call, ast.Attribute, ast.Subscript, ast.IfExp, ast.BoolOp, ast.BinOp, ast.Compare, ast.DictComp, ast.ListComp, ast.SetComp, ast.GeneratorExp, ) arg_offsets = list(self._get_offsets(func_ast)) for i, arg in enumerate(args): try: ast_node = func_ast.args[i] except IndexError: # pragma: no cover # happens when code has been commented out and there are fewer func_ast args than real args yield self.output_class.arg_class(arg) continue if isinstance(ast_node, ast.Name): yield self.output_class.arg_class(arg, name=ast_node.id) elif isinstance(ast_node, complex_nodes): # TODO replace this hack with astor when it get's round to a new release start_line, start_col = arg_offsets[i] if i + 1 < len(arg_offsets): end_line, end_col = arg_offsets[i + 1] else: end_line, end_col = len(code_lines) - 1, None name_lines = [] for l_ in range(start_line, end_line + 1): start_ = start_col if l_ == start_line else 0 end_ = end_col if l_ == end_line else None name_lines.append(code_lines[l_][start_:end_].strip(' ')) yield self.output_class.arg_class(arg, name=' '.join(name_lines).strip(' ,')) else: yield self.output_class.arg_class(arg) kw_arg_names = {} for kw in func_ast.keywords: if isinstance(kw.value, ast.Name): kw_arg_names[kw.arg] = kw.value.id for name, value in kwargs.items(): yield self.output_class.arg_class(value, name=name, variable=kw_arg_names.get(name))