本文整理汇总了Python中ast.comprehension函数的典型用法代码示例。如果您正苦于以下问题:Python comprehension函数的具体用法?Python comprehension怎么用?Python comprehension使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了comprehension函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_DictComp
def test_DictComp(self):
gen = ast.comprehension(ast.Name('x', ast.Store()), ast.Name('y',
ast.Load()), [ast.Num(2)])
dictcomp = ast.DictComp(ast.Name('v', ast.Load()), ast.Name('w',
ast.Load()), [gen])
self.verify(dictcomp, '{v:w for x in y if 2}')
gen2 = ast.comprehension(ast.Name('a', ast.Store()), ast.Name('b',
ast.Load()), [])
dictcomp.generators.append(gen2)
self.verify(dictcomp, '{v:w for x in y if 2 for a in b}')
示例2: seq_comp_test
def seq_comp_test(self, node_type, ends):
gen = ast.comprehension(ast.Name('x', ast.Store()), ast.Name('y',
ast.Load()), [ast.Num(2)])
listcomp = node_type(ast.Name('w', ast.Load()), [gen])
self.verify(listcomp, '{}w for x in y if 2{}'.format(*ends))
gen2 = ast.comprehension(ast.Name('a', ast.Store()), ast.Name('b',
ast.Load()), [])
listcomp.generators.append(gen2)
self.verify(listcomp, '{}w for x in y if 2 for a in b{}'.format(*ends))
return listcomp
示例3: _check_comprehension
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")
示例4: parse
def parse(self):
"""
Convert the parsed tokens into a list of expressions then join them
"""
self.stream = tokenise(self.source)
steps = []
for token in self.stream:
code = self._token_to_code(token)
if code:
steps.append(code)
# result = [str(x) for x in steps]
return ast.Expression(
body=ast.ListComp(
elt=build_call(
ast.Name(id='str', ctx=ast.Load()),
args=[
ast.Name(id='x', ctx=ast.Load()),
],
),
generators=[
ast.comprehension(
target=ast.Name(id='x', ctx=ast.Store()),
iter=ast.List(elts=steps, ctx=ast.Load()),
ifs=[]
)
]
)
)
示例5: For
def For(var_in, body):
gen = [ast.comprehension(Store(n.id), s, []) for (n, s) in var_in]
## Nested for-loops are elided together.
if isinstance(body, ast.GeneratorExp):
gen.extend(body.generators)
body = body.elt
return ast.GeneratorExp(body, gen)
示例6: parse
def parse(self):
'''Convert the parsed tokens into a list of expressions
Then join them'''
steps = []
self.stream = tokenise(self.source)
for token in self.stream:
code = self._token_to_code(token)
if code is not None:
steps.append(code)
# result = [str(x) for x in steps]
return ast.Module(
body=[ast.Assign(
targets=[ast.Name(id='result', ctx=ast.Store())],
value=ast.ListComp(
elt=ast.Call(
func=ast.Name(id='str', ctx=ast.Load()),
args=[
ast.Name(id='x', ctx=ast.Load()),
],
keywords=[],
),
generators=[
ast.comprehension(
target=ast.Name(id='x', ctx=ast.Store()),
iter=ast.List(elts=steps, ctx=ast.Load()),
ifs=[]
)
]
)
)
])
示例7: visit_Call
def visit_Call(self, node):
if node.func.id not in GRP_FUNCS:
return node
else:
self.generic_visit(node)
return ast.Call(
func=node.func,
args=[
ast.ListComp(
elt=node.args[0],
generators=[
ast.comprehension(
target=ast.Name(id="datarow", ctx=ast.Store(), lineno=0, col_offset=0),
iter=ast.Name(id="data", ctx=ast.Load(), lineno=0, col_offset=0),
ifs=[],
lineno=0,
col_offset=0
)
],
lineno=0,
col_offset=0,
)
],
keywords=[],
ctx=ast.Load(),
lineno=0,
col_offset=0,
)
示例8: _peval_comprehension_generators
def _peval_comprehension_generators(state, generators, ctx):
if len(generators) == 0:
return state, []
generator = generators[0]
next_generators = generators[1:]
state, iter_result = _peval_expression(state, generator.iter, ctx)
masked_bindings = _get_masked_bindings(generator.target, ctx.bindings)
masked_ctx = ctx.set('bindings', masked_bindings)
state, ifs_result = _peval_comprehension_ifs(state, generator.ifs, masked_ctx)
if is_known_value(ifs_result):
success, bool_value = try_call(bool, args=(ifs_result.value,))
if success and bool_value:
ifs_result = []
state, new_generator_kwds = map_reify(
state, dict(target=generator.target, iter=iter_result, ifs=ifs_result))
new_generator = ast.comprehension(**new_generator_kwds)
state, new_generators = _peval_comprehension_generators(state, next_generators, ctx)
return state, [new_generator] + new_generators
示例9: compile_list_comprehension
def compile_list_comprehension(self, expr):
# (list-comp expr (target iter) cond?)
expr.pop(0)
expression = expr.pop(0)
tar_it = iter(expr.pop(0))
targets = zip(tar_it, tar_it)
cond = self.compile(expr.pop(0)) if expr != [] else None
ret = ast.ListComp(
lineno=expr.start_line,
col_offset=expr.start_column,
elt=self.compile(expression),
generators=[])
for target, iterable in targets:
ret.generators.append(ast.comprehension(
target=self._storeize(self.compile(target)),
iter=self.compile(iterable),
ifs=[]))
if cond:
ret.generators[-1].ifs.append(cond)
return ret
示例10: handleComprehensions
def handleComprehensions(self, comps, lineno, offset, *args):
ccomps = []
for c in comps:
ccomps.append(ast.comprehension(target=self.dispatch(c.target, *args),
iter=retic_ast.UseCheck(value=self.dispatch(c.iter), type=retic_ast.Subscriptable(), lineno=lineno, col_offset=offset),
ifs=self.reduce(c.ifs, *args)))
return ccomps, []
示例11: _simple_comp
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)
示例12: visit_comp_for
def visit_comp_for(self, values, ctx):
"""comp_for: 'for' exprlist 'in' or_test [comp_iter]"""
if len(values) > 4:
raise NotImplementedError("comp_for with more than 4 arguments not supported")
node = ast.comprehension()
node.target = self.visit(values[1], ast.Store())
node.iter = self.visit(values[3], ctx)
node.ifs = []
return node
示例13: handleComprehensions
def handleComprehensions(self, comps, varchecks, *args):
generators = []
for comp in comps:
iter = self.dispatch(comp.iter, varchecks, *args)
target = self.dispatch(comp.target, varchecks, *args)
vars = scope.WriteTargetFinder().preorder(target)
varchecks = set.union(vars, varchecks)
ifs = self.dispatch(comp.ifs, varchecks, *args)
generators.append(ast.comprehension(target=target, iter=iter, ifs=ifs))
return generators, varchecks
示例14: generate
def generate(self, element:Element, GC:GenerationContext):
acode = element.code
if len(acode) is 2 and is_form(acode[1], "for"):
for_form = acode[1].code
# list comprehension
# («[]» (for (in i lst) (f i))) # list compr
in_el = for_form[1]
in_el_code = in_el.code
#with GC.let(domain=ExDom):
assert is_identifier(in_el, "in")
target_element = in_el_code[1]
iter_element = in_el_code[2]
with GC.let(domain=LVDom):
target_code = GC.generate(target_element)
with GC.let(domain=ExDom):
iter_code = GC.generate(iter_element)
generators = [ ast.comprehension(target=target_code,
iter=iter_code,
ifs=[]) ]
to_evaluate_element = for_form[2]
with GC.let(domain=ExDom):
to_evaluate_code = GC.generate(to_evaluate_element)
return ast.ListComp(to_evaluate_code, generators)
else:
els = self.generate_as_expressions(GC, *acode[1:])
if GC.domain == LVDom:
return ast.List(els, ast.Store())
return expr_wrap(ast.List(els, ast.Load()), GC)
示例15: p_comp_async_for2
def p_comp_async_for2(self, p):
''' comp_for : ASYNC FOR exprlist IN or_test comp_iter '''
target = p[3]
self.set_context(target, Store, p)
gens = []
gens.append(ast.comprehension(target=target, iter=p[5], ifs=[],
is_async=1))
for item in p[6]:
if isinstance(item, ast.comprehension):
gens.append(item)
else:
gens[-1].ifs.append(item)
p[0] = gens