forked from pyta-uoft/pyta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_comp.py
67 lines (60 loc) · 1.91 KB
/
set_comp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
SetComp astroid node
This node represents set comprehension expressions in Python, which take the
form {element for increment in iterable}. This node also makes use of the
Comprehension node.
Attributes:
- elt (Expr)
- The part to be evaluated to make every item in the set. This can
be any expression node, such as a BinOp or Str.
- generators (list[Comprehension])
- This list contains one Comprehension node for every "for" clause
in this set comprehension.
Example 1:
SetComp(
elt=BinOp(
op='*',
left=Name(name='x'),
right=Const(value=2)),
generators=[Comprehension(
is_async=0,
target=AssignName(name='x'),
iter=List(
ctx=<Context.Load: 1>,
elts=[Const(value=1)]),
ifs=[])])
Example 2:
SetComp(
elt=BinOp(
op='*',
left=Name(name='x'),
right=Name(name='y')),
generators=[Comprehension(
is_async=0,
target=AssignName(name='x'),
iter=List(
ctx=<Context.Load: 1>,
elts=[
Const(value=0),
Const(value=1),
Const(value=2)]),
ifs=[]),
Comprehension(
is_async=0,
target=AssignName(name='y'),
iter=List(
ctx=<Context.Load: 1>,
elts=[
Const(value=4),
Const(value=3),
Const(value=2)]),
ifs=[Compare(
left=Name(name='x'),
ops=[['<', Name(name='y')]])])])
Type-checking:
The type of the SetComp is set[T], where T is the type of elt.
"""
# Example 1
{x * 2 for x in [1]}
# Example 2
{x * y for x in [0, 1, 2] for y in [4, 3, 2] if x < y}