def eval_statement_element(self, element): if pr.Array.is_type(element, pr.Array.NOARRAY): try: lst_cmp = element[0].expression_list()[0] if not isinstance(lst_cmp, pr.ListComprehension): raise IndexError except IndexError: r = list( itertools.chain.from_iterable( self.eval_statement(s) for s in element)) else: r = [iterable.GeneratorComprehension(self, lst_cmp)] call_path = element.generate_call_path() next(call_path, None) # the first one has been used already return self.follow_path(call_path, r, element.parent) elif isinstance(element, pr.ListComprehension): return self.eval_statement(element.stmt) elif isinstance(element, pr.Lambda): return [er.Function(self, element)] # With things like params, these can also be functions... elif isinstance(element, pr.Base) and element.isinstance( er.Function, er.Class, er.Instance, iterable.ArrayInstance): return [element] # The string tokens are just operations (+, -, etc.) elif isinstance(element, compiled.CompiledObject): return [element] elif isinstance(element, Token): return [] else: return self.eval_call(element)
def unpack(self, func=None): named_args = [] for stars, el in self._split(): if stars == 1: arrays = self.context.eval_node(el) iterators = [ _iterate_star_args(self.context, a, el, func) for a in arrays ] iterators = list(iterators) for values in list(zip_longest(*iterators)): # TODO zip_longest yields None, that means this would raise # an exception? yield None, context.get_merged_lazy_context( [v for v in values if v is not None]) elif stars == 2: arrays = self._evaluator.eval_element(self.context, el) for dct in arrays: for key, values in _star_star_dict(self.context, dct, el, func): yield key, values else: if el.type == 'argument': c = el.children if len(c) == 3: # Keyword argument. named_args.append(( c[0].value, context.LazyTreeContext(self.context, c[2]), )) else: # Generator comprehension. # Include the brackets with the parent. comp = iterable.GeneratorComprehension( self._evaluator, self.context, self.argument_node.parent) yield None, context.LazyKnownContext(comp) else: yield None, context.LazyTreeContext(self.context, el) # Reordering var_args is necessary, because star args sometimes appear # after named argument, but in the actual order it's prepended. for named_arg in named_args: yield named_arg
def unpack(self, func=None): named_args = [] for stars, el in self._split(): if stars == 1: arrays = self._evaluator.eval_element(el) iterators = [ _iterate_star_args(self._evaluator, a, el, func) for a in arrays ] iterators = list(iterators) for values in list(zip_longest(*iterators)): yield None, [v for v in values if v is not None] elif stars == 2: arrays = self._evaluator.eval_element(el) dicts = [ _star_star_dict(self._evaluator, a, el, func) for a in arrays ] for dct in dicts: for key, values in dct.items(): yield key, values else: if pr.is_node(el, 'argument'): c = el.children if len(c) == 3: # Keyword argument. named_args.append((c[0].value, (c[2], ))) else: # Generator comprehension. # Include the brackets with the parent. comp = iterable.GeneratorComprehension( self._evaluator, self.argument_node.parent) yield None, (iterable.AlreadyEvaluated([comp]), ) elif isinstance(el, (list, tuple)): yield None, el else: yield None, (el, ) # Reordering var_args is necessary, because star args sometimes appear # after named argument, but in the actual order it's prepended. for key_arg in named_args: yield key_arg