Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
Pyston
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
Pyston
Commits
6b26b471
Commit
6b26b471
authored
Mar 10, 2015
by
Michael Arntzenius
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update tests for {dict,set}comps
parent
b890bb6b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
128 additions
and
18 deletions
+128
-18
test/tests/dictcomp.py
test/tests/dictcomp.py
+45
-18
test/tests/setcomp.py
test/tests/setcomp.py
+83
-0
No files found.
test/tests/dictcomp.py
View file @
6b26b471
...
...
@@ -13,24 +13,28 @@ def f():
# print i, j
f
()
def
f2
(
x
):
print
dict2str
({
x
:
i
for
i
in
[
x
]})
print
dict2str
({
i
:
i
for
i
in
[
x
]})
f2
(
7
)
# Combine a
lis
t comprehension with a bunch of other control-flow expressions:
def
f
(
x
,
y
):
# Combine a
dic
t comprehension with a bunch of other control-flow expressions:
def
f
3
(
x
,
y
):
# TODO make sure to use an 'if' in a comprehension where the if contains control flow
print
dict2str
({
y
if
i
%
3
else
y
**
2
+
i
:
(
i
if
i
%
2
else
i
/
2
)
for
i
in
(
xrange
(
4
if
x
else
5
)
if
y
else
xrange
(
3
))})
f
(
0
,
0
)
f
(
0
,
1
)
f
(
1
,
0
)
f
(
1
,
1
)
f
3
(
0
,
0
)
f
3
(
0
,
1
)
f
3
(
1
,
0
)
f
3
(
1
,
1
)
# TODO: test on ifs
def
f
():
def
f
4
():
print
dict2str
({
i
:
j
for
(
i
,
j
)
in
sorted
({
1
:
2
,
3
:
4
,
5
:
6
,
7
:
8
}.
items
())})
f
()
f
4
()
# The expr should not get evaluated if the if-condition fails:
def
f
():
def
f
5
():
def
p
(
i
):
print
i
return
i
**
2
...
...
@@ -39,21 +43,22 @@ def f():
return
i
*
4
+
i
print
dict2str
({
k
(
i
):
p
(
i
)
for
i
in
xrange
(
50
)
if
i
%
5
==
0
if
i
%
3
==
0
})
f
()
f
5
()
def
f
():
def
f
6
():
print
dict2str
({
i
:
j
for
i
in
xrange
(
4
)
for
j
in
xrange
(
i
)})
f
()
f
6
()
def
f
():
def
f
7
():
j
=
1
# The 'if' part of this list comprehension references j;
# the first time through it will use the j above, but later times
# it may-or-may-not use the j from the inner part of the listcomp.
print
dict2str
({
i
:
j
for
i
in
xrange
(
7
)
if
i
%
2
!=
j
%
2
for
j
in
xrange
(
i
)})
f
()
# XXX: why is this here? if we un-indent this line, python raises an exception
f7
()
def
f
():
def
f
8
():
# Checking the order of evaluation of the if conditions:
def
c1
(
x
):
...
...
@@ -65,8 +70,30 @@ def f():
return
x
%
3
==
0
print
dict2str
({
i
:
i
for
i
in
xrange
(
20
)
if
c1
(
i
)
if
c2
(
i
)})
f
()
f8
()
def
f9
():
# checking that dictcomps don't contaminate our scope like listcomps do
print
dict2str
({
i
:
j
for
i
,
j
in
[(
1
,
2
)]})
try
:
print
i
except
NameError
as
e
:
print
e
try
:
print
j
except
NameError
as
e
:
print
e
print
dict2str
({
1
:
2
for
x
in
xrange
(
4
)
for
y
in
xrange
(
5
)})
try
:
print
x
except
NameError
as
e
:
print
e
try
:
print
y
except
NameError
as
e
:
print
e
f9
()
def
f10
():
x
=
'for'
y
=
'eva'
print
dict2str
({
i
:
j
for
i
in
[
x
for
x
in
xrange
(
6
)]
for
j
in
[
y
for
y
in
[
1
]]})
print
x
,
y
f10
()
def
control_flow_in_
lis
tcomp
():
def
control_flow_in_
dic
tcomp
():
print
dict2str
({(
i
**
2
if
i
>
5
else
i
**
2
*
-
1
):(
i
if
i
else
-
1
)
for
i
in
(
xrange
(
10
)
if
True
else
[])
if
(
i
%
2
==
0
or
i
%
3
!=
0
)})
control_flow_in_
lis
tcomp
()
control_flow_in_
dic
tcomp
()
test/tests/setcomp.py
0 → 100644
View file @
6b26b471
# this file is adapted from dictcomp.py
def
set2str
(
s
):
# set isn't guaranteed to keep things in sorted order (although in CPython it does AFAICT)
# https://docs.python.org/2/library/stdtypes.html#types-set
return
'{%s}'
%
', '
.
join
(
repr
(
x
)
for
x
in
sorted
(
s
))
print
set2str
({
i
for
i
in
xrange
(
4
)})
print
set2str
({(
i
,
j
)
for
i
in
xrange
(
4
)
for
j
in
xrange
(
4
)})
def
f
():
print
set2str
({
i
:
j
for
i
in
range
(
4
)
for
j
in
range
(
4
)})
f
()
def
f2
(
x
):
print
set2str
({(
x
,
i
)
for
i
in
[
x
]})
print
set2str
({
i
for
i
in
[
x
]})
f2
(
7
)
# Combine a set comprehension with a bunch of other control-flow expressions:
def
f3
(
x
,
y
):
print
set2str
({(
y
if
i
%
3
else
y
**
2
+
i
,
i
if
i
%
2
else
i
/
2
)
for
i
in
(
xrange
(
4
if
x
else
5
)
if
y
else
xrange
(
3
))})
f3
(
0
,
0
)
f3
(
0
,
1
)
f3
(
1
,
0
)
f3
(
1
,
1
)
def
f4
():
print
set2str
({(
i
,
j
)
for
i
,
j
in
sorted
({
1
:
2
,
3
:
4
,
5
:
6
,
7
:
8
}.
items
())})
f4
()
# The expr should not get evaluated if the if-condition fails:
def
f5
():
def
p
(
i
):
print
i
return
i
**
2
def
k
(
i
):
print
i
return
i
*
4
+
i
print
set2str
({(
k
(
i
),
p
(
i
))
for
i
in
xrange
(
50
)
if
i
%
5
==
0
if
i
%
3
==
0
})
f5
()
def
f6
():
print
set2str
({(
i
,
j
)
for
i
in
xrange
(
4
)
for
j
in
xrange
(
i
)})
f6
()
def
f8
():
# Checking the order of evaluation of the if conditions:
def
c1
(
x
):
print
"c1"
,
x
return
x
%
2
==
0
def
c2
(
x
):
print
"c2"
,
x
return
x
%
3
==
0
print
set2str
({
i
for
i
in
xrange
(
20
)
if
c1
(
i
)
if
c2
(
i
)})
f8
()
def
f9
():
# checking that setcomps don't contaminate our scope like listcomps do
print
set2str
({
i
for
i
in
[
1
]})
try
:
print
i
except
NameError
as
e
:
print
e
print
set2str
({
1
for
x
in
xrange
(
4
)
for
y
in
xrange
(
5
)})
try
:
print
x
except
NameError
as
e
:
print
e
try
:
print
y
except
NameError
as
e
:
print
e
f9
()
def
f10
():
x
=
'for'
y
=
'eva'
print
set2str
({(
i
,
j
)
for
i
in
[
x
for
x
in
xrange
(
6
)]
for
j
in
[
y
for
y
in
xrange
(
3
)]})
print
x
,
y
f10
()
def
control_flow_in_setcomp
():
print
set2str
({(
i
**
2
if
i
>
5
else
i
**
2
*
-
1
,
i
if
i
else
-
1
)
for
i
in
(
xrange
(
10
)
if
True
else
[])
if
(
i
%
2
==
0
or
i
%
3
!=
0
)})
control_flow_in_setcomp
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment