def test_atto_seconds(self): # The unit 'as' was bugged because it is a python keyword. fact = unit_conversion('s', 'as') assert_near_equal(fact[0], 1e18) # Make sure regex for 'as' doesn't pick up partial words. fact = unit_conversion('aslug*as*as', 'aslug*zs*zs') assert_near_equal(fact[0], 1e6) # Make sure simplification works. simple = simplify_unit('m*as/as') self.assertEqual(simple, 'm') simple = simplify_unit('as**6/as**4') self.assertEqual(simple, 'as**2')
def test_unit_simplification(self): test_strings = [ 'ft/s*s', 'm/s*s', 'm * ft * cm / km / m', 's/s', 'm ** 7 / m ** 5' ] correct_strings = ['ft', 'm', 'ft*cm/km', None, 'm**2'] for test_str, correct_str in zip(test_strings, correct_strings): simplified_str = simplify_unit(test_str) self.assertEqual(simplified_str, correct_str)
def get_val(self, name, units=None, indices=None): """ Get an output/input variable. Function is used if you want to specify display units. Parameters ---------- name : str Promoted or relative variable name in the root system's namespace. units : str, optional Units to convert to before upon return. indices : int or list of ints or tuple of ints or int ndarray or Iterable or None, optional Indices or slice to return. Returns ------- float or ndarray The requested output/input variable. """ val = self[name] if indices is not None: val = val[indices] if units is not None: base_units = self._get_units(name) simp_units = simplify_unit(units) if base_units is None: msg = "Can't express variable '{}' with units of 'None' in units of '{}'." raise TypeError(msg.format(name, simp_units)) try: scale, offset = unit_conversion(base_units, simp_units) except TypeError: msg = "Can't express variable '{}' with units of '{}' in units of '{}'." raise TypeError(msg.format(name, base_units, simp_units)) val = (val + offset) * scale return val