Example #1
0
 def test_01(self):
     """TestUnitsBasic.test_01(): 1e5 random units converted to and fro and tested to 10 Sig. Fig."""
     myCats = Units.unitCategories()
     cCount = 0
     random.seed()
     tS = time.clock()
     while cCount < 1e2:
         # Choose a category at random
         iC = random.randint(0, len(myCats) - 1)
         myUnits = Units.units(myCats[iC])
         #print 'Category: %s' % myCats[iC]
         #print '   Units: %s' % ', '.join(myUnits)
         uCount = 0
         while uCount < 1e3:
             iU_1 = random.randint(0, len(myUnits) - 1)
             iU_2 = random.randint(0, len(myUnits) - 1)
             val = random.random()
             newVal = Units.convert(val, myUnits[iU_1], myUnits[iU_2])
             oldVal = Units.convert(newVal, myUnits[iU_2], myUnits[iU_1])
             #print
             #print(oldVal)
             self.assertAlmostEqual(oldVal, val, places=10)
             #self.assertAlmostEqual(oldVal, val)
             uCount += 1
         cCount += 1
     tE = time.clock() - tS
     sys.stderr.write('Time: %8.3f rate %10.3f k/S ' %
                      (tE, (cCount * uCount) / (1024 * tE)))
Example #2
0
 def test_03(self):
     """TestUnitsBasic.test_03(): 1e5  fixed units converted to and fro and tested to 10 Sig. Fig."""
     myCats = Units.unitCategories()
     cCount = 0
     random.seed()
     tS = time.perf_counter()
     while cCount < 1e2:
         # Choose a category at random
         iC = random.randint(0, len(myCats) - 1)
         myUnits = Units.units(myCats[iC])
         iU_1 = random.randint(0, len(myUnits) - 1)
         iU_2 = random.randint(0, len(myUnits) - 1)
         val = random.random()
         uCount = 0
         while uCount < 1e3:
             newVal = Units.convert(val, myUnits[iU_1], myUnits[iU_2])
             self.assertAlmostEqual(
                 Units.convert(newVal, myUnits[iU_2], myUnits[iU_1]),
                 val,
                 places=10,
             )
             uCount += 1
         cCount += 1
     tE = time.perf_counter() - tS
     sys.stderr.write('Time: %8.3f rate %10.3f k/S ' %
                      (tE, (cCount * uCount) / (1024 * tE)))
Example #3
0
 def test_04(self):
     """TestUnitsBasic.test_04(): 1e5  fixed units converted to and fro untested."""
     myCats = Units.unitCategories()
     cCount = 0
     random.seed()
     tS = time.perf_counter()
     while cCount < 1e1:
         # Choose a category at random
         iC = random.randint(0, len(myCats) - 1)
         myUnits = Units.units(myCats[iC])
         iU_1 = random.randint(0, len(myUnits) - 1)
         iU_2 = random.randint(0, len(myUnits) - 1)
         val = random.random()
         uCount = 0
         while uCount < 1e4:
             newVal = Units.convert(val, myUnits[iU_1], myUnits[iU_2])
             uCount += 1
         cCount += 1
     tE = time.perf_counter() - tS
     sys.stderr.write('Time: %8.3f rate %10.3f k/S ' %
                      (tE, (cCount * uCount) / (1024 * tE)))
Example #4
0
def randomUnit():
    """Returns a random unit mnemonic."""
    return random.choice(Units.units(theCat=None))