|
32 | 32 | from SimpleDataTypes import * |
33 | 33 | from TypeChecker import * |
34 | 34 |
|
35 | | -def type_from_string(type_str): |
36 | | - """ Look for the type definition in the global scope from the type string. |
37 | | - @TODO: find a better implementation than testing all modules! |
| 35 | +class BaseAggregate(object): |
| 36 | + """ A class that define common properties to ARRAY, LIST, SET and BAG. |
38 | 37 | """ |
39 | | - modules = sys.modules |
40 | | - for module in modules.values(): |
41 | | - if (module is not None) and (not '__' in module.__name__): |
42 | | - module_variables = vars(module) |
43 | | - if module_variables.has_key(type_str): |
44 | | - typ = module_variables[type_str] |
45 | | - return True, vars(module)[type_str] |
46 | | - return False,None |
| 38 | + def __init__( self , bound1 , bound2 , base_type ): |
| 39 | + # check that bound1<bound2 |
| 40 | + if (bound1!=None and bound2!=None): |
| 41 | + if bound1>bound2: |
| 42 | + raise AssertionError("bound1 shall be less than or equal to bound2") |
| 43 | + self._bound1 = bound1 |
| 44 | + self._bound2 = bound2 |
| 45 | + self._base_type = base_type |
| 46 | + |
| 47 | + def __getitem__(self, index): |
| 48 | + if index<self._bound1: |
| 49 | + raise IndexError("ARRAY index out of bound (lower bound is %i, passed %i)"%(self._bound1,index)) |
| 50 | + elif(self._bound2!=None and index>self._bound2): |
| 51 | + raise IndexError("ARRAY index out of bound (upper bound is %i, passed %i)"%(self._bound2,index)) |
| 52 | + else: |
| 53 | + return list.__getitem__(self,index) |
| 54 | + |
| 55 | + def __setitem__(self,index,value): |
| 56 | + if index<self._bound1: |
| 57 | + raise IndexError("ARRAY index out of bound (lower bound is %i, passed %i)"%(self._bound1,index)) |
| 58 | + elif (self._bound2!=None and index>self._bound2): |
| 59 | + raise IndexError("ARRAY index out of bound (upper bound is %i, passed %i)"%(self._bound2,index)) |
| 60 | + elif not isinstance(value,self._base_type): |
| 61 | + raise TypeError("%s type expected, passed %s."%(self._base_type, type(value))) |
| 62 | + else: |
| 63 | + # first find the length of the list, and extend it if ever |
| 64 | + # the index is |
| 65 | + list.__setitem__(self,index,value) |
47 | 66 |
|
48 | 67 | class ARRAY(object): |
49 | 68 | """An array data type has as its domain indexed, fixed-size collections of like elements. The lower |
|
0 commit comments