Example #1
0
 def to(self,units):
     if isinstance(units,udunits):
         u=units.units
     else:
         u=units
     s,i=udunits_wrap.convert(self.units,u)
     return udunits(self.value*s+i,u)
Example #2
0
 def how(self,units):
     if isinstance(units,udunits):
         u=units.units
     else:
         u=units
     s,i=udunits_wrap.convert(self.units,u)
     return s,i
Example #3
0
 def _setunits(self,units):
     if self.value is None:
         self._units=units
     elif units!=self.units:
         s,i=udunits_wrap.convert(self.units,units)
         self.value=self.value*s+i
         self._units=units
     return
Example #4
0
 def __rsub__(self,other):
     if not isinstance(other, (udunits,int,long,float)):
         raise "Error must sub a number or a udunit object"
     out=udunits(self.units,self.value)
     if isinstance(other, udunits):
         s,i=udunits_wrap.convert(other.units,self.units)
         out.value=(other.value*s+i)-self.value
     else:
         out.value=other-out.value
     return out
Example #5
0
 def __radd__(self,other):
     if not isinstance(other, (udunits,int,long,float)):
         raise "Error must add a number or a udunit object"
     out=udunits(self.units,self.value)
     if isinstance(other, udunits):
         s,i=udunits_wrap.convert(other.units,self.units)
         out.value=self.value+other.value*s+i
     else:
         out.value+=other
     return out
Example #6
0
 def __div__(self,other):
     if not isinstance(other, (udunits,int,long,float)):
         raise "Error must divide by a number or a udunit object"
     out=udunits(self.units,self.value)
     if isinstance(other, udunits):
         try:
             s,i=udunits_wrap.convert(other.units,self.units)
             out=self.value/(other.value*s+i)
         except: #Ok uncompatible units, just do the produce
             out=udunits(self.value/other.value,self.units+'/'+other.units)
     else:
         out.value/=other
     return out
Example #7
0
 def __rmul__(self,other):
     if not isinstance(other, (udunits,int,long,float)):
         raise "Error must multiply a number or a udunit object"
     out=udunits(self.units+'*'+self.units,self.value)
     if isinstance(other, udunits):
         try:
             s,i=udunits_wrap.convert(other.units,self.units)
             out.value=self.value*(other.value*s+i)
         except: #Ok uncompatible units, just do the produce
             out=udunits(self.units+'*'+other.units,self.value*other.value)
     else:
         out = udunits(other*self.value,self.units)
     return out