本文整理汇总了Python中ast.comprehension方法的典型用法代码示例。如果您正苦于以下问题:Python ast.comprehension方法的具体用法?Python ast.comprehension怎么用?Python ast.comprehension使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ast
的用法示例。
在下文中一共展示了ast.comprehension方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: analyze_generators
# 需要导入模块: import ast [as 别名]
# 或者: from ast import comprehension [as 别名]
def analyze_generators(self, generators):
"""Analyze the generators in a comprehension form.
Analyzes the binding part, and visits the "if" expressions (if any).
generators: an iterable of ast.comprehension objects
"""
for gen in generators:
# TODO: there's also an is_async field we might want to use in a future version.
targets = sanitize_exprs(gen.target)
values = sanitize_exprs(gen.iter)
self.analyze_binding(targets, values)
for expr in gen.ifs:
self.visit(expr)
示例2: collapse_inner
# 需要导入模块: import ast [as 别名]
# 或者: from ast import comprehension [as 别名]
def collapse_inner(self):
"""Combine lambda and comprehension Nodes with their parent Nodes to reduce visual noise.
Also mark those original nodes as undefined, so that they won't be visualized."""
# Lambdas and comprehensions do not define any names in the enclosing
# scope, so we only need to treat the uses edges.
# TODO: currently we handle outgoing uses edges only.
#
# What about incoming uses edges? E.g. consider a lambda that is saved
# in an instance variable, then used elsewhere. How do we want the
# graph to look like in that case?
for name in self.nodes:
if name in ('lambda', 'listcomp', 'setcomp', 'dictcomp', 'genexpr'):
for n in self.nodes[name]:
pn = self.get_parent_node(n)
if n in self.uses_edges:
for n2 in self.uses_edges[n]: # outgoing uses edges
self.logger.info(
'Collapsing inner from %s to %s, uses %s'
% (n, pn, n2)
)
self.add_uses_edge(pn, n2)
n.defined = False
示例3: _should_instrument_as_expression
# 需要导入模块: import ast [as 别名]
# 或者: from ast import comprehension [as 别名]
def _should_instrument_as_expression(self, node):
return (
isinstance(node, _ast.expr)
and hasattr(node, "end_lineno")
and hasattr(node, "end_col_offset")
and not getattr(node, "incorrect_range", False)
and "ignore" not in node.tags
and (not hasattr(node, "ctx") or isinstance(node.ctx, ast.Load))
# TODO: repeatedly evaluated subexpressions of comprehensions
# can be supported (but it requires some redesign both in backend and GUI)
and "ListComp.elt" not in node.tags
and "SetComp.elt" not in node.tags
and "DictComp.key" not in node.tags
and "DictComp.value" not in node.tags
and "comprehension.if" not in node.tags
)
示例4: comprehension
# 需要导入模块: import ast [as 别名]
# 或者: from ast import comprehension [as 别名]
def comprehension(target, iter, ifs, _):
return _ast.comprehension(target, iter, ifs)
示例5: get_iter_vars_from_for_loops
# 需要导入模块: import ast [as 别名]
# 或者: from ast import comprehension [as 别名]
def get_iter_vars_from_for_loops(tree):
for_nodes = get_nodes_of_type(tree, (ast.For, ast.comprehension))
try:
iter_var_names = {n.target.id for n in for_nodes}
except AttributeError:
iter_var_names = {}
return iter_var_names
示例6: addBinding
# 需要导入模块: import ast [as 别名]
# 或者: from ast import comprehension [as 别名]
def addBinding(self, node, value):
"""
Called when a binding is altered.
- `node` is the statement responsible for the change
- `value` is the new value, a Binding instance
"""
# assert value.source in (node, node.parent):
for scope in self.scopeStack[::-1]:
if value.name in scope:
break
existing = scope.get(value.name)
if existing and not self.differentForks(node, existing.source):
parent_stmt = self.getParent(value.source)
if isinstance(existing, Importation) and isinstance(parent_stmt, ast.For):
self.report(messages.ImportShadowedByLoopVar,
node, value.name, existing.source)
elif scope is self.scope:
if (isinstance(parent_stmt, ast.comprehension) and
not isinstance(self.getParent(existing.source),
(ast.For, ast.comprehension))):
self.report(messages.RedefinedInListComp,
node, value.name, existing.source)
elif not existing.used and value.redefines(existing):
if value.name != '_' or isinstance(existing, Importation):
self.report(messages.RedefinedWhileUnused,
node, value.name, existing.source)
elif isinstance(existing, Importation) and value.redefines(existing):
existing.redefined.append(node)
if value.name in self.scope:
# then assume the rebound name is used as a global or within a loop
value.used = self.scope[value.name].used
self.scope[value.name] = value
示例7: handleNodeStore
# 需要导入模块: import ast [as 别名]
# 或者: from ast import comprehension [as 别名]
def handleNodeStore(self, node):
name = getNodeName(node)
if not name:
return
# if the name hasn't already been defined in the current scope
if isinstance(self.scope, FunctionScope) and name not in self.scope:
# for each function or module scope above us
for scope in self.scopeStack[:-1]:
if not isinstance(scope, (FunctionScope, ModuleScope)):
continue
# if the name was defined in that scope, and the name has
# been accessed already in the current scope, and hasn't
# been declared global
used = name in scope and scope[name].used
if used and used[0] is self.scope and name not in self.scope.globals:
# then it's probably a mistake
self.report(messages.UndefinedLocal,
scope[name].used[1], name, scope[name].source)
break
parent_stmt = self.getParent(node)
if isinstance(parent_stmt, (ast.For, ast.comprehension)) or (
parent_stmt != node.parent and
not self.isLiteralTupleUnpacking(parent_stmt)):
binding = Binding(name, node)
elif name == '__all__' and isinstance(self.scope, ModuleScope):
binding = ExportBinding(name, node.parent, self.scope)
else:
binding = Assignment(name, node)
self.addBinding(node, binding)
示例8: test_base_classes
# 需要导入模块: import ast [as 别名]
# 或者: from ast import comprehension [as 别名]
def test_base_classes(self):
self.assertTrue(issubclass(ast.For, ast.stmt))
self.assertTrue(issubclass(ast.Name, ast.expr))
self.assertTrue(issubclass(ast.stmt, ast.AST))
self.assertTrue(issubclass(ast.expr, ast.AST))
self.assertTrue(issubclass(ast.comprehension, ast.AST))
self.assertTrue(issubclass(ast.Gt, ast.AST))
示例9: test_compile_from_ast_019
# 需要导入模块: import ast [as 别名]
# 或者: from ast import comprehension [as 别名]
def test_compile_from_ast_019(self):
p = ast.parse("[x for x in range(2)]", mode="eval") # list comprehension
c = compile(p,"<unknown>", mode="eval")
self.assertEqual( eval(c), [0,1] )
示例10: test_compile_from_ast_020
# 需要导入模块: import ast [as 别名]
# 或者: from ast import comprehension [as 别名]
def test_compile_from_ast_020(self):
# list comprehension
p = ast.parse("[(x, y, z) for x in [1,2,3] if x!=2 for y in [3,1,4] for z in [7,8,9] if x != y]", mode="eval")
c = compile(p,"<unknown>", mode="eval")
self.assertEqual( eval(c), [(1, 3, 7), (1, 3, 8), (1, 3, 9), (1, 4, 7),
(1, 4, 8), (1, 4, 9), (3, 1, 7), (3, 1, 8),
(3, 1, 9), (3, 4, 7), (3, 4, 8), (3, 4, 9)] )
示例11: test_compile_from_ast_023
# 需要导入模块: import ast [as 别名]
# 或者: from ast import comprehension [as 别名]
def test_compile_from_ast_023(self):
p = ast.parse("{x for x in range(3) if x!=2 }", mode="eval") # set comprehension
c = compile(p,"<unknown>", mode="eval")
self.assertEqual( eval(c), {0,1} )
示例12: test_compile_from_ast_024
# 需要导入模块: import ast [as 别名]
# 或者: from ast import comprehension [as 别名]
def test_compile_from_ast_024(self):
p = ast.parse("{ x : ord(x) for x in ['a','b'] }", mode="eval") # dict comprehension
c = compile(p,"<unknown>", mode="eval")
self.assertEqual( eval(c), {'a':97, 'b':98 } )
示例13: _check_comprehension
# 需要导入模块: import ast [as 别名]
# 或者: from ast import comprehension [as 别名]
def _check_comprehension(self, fac):
self.expr(fac([]), "comprehension with no generators")
g = ast.comprehension(ast.Name("x", ast.Load()),
ast.Name("x", ast.Load()), [])
self.expr(fac([g]), "must have Store context")
g = ast.comprehension(ast.Name("x", ast.Store()),
ast.Name("x", ast.Store()), [])
self.expr(fac([g]), "must have Load context")
x = ast.Name("x", ast.Store())
y = ast.Name("y", ast.Load())
g = ast.comprehension(x, y, [None])
self.expr(fac([g]), "None disallowed")
g = ast.comprehension(x, y, [ast.Name("x", ast.Store())])
self.expr(fac([g]), "must have Load context")
示例14: _simple_comp
# 需要导入模块: import ast [as 别名]
# 或者: from ast import comprehension [as 别名]
def _simple_comp(self, fac):
g = ast.comprehension(ast.Name("x", ast.Store()),
ast.Name("x", ast.Load()), [])
self.expr(fac(ast.Name("x", ast.Store()), [g]),
"must have Load context")
def wrap(gens):
return fac(ast.Name("x", ast.Store()), gens)
self._check_comprehension(wrap)
示例15: test_dictcomp
# 需要导入模块: import ast [as 别名]
# 或者: from ast import comprehension [as 别名]
def test_dictcomp(self):
g = ast.comprehension(ast.Name("y", ast.Store()),
ast.Name("p", ast.Load()), [])
c = ast.DictComp(ast.Name("x", ast.Store()),
ast.Name("y", ast.Load()), [g])
self.expr(c, "must have Load context")
c = ast.DictComp(ast.Name("x", ast.Load()),
ast.Name("y", ast.Store()), [g])
self.expr(c, "must have Load context")
def factory(comps):
k = ast.Name("x", ast.Load())
v = ast.Name("y", ast.Load())
return ast.DictComp(k, v, comps)
self._check_comprehension(factory)