def _validate(self): """ Validate the current state to see if the wizard can finish. """ # Can't have a sample interval of 0.0 or less, since we would never # reach the bottom of the log. if not unit_manager.is_compatible(self.obj.units, self.obj.value_unit_family): self.complete = False self._error_text.SetLabel( 'Units and family are incompatible. Try one of: %s' % unit_manager.get_valid_unit_strings( self.obj.value_unit_family) ) if self._error_panel is not None: self._error_panel.Show(True) self._error_panel.GetParent().GetSizer().Layout() elif self.obj.units is None or self.obj.units == '': self.complete = False self._error_text.SetLabel( 'Units are required.' ) if self._error_panel is not None: self._error_panel.Show(True) self._error_panel.GetParent().GetSizer().Layout() else: self.complete = True if self._error_panel is not None: self._error_panel.Show(False) self._error_panel.GetParent().GetSizer().Layout() return
def setattr(self, info, object, name, value): """ Handles setting a specified object trait's value. Parameters ---------- object : object The object whose attribute is being set name : string The name of the attribute being set value The value to which the attribute is being set """ if not info.initialized: return if name == 'units': # Convert units label to units object. if not unit_manager.is_compatible(value, object.family_name): raise TraitError() value = unit_parser.parse_unit(value, suppress_warnings=False) super(QuantityViewHandler, self).setattr(info, object, name, value) return
def _validate(self): """ Validate the current state to see if the wizard can finish. """ # Can't have a sample interval of 0.0 or less, since we would never # reach the bottom of the log. if not unit_manager.is_compatible( self.obj.units, self.obj.value_unit_family): self.complete = False self._error_text.SetLabel( 'Units and family are incompatible. Try one of: %s' % unit_manager.get_valid_unit_strings( self.obj.value_unit_family ) ) if self._error_panel is not None: self._error_panel.Show(True) self._error_panel.GetParent().GetSizer().Layout() elif self.obj.units is None or self.obj.units == '': self.complete = False self._error_text.SetLabel( 'Units are required.' ) if self._error_panel is not None: self._error_panel.Show(True) self._error_panel.GetParent().GetSizer().Layout() else: self.complete = True if self._error_panel is not None: self._error_panel.Show(False) self._error_panel.GetParent().GetSizer().Layout() return
def setattr(self, info, object, name, value): """ Handles setting a specified object trait's value. Parameters ---------- object : object The object whose attribute is being set name : string The name of the attribute being set value The value to which the attribute is being set """ if not info.initialized: return if name == 'units': # Convert units label to units object. if not unit_manager.is_compatible(value, object.family_name): raise TraitError() value = unit_parser.parse_unit(value, suppress_warnings=False) super(QuantityViewHandler, self).setattr(info, object, name, value) return
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
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