Skip to content

Commit 50b9032

Browse files
committed
LIST better implementation
1 parent d567959 commit 50b9032

3 files changed

Lines changed: 56 additions & 7 deletions

File tree

src/fedex_python/python/SCL/AggregationDataTypes.py

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,64 @@ def __setitem__(self, index, value):
126126
raise AssertionError("UNIQUE keyword prevent inserting this instance.")
127127
self._container[index-self._bound_1] = value
128128

129-
class LIST(list, BaseAggregate):
129+
class LIST(object):
130130
"""A list data type has as its domain sequences of like elements. The optional lower and upper
131131
bounds, which are integer-valued expressions, define the minimum and maximum number of
132132
elements that can be held in the collection defined by a list data type.
133133
A list data type
134134
definition may optionally specify that a list value cannot contain duplicate elements.
135135
"""
136-
def __init__( self , bound1 , bound2 , base_type ):
137-
BaseAggregate.__init__( self , bound1 , bound2 , base_type )
136+
def __init__( self , bound_1 , bound_2 , base_type , UNIQUE = False):
137+
if not type(bound_1)==int:
138+
raise TypeError("LIST lower bound must be an integer")
139+
# bound_2 can be set to None
140+
if not type(bound_2)==int:
141+
raise TypeError("LIST upper bound must be an integer")
142+
if not bound_1>=0:
143+
raise AssertionError("LIST lower bound must be greater of equal to 0")
144+
if not (bound_1 <= bound_2):
145+
raise AssertionError("ARRAY lower bound must be less than or equal to upper bound")
146+
# set up class attributes
147+
self._bound_1 = bound_1
148+
self._bound_2 = bound_2
149+
self._base_type = base_type
150+
self._unique = UNIQUE
151+
self._optional = OPTIONAL
152+
# preallocate list elements
153+
list_size = bound_2 - bound_1 + 1
154+
self._container = list_size*[None]
138155

156+
def bound_1(self):
157+
return self._bound_1
158+
159+
def bound_2(self):
160+
return self._bound_2
161+
162+
def __getitem__(self, index):
163+
if index<self._bound_1:
164+
raise IndexError("ARRAY index out of bound (lower bound is %i, passed %i)"%(self._bound_1,index))
165+
elif(index>self._bound_2):
166+
raise IndexError("ARRAY index out of bound (upper bound is %i, passed %i)"%(self._bound_2,index))
167+
else:
168+
value = self._container[index-self._bound_1]
169+
if not self._optional and value==None:
170+
raise AssertionError("Not OPTIONAL prevent the value with index %i from being None (default). Please set the value first."%index)
171+
return value
172+
173+
def __setitem__(self, index, value):
174+
if index<self._bound_1:
175+
raise IndexError("ARRAY index out of bound (lower bound is %i, passed %i)"%(self._bound_1,index))
176+
elif(index>self._bound_2):
177+
raise IndexError("ARRAY index out of bound (upper bound is %i, passed %i)"%(self._bound_2,index))
178+
else:
179+
# first check the type of the value
180+
check_type(value,self._base_type)
181+
# then check if the value is already in the array
182+
if self._unique:
183+
if value in self._container:
184+
raise AssertionError("UNIQUE keyword prevent inserting this instance.")
185+
self._container[index-self._bound_1] = value
186+
139187
class BAG(tuple, BaseAggregate):
140188
"""A bag data type has as its domain unordered collections of like elements. The optional lower
141189
and upper bounds, which are integer-valued expressions, de ne the minimum and maximum
-650 Bytes
Binary file not shown.

src/fedex_python/python/SCL/TypeChecker.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@
3333

3434
RAISE_EXCEPTION_IF_TYPE_DOES_NOT_MATCH = False
3535

36-
def cast_python_list_to_aggregate(lst, aggregate):
37-
""" This function casts a python list to an aggregate type. For instance:
36+
def cast_python_object_to_aggregate(obj, aggregate):
37+
""" This function casts a python object to an aggregate type. For instance:
3838
[1.,2.,3.]-> ARRAY(1,3,REAL)"""
3939
aggregate_lower_bound = aggregate.bound_1()
4040
aggregate_upper_bound = aggregate.bound_2()
41-
for idx in range(aggregate_lower_bound,aggregate_upper_bound+1):
42-
aggregate[idx] = lst[idx-aggregate_lower_bound]
41+
if type(obj)==list:
42+
for idx in range(aggregate_lower_bound,aggregate_upper_bound+1):
43+
aggregate[idx] = obj[idx-aggregate_lower_bound]
4344
return aggregate
4445

4546
def check_type(instance, expected_type):

0 commit comments

Comments
 (0)