本文整理匯總了Python中ast.GeneratorExp方法的典型用法代碼示例。如果您正苦於以下問題:Python ast.GeneratorExp方法的具體用法?Python ast.GeneratorExp怎麽用?Python ast.GeneratorExp使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類ast
的用法示例。
在下文中一共展示了ast.GeneratorExp方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: ast_names
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import GeneratorExp [as 別名]
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
示例2: _process_set_literal
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import GeneratorExp [as 別名]
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', '{')]
示例3: visit_Call
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import GeneratorExp [as 別名]
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)
示例4: _get_offsets
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import GeneratorExp [as 別名]
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)
示例5: isScopeNode
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import GeneratorExp [as 別名]
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
示例6: test_generatorexp
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import GeneratorExp [as 別名]
def test_generatorexp(self):
self._simple_comp(ast.GeneratorExp)
示例7: _execute_comprehension
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import GeneratorExp [as 別名]
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)
示例8: visit_GeneratorExp
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import GeneratorExp [as 別名]
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
示例9: visit_Call
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import GeneratorExp [as 別名]
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)
示例10: _nodes_of_interest
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import GeneratorExp [as 別名]
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)
示例11: p_testlist_comp_1
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import GeneratorExp [as 別名]
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])
示例12: p_argument_2
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import GeneratorExp [as 別名]
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])
示例13: visit_GeneratorExp
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import GeneratorExp [as 別名]
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')
示例14: _scope_helper
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import GeneratorExp [as 別名]
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,)
示例15: _process_args
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import GeneratorExp [as 別名]
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))