Skip to content

Commit

Permalink
cleaned up RPNMeasurement methods, and got rid of twiddleUnits
Browse files Browse the repository at this point in the history
It wasn't needed any more.
  • Loading branch information
ConceptJunkie committed Apr 14, 2019
1 parent 2e1d26b commit 2b00855
Showing 1 changed file with 11 additions and 119 deletions.
130 changes: 11 additions & 119 deletions rpn/rpnMeasurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,7 @@ def add( self, other ):
if self.units == other.units:
return RPNMeasurement( fadd( self.value, other.value ), self.units )
else:
newOther = other.convertValue( self )

return RPNMeasurement( fadd( self.value, newOther ), self.units )
return RPNMeasurement( fadd( self.value, other.convertValue( self ) ), self.units )
else:
return RPNMeasurement( fadd( self.value, other ), self.units )

Expand All @@ -223,67 +221,45 @@ def subtract( self, other ):
if self.units == other.units:
return RPNMeasurement( fsub( self.value, other.value ), self.units )
else:
newOther = other.convertValue( self )
return RPNMeasurement( fsub( self.value, newOther ), self.units )
return RPNMeasurement( fsub( self.value, other.convertValue( self ) ), self.units )

else:
return RPNMeasurement( fsub( self.value, other ), self.units )

def multiply( self, other ):
if isinstance( other, RPNMeasurement ):
newValue = fmul( self.value, other.value )

factor, newUnits = self.units.combineUnits( other.units )

self = RPNMeasurement( fmul( newValue, factor ), newUnits )
return RPNMeasurement( fmul( fmul( self.value, other.value ), factor ), newUnits ).normalizeUnits( )
else:
newValue = fmul( self.value, other )

self = RPNMeasurement( newValue, self.units )

return self.normalizeUnits( )
return RPNMeasurement( fmul( self.value, other ), self.units ).normalizeUnits( )

def divide( self, other ):
if isinstance( other, RPNMeasurement ):
newValue = fdiv( self.value, other.value )

factor, newUnits = self.units.combineUnits( other.units.inverted( ) )

self = RPNMeasurement( fmul( newValue, factor ), newUnits )
return RPNMeasurement( fmul( fdiv( self.value, other.value ), factor ), newUnits ).normalizeUnits( )
else:
newValue = fdiv( self.value, other )

self = RPNMeasurement( newValue, self.units )

return self.normalizeUnits( )
return RPNMeasurement( fdiv( self.value, other ), self.units ).normalizeUnits( )

def getModulo( self, other ):
if isinstance( other, RPNMeasurement ):
measurement = RPNMeasurement( self )

measurement = measurement.convert( other.units )
measurement = RPNMeasurement( self ).convert( other.units )
measurement.value = fmod( measurement.value, other.value )

self = RPNMeasurement( measurement )
return measurement.normalizeUnits( )
else:
self.value = fmod( self.value, other )

return self.normalizeUnits( )
return RPNMeasurement( fmod( self.value, other ), self.units ).normalizeUnits( )

def exponentiate( self, exponent ):
if ( floor( exponent ) != exponent ):
raise ValueError( 'cannot raise a measurement to a non-integral power' )

exponent = int( exponent )

newValue = power( self.value, exponent )

for unit in self.units:
self.units[ unit ] *= exponent

self = RPNMeasurement( newValue, self.units )

return self
return RPNMeasurement( newValue, self.units )

def getRoot( self, operand ):
if ( floor( operand ) != operand ):
Expand Down Expand Up @@ -328,6 +304,7 @@ def getInverted( self, invertValue=True ):

def normalizeUnits( self ):
units = self.units.normalizeUnits( )

debugPrint( )
debugPrint( 'normalize', units )

Expand Down Expand Up @@ -748,26 +725,6 @@ def convertValue( self, other ):

value = conversionValue

# If we can't convert, then let's twiddle the units around and see if we can get it another way.
if not foundConversion:
debugPrint( 'attempting to twiddle the units' )
success, returnValue = self.twiddleUnits( units1, True, units2, False )

if success:
return returnValue

success, returnValue = self.twiddleUnits( units1, False, units2, True )

if success:
return returnValue

success, returnValue = self.twiddleUnits( units1, True, units2, True )

if success:
return returnValue
else:
raise ValueError( 'unable to convert ' + units1.getUnitString( ) + ' to ' + units2.getUnitString( ) )

debugPrint( 'Iterating through conversions...' )

for conversion in conversions:
Expand Down Expand Up @@ -906,71 +863,6 @@ def convertIncompatibleUnit( self, other ):

raise ValueError( 'incompatible units cannot be converted: ' + self.getUnitName( ) + ' and ' + otherUnit )

def twiddleUnits( self, units1, twiddle1, units2, twiddle2 ):
numeratorFound = False
denominatorFound = False

twiddleUnits1 = RPNUnits( units1 )
twiddleUnits2 = RPNUnits( units2 )

debugPrint( '0 twiddleUnits1', twiddleUnits1 )
debugPrint( '0 twiddleUnits2', twiddleUnits2 )

if twiddle1:
for unit in twiddleUnits1:
if twiddleUnits1[ unit ] > 0:
numeratorFound = True
continue

if twiddleUnits1[ unit ] < 0:
denominatorFound = True
exponent = twiddleUnits1[ unit ] * -1

if unit in twiddleUnits2:
twiddleUnits2[ unit ] += exponent
else:
twiddleUnits2[ unit ] = exponent

twiddleUnits1[ unit ] = 0

twiddleUnits1 = RPNUnits( twiddleUnits1 ).normalizeUnits( )
twiddleUnits2 = RPNUnits( twiddleUnits2 ).normalizeUnits( )

debugPrint( '1 twiddleUnits1', twiddleUnits1 )
debugPrint( '1 twiddleUnits2', twiddleUnits2 )

if twiddle2:
for unit in twiddleUnits2:
if twiddleUnits2[ unit ] > 0:
numeratorFound = True
continue

if twiddleUnits2[ unit ] < 0:
denominatorFound = True

if unit in twiddleUnits1:
twiddleUnits1[ unit ] += twiddleUnits2[ unit ] * -1
else:
twiddleUnits1[ unit ] = twiddleUnits2[ unit ] * -1

twiddleUnits2[ unit ] = 0

twiddleUnits1 = RPNUnits( twiddleUnits1 ).normalizeUnits( )
twiddleUnits2 = RPNUnits( twiddleUnits2 ).normalizeUnits( )

debugPrint( '2 twiddleUnits1', twiddleUnits1 )
debugPrint( '2 twiddleUnits2', twiddleUnits2 )

if denominatorFound and numeratorFound:
try:
value = RPNMeasurement( self.value, twiddleUnits1 ). convertValue( twiddleUnits2 )
return True, RPNMeasurement( value, units2 )
except:
return False, None
pass

return False, None


# //******************************************************************************
# //
Expand Down

0 comments on commit 2b00855

Please sign in to comment.