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)
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
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
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
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
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
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