Package MDSplus :: Module mdsscalar
[frames] | no frames]

Source Code for Module MDSplus.mdsscalar

  1  import numpy,copy 
  2  from types import NotImplementedType 
  3  from mdsdata import * 
  4  from _mdsdtypes import * 
  5   
6 -def makeScalar(value):
7 if isinstance(value,Scalar): 8 return copy.deepcopy(value) 9 if isinstance(value,numpy.generic): 10 if isinstance(value,numpy.string_): 11 return String(value) 12 if isinstance(value,numpy.bool_): 13 return makeScalar(int(value)) 14 else: 15 exec 'ans='+value.__class__.__name__.capitalize()+'(value)' 16 return ans 17 if isinstance(value,int): 18 return Int32(value) 19 if isinstance(value,long): 20 return Int64(value) 21 if isinstance(value,float): 22 return Float32(value) 23 if isinstance(value,str): 24 return String(value) 25 if isinstance(value,bool): 26 return Int8(int(value)) 27 raise TypeError,'Cannot make Scalar out of '+str(type(value))
28
29 -class Scalar(Data):
30
31 - def __new__(cls,value=0):
32 try: 33 import numpy 34 from mdsarray import Array 35 if (isinstance(value,Array)) or isinstance(value,list) or isinstance(value,numpy.ndarray): 36 exec "from mdsarray import "+cls.__name__+"Array" 37 return eval(cls.__name__+"Array(value)") 38 except: 39 pass 40 41 return super(Scalar,cls).__new__(cls,value)
42
43 - def __init__(self,value=0):
44 if self.__class__.__name__ == 'Scalar': 45 raise TypeError,"cannot create 'Scalar' instances" 46 if self.__class__.__name__ == 'String': 47 self._value=numpy.string_(value) 48 return 49 exec 'self._value=numpy.'+self.__class__.__name__.lower()+'(value)'
50
51 - def __getattr__(self,name):
52 exec 'ans=self.value.'+name 53 return ans
54
55 - def _getValue(self):
56 """Return the numpy scalar representation of the scalar""" 57 return self._value
58 59 value=property(_getValue) 60
61 - def __int__(self):
62 """Integer: x.__int__() <==> int(x) 63 @rtype: int""" 64 return self._value.__int__()
65
66 - def __long__(self):
67 """Long: x.__long__() <==> long(x) 68 @rtype: int""" 69 return self.__value.__long__()
70
71 - def _unop(self,op):
72 return makeData(getattr(self.value,op)())
73
74 - def _binop(self,op,y):
75 try: 76 y=y.value 77 except (AttributeError),e: 78 pass 79 ans=getattr(self.value,op)(y) 80 if isinstance(ans,NotImplementedType): 81 raise AttributeError,op+' is not supported for types '+str(type(self.value))+' and '+str(type(y)) 82 return makeData(ans)
83
84 - def _triop(self,op,y,z):
85 try: 86 y=y.value 87 except (AttributeError),e: 88 pass 89 try: 90 z=z.value 91 except (AttributeError),e: 92 pass 93 return makeData(getattr(self.value,op)(y,z))
94
95 - def _getMdsDtypeNum(self):
96 return {'Uint8':DTYPE_BU,'Uint16':DTYPE_WU,'Uint32':DTYPE_LU,'Uint64':DTYPE_QU, 97 'Int8':DTYPE_B,'Int16':DTYPE_W,'Int32':DTYPE_L,'Int64':DTYPE_Q, 98 'String':DTYPE_T, 99 'Float32':DTYPE_FS, 100 'Float64':DTYPE_FT}[self.__class__.__name__]
101 mdsdtype=property(_getMdsDtypeNum) 102 103
104 - def all(self):
105 return self._unop('all')
106
107 - def any(self):
108 return self._unop('any')
109
110 - def argmax(self,*axis):
111 if axis: 112 return self._binop('argmax',axis[0]) 113 else: 114 return self._unop('argmax')
115
116 - def argmin(self,*axis):
117 if axis: 118 return self._binop('argmin',axis[0]) 119 else: 120 return self._unop('argmin')
121
122 - def argsort(self,axis=-1,kind='quicksort',order=None):
123 return makeData(self.value.argsort(axis,kind,order))
124
125 - def astype(self,type):
126 return makeData(self.value.astype(type))
127
128 - def byteswap(self):
129 return self._unop('byteswap')
130
131 - def clip(self,y,z):
132 return self._triop('clip',y,z)
133 134
135 -class Int8(Scalar):
136 """8-bit signed number"""
137
138 -class Int16(Scalar):
139 """16-bit signed number"""
140
141 -class Int32(Scalar):
142 """32-bit signed number"""
143
144 -class Int64(Scalar):
145 """64-bit signed number"""
146
147 -class Uint8(Scalar):
148 """8-bit unsigned number"""
149
150 -class Uint16(Scalar):
151 """16-bit unsigned number"""
152
153 -class Uint32(Scalar):
154 """32-bit unsigned number"""
155
156 -class Uint64(Scalar):
157 """64-bit unsigned number""" 158
159 - def _getDate(self):
160 return Data.execute('date_time($)',self)
161 date=property(_getDate)
162
163 -class Float32(Scalar):
164 """32-bit floating point number"""
165
166 -class Float64(Scalar):
167 """64-bit floating point number"""
168
169 -class String(Scalar):
170 """String"""
171 - def __radd__(self,y):
172 """Reverse add: x.__radd__(y) <==> y+x 173 @rtype: Data""" 174 return self.execute('$//$',y,self)
175 - def __add__(self,y):
176 """Add: x.__add__(y) <==> x+y 177 @rtype: Data""" 178 return self.execute('$//$',self,y)
179 - def __str__(self):
180 """String: x.__str__() <==> str(x) 181 @rtype: String""" 182 return self.value
183
184 -class Int128(Scalar):
185 """128-bit number"""
186 - def __init__(self):
187 raise TypeError,"Int128 is not yet supported"
188
189 -class Uint128(Scalar):
190 """128-bit unsigned number"""
191 - def __init__(self):
192 raise TypeError,"Uint128 is not yet supported"
193