Dict comprehensions are working partially.

This commit is contained in:
Dave Halter
2015-12-27 17:20:49 +01:00
parent b3f7d0c29a
commit 03eaf8455f
4 changed files with 25 additions and 8 deletions

View File

@@ -199,7 +199,7 @@ class Evaluator(object):
if_stmt = element.get_parent_until((tree.IfStmt, tree.ForStmt, tree.IsScope))
predefined_if_name_dict = self.predefined_if_name_dict_dict.get(if_stmt)
if not predefined_if_name_dict and isinstance(if_stmt, tree.IfStmt):
if predefined_if_name_dict is None and isinstance(if_stmt, tree.IfStmt):
if_stmt_test = if_stmt.children[1]
name_dicts = [{}]
# If we already did a check, we don't want to do it again -> If
@@ -358,8 +358,14 @@ class Evaluator(object):
except (IndexError, AttributeError):
pass
else:
if comp_for.type == 'comp_for' \
or comp_for == ':' and c[1].children[3].type == 'comp_for': # dict
if comp_for == ':':
# Dict comprehensions have it at the 3rd index.
try:
comp_for = c[1].children[3]
except IndexError:
pass
if comp_for.type == 'comp_for':
return set([iterable.Comprehension.from_atom(self, atom)])
return set([iterable.Array(self, atom)])

View File

@@ -124,7 +124,7 @@ class list():
return self.__iterable[y]
def pop(self):
return self.__iterable[-1]
return self.__iterable[int()]
class tuple():

View File

@@ -201,6 +201,13 @@ class SetComprehension(Comprehension, ArrayMixin):
type = 'set'
class DictComprehension(Comprehension, ArrayMixin):
type = 'dict'
def _get_comp_for(self):
return self._get_comprehension().children[3]
class GeneratorComprehension(Comprehension, GeneratorMixin):
pass

View File

@@ -126,10 +126,12 @@ right
# dict comprehensions
# -----------------
##? str()
{a - 1: b for a, b in {1: 'a', 3: 1.0}.items()}[0]
d = {a - 1: b for a, b in {1: 'a', 3: 1.0}.items()}
#? str()
list()[0]
#? int()
{a - 1: 3 for a in [1]}[0]
list({a - 1: 3 for a in [1]})[0]
# -----------------
# set comprehensions
@@ -146,9 +148,11 @@ right
#? int()
{a for a in range(10)}.pop()
#? float() str()
{b for a in [[3.0], ['']] for b in a}.pop()
#? int()
iter({a for a in range(10)}).next()
next(iter({a for a in range(10)}))
# -----------------