Beispiel #1
0
 def _get_display_units(self, quantity):
     if self.factory.display_units is None:
         du = unit_manager.default_units_for(
             self._get_family_name(quantity))
     else:
         du = self.factory.display_units
     return du
Beispiel #2
0
 def _get_display_units(self, quantity):
     if self.factory.display_units is None:
         du = unit_manager.default_units_for(
             self._get_family_name(quantity))
     else:
         du = self.factory.display_units
     return du
Beispiel #3
0
 def validate(self, object, name, value):
     try:
         if isinstance(value, Quantity):
             if value.family_name == self.family_name:
                 return value
         else:
             if self.display_units is None:
                 units = unit_manager.default_units_for(self.family_name)
             else:
                 units = self.display_units
             return Quantity(value, units=units,
                             family_name=self.family_name)
     except:
         pass
     self.error(object, name, self.repr(value))
Beispiel #4
0
 def validate ( self, object, name, value ):
     try:
         if isinstance( value, Quantity ):
             if value.family_name == self.family_name:
                 return value
         else:
             if self.display_units is None:
                 units = unit_manager.default_units_for( self.family_name )
             else:
                 units = self.display_units
             return Quantity( value, units       = units,
                                     family_name = self.family_name )
     except:
         pass
     self.error( object, name, self.repr( value ) )
Beispiel #5
0
    def __init__(self, data, units=None, name='', **traits):
        """ Constructor. """

        # Base class constructors.
        super(Quantity, self).__init__(**traits)

        # Flag used to save a compare if units/family_name compatibility has
        # already been validated.
        compatibility_checked = None

        self.name = name

        # Determine what units to use and if they are family compatible.
        if units is None:
            units = unit_manager.default_units_for(self.family_name)

        # Allow for parsing a unit string as the units argument
        else:
            if isinstance(units, str):
                units = unit_parser.parse_unit(units, suppress_warnings=False)

        if 'family_name' not in traits:
            # If the family name wasn't passed in, we try to guess it.
            # TODO: Should lower() be called here--one can do an
            # 'obj.family_name='xxx'" that would not call a 'lower' method.

            try:
                um = unit_manager
                if self.family_name is None:
                    family_name = ''
                else:
                    family_name = self.family_name.lower()
                self.family_name = \
                               um.get_family_name(family_name) or \
                               em.get_family_name(name.lower())

                # If units were passed in, but don't match the guessed family_name,
                # punt.
                if not unit_manager.is_compatible(units, self.family_name):
                    self.family_name = "unknown"
                    compatibility_checked = True

            except KeyError:
                logger.warn("Could not find family name: %s" % \
                              self.family_name or name)
        else:
            # fixme: This is subverting the FamilyNameTrait behavior,
            #        but it gets around proava ticket:1715-comment14,item 2.
            self.family_name = traits['family_name']

        # If we haven't checked compatibilty before, and units
        if (compatibility_checked != True
                and not unit_manager.is_compatible(units, self.family_name)):
            raise ValueError("units (%s) not compatible with family_name (%s)" \
                            % (units.label, self.family_name))

        self.units = units

        # The following timing data is from using the code below the
        #     timings that sets values for 'data' and 'units' using the
        #     'slow' method.

        if isinstance(data, Quantity):
            self.data = self._convert(data, units)
        else:
            self.data = data

        # The 'fast' method of just stuffing the __dict__ values yields a
        #     performance improvement of 2x-- 0.0016 sec vs. 0.0035 sec per
        #     unit change (which creates a new Quantity object).

        #self.__dict__['data'] = self._convert(data, units)
        #self.__dict__['units'] = units

        return
Beispiel #6
0
    def __init__(self, data, units = None, name = '', **traits):
        """ Constructor. """

        # Base class constructors.
        super(Quantity, self).__init__(**traits)


        # Flag used to save a compare if units/family_name compatibility has
        # already been validated.
        compatibility_checked = None

        self.name = name

        # Determine what units to use and if they are family compatible.
        if units is None:
            units = unit_manager.default_units_for( self.family_name )

        # Allow for parsing a unit string as the units argument
        else:
            if isinstance(units, basestring):
                units = unit_parser.parse_unit(units, suppress_warnings=False)

        if not traits.has_key('family_name'):
            # If the family name wasn't passed in, we try to guess it.
            # TODO: Should lower() be called here--one can do an
            # 'obj.family_name='xxx'" that would not call a 'lower' method.

            try:
                um = unit_manager
                if self.family_name is None:
                    family_name = ''
                else:
                    family_name = self.family_name.lower()
                self.family_name = \
                               um.get_family_name(family_name) or \
                               em.get_family_name(name.lower())

                # If units were passed in, but don't match the guessed family_name,
                # punt.
                if not unit_manager.is_compatible( units, self.family_name ):
                    self.family_name = "unknown"
                    compatibility_checked = True

            except KeyError:
                logger.warn("Could not find family name: %s" % \
                              self.family_name or name)
        else:
            # fixme: This is subverting the FamilyNameTrait behavior,
            #        but it gets around proava ticket:1715-comment14,item 2.
            self.family_name = traits['family_name']


        # If we haven't checked compatibilty before, and units
        if (compatibility_checked != True and
            not unit_manager.is_compatible( units, self.family_name )):
            raise ValueError, "units (%s) not compatible with family_name (%s)" \
                            % (units.label, self.family_name)

        self.units = units

        # The following timing data is from using the code below the
        #     timings that sets values for 'data' and 'units' using the
        #     'slow' method.

        if isinstance(data, Quantity):
            self.data = self._convert(data, units)
        else:
            self.data = data

        # The 'fast' method of just stuffing the __dict__ values yields a
        #     performance improvement of 2x-- 0.0016 sec vs. 0.0035 sec per
        #     unit change (which creates a new Quantity object).

        #self.__dict__['data'] = self._convert(data, units)
        #self.__dict__['units'] = units

        return