Exemple #1
0
    def _validate_with_metadata(self, obj, name, value, src_units):
        """Perform validation and unit conversion using metadata from
        the source trait.
        """

        # pylint: disable=E1101
        dst_units = self.units

        try:
            pq = PhysicalQuantity(1.0, src_units)
        except NameError:
            raise NameError("while setting value of %s: undefined unit '%s'" %
                            (src_units, name))

        try:
            pq.convert_to_unit(dst_units)
        except NameError:
            raise NameError("undefined unit '%s' for variable '%s'" %
                            (dst_units, name))
        except TypeError:
            msg = "%s: units '%s' are incompatible " % (name, src_units) + \
                   "with assigning units of '%s'" % (dst_units)
            raise TypeError(msg)

        value *= pq.value

        return value
    def _validate_with_metadata(self, obj, name, value, src_units):
        """Perform validation and unit conversion using metadata from
        the source trait.
        """

        # pylint: disable=E1101
        dst_units = self.units

        if isinstance(value, UncertainDistribution):
            value = value.getvalue()

        # FIXME: The try blocks testing whether the unit is bogus or undefined
        # are generally redundant because that test is done at creation. HOWEVER
        # you might have a case where it wasn't tested because it's technically
        # not a float. NPSS wrapper may be such a case. A test needs to be
        # constructed to test these lines.

        try:
            pq = PhysicalQuantity(value, src_units)
        except NameError:
            raise NameError("while setting value of %s: undefined unit '%s'" %
                             (src_units, name))

        try:
            pq.convert_to_unit(dst_units)
        except NameError:
            raise NameError("undefined unit '%s' for variable '%s'" %
                             (dst_units, name))
        except TypeError:
            msg = "%s: units '%s' are incompatible " % (name, src_units) + \
                   "with assigning units of '%s'" % (dst_units)
            raise TypeError(msg)

        return pq.value
Exemple #3
0
    def _validate_with_metadata(self, obj, name, value, src_units):
        """Perform validation and unit conversion using metadata from
        the source trait.
        """
        
        # pylint: disable-msg=E1101
        dst_units = self.units

        try:
            pq = PhysicalQuantity(1.0, src_units)
        except NameError:
            raise NameError("while setting value of %s: undefined unit '%s'" %
                            (src_units, name))
        
        try:
            pq.convert_to_unit(dst_units)
        except NameError:
            raise NameError("undefined unit '%s' for variable '%s'" %
                            (dst_units, name))
        except TypeError:
            msg = "%s: units '%s' are incompatible " % (name, src_units) + \
                   "with assigning units of '%s'" % (dst_units)
            raise TypeError(msg)
        
        try:
            value *= pq.value
            return super(Array, self).validate(obj, name, value)
        except Exception:
            self.error(obj, name, value)
Exemple #4
0
    def _validate_with_metadata(self, obj, name, value, src_units):
        """Perform validation and unit conversion using metadata from
        the source trait.
        """

        # pylint: disable=E1101
        dst_units = self.units

        if isinstance(value, UncertainDistribution):
            value = value.getvalue()

        # FIXME: The try blocks testing whether the unit is bogus or undefined
        # are generally redundant because that test is done at creation. HOWEVER
        # you might have a case where it wasn't tested because it's technically
        # not a float. NPSS wrapper may be such a case. A test needs to be
        # constructed to test these lines.

        try:
            pq = PhysicalQuantity(value, src_units)
        except NameError:
            raise NameError("while setting value of %s: undefined unit '%s'" %
                            (src_units, name))

        try:
            pq.convert_to_unit(dst_units)
        except NameError:
            raise NameError("undefined unit '%s' for variable '%s'" %
                            (dst_units, name))
        except TypeError:
            msg = "%s: units '%s' are incompatible " % (name, src_units) + \
                   "with assigning units of '%s'" % (dst_units)
            raise TypeError(msg)

        return pq.value
Exemple #5
0
 def test_pi(self):
     # Fixes issue 786
     x = PhysicalQuantity('1rpm')
     x.convert_to_unit('rad/min')
     self.assertAlmostEqual(x.value,
                            PhysicalQuantity('6.283185rad/min').value,
                            places=3)
Exemple #6
0
 def test_new_units(self):
     # Hour added to test problem in Classic OpenMDAO Ticket 466
     # knot, rev, month added to test problem in Issue 804
     x = PhysicalQuantity('7200s')
     x.convert_to_unit('h')
     self.assertEqual(x, PhysicalQuantity('2h'))
     x = PhysicalQuantity('5knot')
     x.convert_to_unit('nm/h')
     self.assertEqual(x, PhysicalQuantity('5nmi/h'))
     x = PhysicalQuantity('33rev/min')
     x.convert_to_unit('rpm')
     self.assertEqual(x, PhysicalQuantity('33rpm'))
     x = PhysicalQuantity('12mo')
     x.convert_to_unit('yr')
     self.assertEqual(x, PhysicalQuantity('1yr'))
     x = PhysicalQuantity('1Mibyte')
     x.convert_to_unit('Kibyte')
     self.assertEqual(x, PhysicalQuantity('1024Kibyte'))
Exemple #7
0
    def test_convert_to_unit(self):
        #convert_to_unit should change the unit of the calling instance to the requested new unit
        x = PhysicalQuantity('5cm')
        x.convert_to_unit('m')
        self.assertEqual(x, PhysicalQuantity('0.05m'))

        #Test for no compatible units
        x = PhysicalQuantity('5cm')
        try:
            x.convert_to_unit('kg')
        except TypeError as err:
            self.assertEqual(str(err), 'Incompatible units')
        else:
            self.fail("TypeError expected")

        x = PhysicalQuantity('1.0psi')
        x.convert_to_unit('psf')
        self.assertEqual(x, PhysicalQuantity('144.0psf'))