Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.79 KB | None | 0 0
  1. #Usage:
  2. #
  3. #converter = UnitConvert()
  4. #result = converter.convert(Numerical value, current unit, wanted unit)
  5. #
  6. #Result will be a string, e.g. 576.3g
  7.  
  8. class UnitConvert:
  9.  
  10.     allowedMass = ['g','kg']
  11.     allowedForce = ['N']
  12.     gravity = 9.8
  13.     conversion = ""
  14.  
  15.     #For now I am just bastardizing this.
  16.     #Eventually pretty code and awesome conversions will happen.
  17.     def convert(self, amount, uFrom, uTo):
  18.         #Store given information
  19.         self.uFrom = uFrom
  20.         self.uTo = uTo
  21.         #a is amount. not in untis
  22.         self.amount = amount
  23.        
  24.         #Identify Units
  25.         self.identifyConversionType()
  26.  
  27.         #Call proper conversion
  28.         if self.conversion == "mm":
  29.             self.convertMassToMass()
  30.         elif self.conversion == "mf":
  31.             self.convertMassToForce()
  32.         elif selfconversion == "fm":
  33.             self.convertForceToMass()
  34.  
  35.         #Return result as a string to the requesting function
  36.         self.conversion = ""
  37.         return self.result
  38.  
  39.     def identifyConversionType(self):
  40.         #Identify the incoming unit type
  41.         for i in self.allowedMass:
  42.             if self.uFrom == i:
  43.                 self.conversion += "m"
  44.         else:
  45.             for i in self.allowedForce:
  46.                 if self.uFrom == i:
  47.                     self.conversion += "f"
  48.  
  49.         #Identify the outgoing unit type
  50.         for i in self.allowedMass:
  51.             if self.uTo == i:
  52.                 self.conversion += "m"
  53.         else:
  54.             for i in self.allowedForce:
  55.                 if self.uTo == i:
  56.                     self.conversion += "f"
  57.  
  58.     def convertMassToMass(self):
  59.         #First convert from whatever unit to SI unit
  60.         self.convertMassToSIMass(self.uFrom)
  61.         #Then from the SI unit to the resultant unit
  62.         self.convertMassFromSIMass(self.uTo)
  63.         #Generate a string of the result
  64.         self.result = str(self.amount) + self.uTo
  65.  
  66.     def convertMassToForce(self):
  67.         #Force = Mass * Acceleration
  68.         self.amount = self.convertMassToSIMass(self.uFrom) * self.gravity
  69.         #Generate resultant string
  70.         self.result = str(self.amount) + self.uTo
  71.  
  72.     def convertForceToMass(self):
  73.         #Mass = Force / gravity
  74.         self.amount = self.amount / self.gravity
  75.         #Convert the kg to outgoing unti
  76.         self.convertMassFromSIMass(self.uTo)
  77.         #Generate resultant string
  78.         self.result = str(self.amount) + self.uTo
  79.  
  80.     def convertMassToSIMass(self, unit):
  81.         if unit == 'g':
  82.             return self.amount * 0.001
  83.         elif unit == 'kg':
  84.             return self.amount
  85.  
  86.     def convertMassFromSIMass(self, unit):
  87.         if unit == 'kg':
  88.             return self.amount
  89.         if unit == 'g':
  90.             return self.amount * 1000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement