13
13
from pyccel .ast .datatypes import NativeVoid , NativeGeneric , NativeHomogeneousList
14
14
from pyccel .ast .internals import PyccelInternalFunction
15
15
16
-
17
16
__all__ = ('ListAppend' ,
18
17
'ListClear' ,
18
+ 'ListExtend' ,
19
19
'ListInsert' ,
20
+ 'ListMethod' ,
20
21
'ListPop' ,
21
22
)
22
23
23
24
#==============================================================================
24
- class ListAppend (PyccelInternalFunction ):
25
+ class ListMethod (PyccelInternalFunction ):
26
+ """
27
+ Abstract class for list method calls.
28
+
29
+ A subclass of this base class represents calls to a specific list
30
+ method.
31
+
32
+ Parameters
33
+ ----------
34
+ list_obj : TypedAstNode
35
+ The object which the method is called from.
36
+
37
+ *args : TypedAstNode
38
+ The arguments passed to list methods.
39
+ """
40
+ __slots__ = ("_list_obj" ,)
41
+ _attribute_nodes = ("_list_obj" ,)
42
+ name = None
43
+ def __init__ (self , list_obj , * args ):
44
+ self ._list_obj = list_obj
45
+ super ().__init__ (* args )
46
+
47
+ @property
48
+ def list_obj (self ):
49
+ """
50
+ Get the object representing the list.
51
+
52
+ Get the object representing the list.
53
+ """
54
+ return self ._list_obj
55
+
56
+ #==============================================================================
57
+ class ListAppend (ListMethod ):
25
58
"""
26
59
Represents a call to the .append() method.
27
60
@@ -36,14 +69,13 @@ class ListAppend(PyccelInternalFunction):
36
69
37
70
Parameters
38
71
----------
39
- list_variable : Variable
40
- The variable representing the list .
72
+ list_obj : TypedAstNode
73
+ The list object which the method is called from .
41
74
42
75
new_elem : TypedAstNode
43
76
The argument passed to append() method.
44
77
"""
45
- __slots__ = ("_list_variable" , "_append_arg" )
46
- _attribute_nodes = ("_list_variable" , "_append_arg" )
78
+ __slots__ = ()
47
79
_dtype = NativeVoid ()
48
80
_shape = None
49
81
_order = None
@@ -52,104 +84,70 @@ class ListAppend(PyccelInternalFunction):
52
84
_class_type = NativeVoid ()
53
85
name = 'append'
54
86
55
- def __init__ (self , list_variable , new_elem ) -> None :
87
+ def __init__ (self , list_obj , new_elem ) -> None :
56
88
is_homogeneous = (
57
89
new_elem .dtype is not NativeGeneric () and
58
- list_variable .dtype is not NativeGeneric () and
59
- list_variable .dtype == new_elem .dtype and
60
- list_variable .precision == new_elem .precision and
61
- list_variable .rank - 1 == new_elem .rank
62
- )
90
+ list_obj .dtype is not NativeGeneric () and
91
+ list_obj .dtype == new_elem .dtype and
92
+ list_obj .precision == new_elem .precision and
93
+ list_obj .rank - 1 == new_elem .rank
94
+ )
63
95
if not is_homogeneous :
64
96
raise TypeError ("Expecting an argument of the same type as the elements of the list" )
65
- self ._list_variable = list_variable
66
- self ._append_arg = new_elem
67
- super ().__init__ ()
68
-
69
- @property
70
- def list_variable (self ):
71
- """
72
- Get the variable representing the list.
73
-
74
- Get the variable representing the list.
75
- """
76
- return self ._list_variable
77
-
78
- @property
79
- def append_argument (self ):
80
- """
81
- Get the argument which is passed to append().
82
-
83
- Get the argument which is passed to append().
84
- """
85
- return self ._append_arg
97
+ super ().__init__ (list_obj , new_elem )
86
98
87
99
#==============================================================================
88
- class ListPop (PyccelInternalFunction ) :
100
+ class ListPop (ListMethod ) :
89
101
"""
90
102
Represents a call to the .pop() method.
91
103
92
104
Represents a call to the .pop() method which
93
105
removes the item at the specified index.
94
106
The method also returns the removed item.
95
107
108
+ >>> [1, 2].pop()
109
+ 2
110
+
96
111
Parameters
97
112
----------
98
- list_variable : TypedAstNode
99
- The name of the list .
113
+ list_obj : TypedAstNode
114
+ The list object which the method is called from .
100
115
101
116
index_element : TypedAstNode
102
117
The current index value for the element to be popped.
103
118
"""
104
- __slots__ = ('_dtype' ,'_precision' , '_index' ,'_list_variable' )
105
- _attribute_nodes = ('_index' ,'_list_variable' )
106
- _rank = 0
107
- _order = None
108
- _shape = None
119
+ __slots__ = ('_dtype' ,'_precision' , '_rank' , '_shape' , '_order' )
109
120
_class_type = NativeHomogeneousList ()
110
121
name = 'pop'
111
122
112
- def __init__ (self , list_variable , index_element = None ):
113
- self ._index = index_element
114
- self ._list_variable = list_variable
115
- self ._dtype = list_variable .dtype
116
- self ._precision = list_variable .precision
117
- super ().__init__ ()
118
-
119
- @property
120
- def pop_index (self ):
121
- """
122
- The current index value for the element to be popped.
123
-
124
- The current index value for the element to be popped.
125
- """
126
- return self ._index
127
-
128
- @property
129
- def list_variable (self ):
130
- """
131
- Provide the name of the list as the return value.
132
-
133
- Provide the name of the list as the return value.
134
- """
135
- return self ._list_variable
123
+ def __init__ (self , list_obj , index_element = None ) -> None :
124
+ self ._rank = list_obj .rank - 1
125
+ self ._dtype = list_obj .dtype
126
+ self ._precision = list_obj .precision
127
+ self ._shape = (None if len (list_obj .shape ) == 1 else tuple (list_obj .shape [1 :]))
128
+ self ._order = (None if self ._shape is None or len (self ._shape ) == 1 else list_obj .order )
129
+ super ().__init__ (list_obj , index_element )
136
130
137
131
#==============================================================================
138
- class ListClear (PyccelInternalFunction ) :
132
+ class ListClear (ListMethod ) :
139
133
"""
140
134
Represents a call to the .clear() method.
141
135
142
136
Represents a call to the .clear() method which deletes all elements from a list,
143
137
effectively turning it into an empty list.
144
138
Note that the .clear() method doesn't return any value.
145
139
140
+ >>> a = [1, 2]
141
+ >>> a.clear()
142
+ >>> print(a)
143
+ []
144
+
146
145
Parameters
147
146
----------
148
- list_variable : TypedAstNode
149
- The name of the list .
147
+ list_obj : TypedAstNode
148
+ The list object which the method is called from .
150
149
"""
151
- __slots__ = ('_list_variable' ,)
152
- _attribute_nodes = ('_list_variable' ,)
150
+ __slots__ = ()
153
151
_dtype = NativeVoid ()
154
152
_precision = - 1
155
153
_rank = 0
@@ -158,21 +156,11 @@ class ListClear(PyccelInternalFunction) :
158
156
_class_type = NativeVoid ()
159
157
name = 'clear'
160
158
161
- def __init__ (self , list_variable ):
162
- self ._list_variable = list_variable
163
- super ().__init__ ()
164
-
165
- @property
166
- def list_variable (self ):
167
- """
168
- Provide the name of the list as the return value.
169
-
170
- Provide the name of the list as the return value.
171
- """
172
- return self ._list_variable
159
+ def __init__ (self , list_obj ) -> None :
160
+ super ().__init__ (list_obj )
173
161
174
162
#==============================================================================
175
- class ListInsert (PyccelInternalFunction ):
163
+ class ListInsert (ListMethod ):
176
164
"""
177
165
Represents a call to the .insert() method.
178
166
@@ -188,17 +176,16 @@ class ListInsert(PyccelInternalFunction):
188
176
189
177
Parameters
190
178
----------
191
- list_variable : Variable
192
- The variable representing the list .
179
+ list_obj : TypedAstNode
180
+ The list object which the method is called from .
193
181
194
182
index : TypedAstNode
195
183
The index value for the element to be added.
196
184
197
185
new_elem : TypedAstNode
198
186
The argument passed to insert() method.
199
187
"""
200
- __slots__ = ("_index" , "_list_variable" , "_insert_arg" )
201
- _attribute_nodes = ("_index" , "_list_variable" , "_insert_arg" )
188
+ __slots__ = ()
202
189
_dtype = NativeVoid ()
203
190
_shape = None
204
191
_order = None
@@ -207,44 +194,49 @@ class ListInsert(PyccelInternalFunction):
207
194
_class_type = NativeVoid ()
208
195
name = 'insert'
209
196
210
- def __init__ (self , list_variable , index , new_elem ) -> None :
197
+ def __init__ (self , list_obj , index , new_elem ) -> None :
211
198
is_homogeneous = (
212
199
new_elem .dtype is not NativeGeneric () and
213
- list_variable .dtype is not NativeGeneric () and
214
- list_variable .dtype == new_elem .dtype and
215
- list_variable .precision == new_elem .precision and
216
- list_variable .rank - 1 == new_elem .rank
200
+ list_obj .dtype is not NativeGeneric () and
201
+ list_obj .dtype == new_elem .dtype and
202
+ list_obj .precision == new_elem .precision and
203
+ list_obj .rank - 1 == new_elem .rank
217
204
)
218
205
if not is_homogeneous :
219
206
raise TypeError ("Expecting an argument of the same type as the elements of the list" )
220
- self ._index = index
221
- self ._list_variable = list_variable
222
- self ._insert_arg = new_elem
223
- super ().__init__ ()
224
-
225
- @property
226
- def index (self ):
227
- """
228
- Index in which the element will be added.
229
-
230
- Index in which the element will be added.
231
- """
232
- return self ._index
207
+ super ().__init__ (list_obj , index , new_elem )
233
208
234
- @property
235
- def list_variable (self ):
236
- """
237
- Get the variable representing the list.
209
+ #==============================================================================
210
+ class ListExtend (ListMethod ):
211
+ """
212
+ Represents a call to the .extend() method.
213
+
214
+ Represents a call to the .extend() method of an object with a list type,
215
+ which adds items of an iterable (list, tuple, dictionary, etc) at the end
216
+ of a list.
217
+ This method is handled through the call to `_visit_ListExtend` in
218
+ the semantic stage. It then attempts to construct a `For` loop node with
219
+ a body that calls `append()`, or direct `append()` nodes depending on
220
+ the type of the iterable passed to `extend()`.
221
+ This class should never be instantiated; it's only purpose is to help
222
+ construct the annotation_method `_visit_ListExtend`.
223
+ The extend method is called as follows:
224
+
225
+ >>> a = [1, 2, 3]
226
+ >>> a.extend(range(4, 8))
227
+ >>> print(a)
228
+ [1, 2, 3, 4, 5, 6, 7]
238
229
239
- Get the variable representing the list.
240
- """
241
- return self ._list_variable
230
+ Parameters
231
+ ----------
232
+ list_obj : TypedAstNode
233
+ The list object which the method is called from.
242
234
243
- @property
244
- def insert_argument (self ):
245
- """
246
- Get the argument which is passed to insert().
235
+ iterable : TypedAstNode
236
+ The argument passed to extend() method.
237
+ """
238
+ __slots__ = ()
239
+ name = 'extend'
247
240
248
- Get the argument which is passed to insert().
249
- """
250
- return self ._insert_arg
241
+ def __init__ (self , list_obj , iterable ) -> None :
242
+ super ().__init__ (list_obj , iterable )
0 commit comments