def __setattr__(self, name, val, level=0): # attribute access is switched off until this attribute is created by # _enable_group_attributes if not hasattr(self, '_group_attribute_access_active') or name in self.__dict__: object.__setattr__(self, name, val) elif name in self.variables: var = self.variables[name] if not isinstance(val, basestring): if var.unit.dim is DIMENSIONLESS: fail_for_dimension_mismatch(val, var.unit, ('%s should be set with a ' 'dimensionless value, but got ' '{value}') % name, value=val) else: fail_for_dimension_mismatch(val, var.unit, ('%s should be set with a ' 'value with units %r, but got ' '{value}') % (name, var.unit), value=val) if var.read_only: raise TypeError('Variable %s is read-only.' % name) # Make the call X.var = ... equivalent to X.var[:] = ... var.get_addressable_value_with_unit(name, self).set_item(slice(None), val, level=level+1) elif len(name) and name[-1]=='_' and name[:-1] in self.variables: # no unit checking var = self.variables[name[:-1]] if var.read_only: raise TypeError('Variable %s is read-only.' % name[:-1]) # Make the call X.var = ... equivalent to X.var[:] = ... var.get_addressable_value(name[:-1], self).set_item(slice(None), val, level=level+1) elif hasattr(self, name) or name.startswith('_'): object.__setattr__(self, name, val) else: # Try to suggest the correct name in case of a typo checker = SpellChecker([varname for varname, var in self.variables.iteritems() if not (varname.startswith('_') or var.read_only)]) if name.endswith('_'): suffix = '_' name = name[:-1] else: suffix = '' error_msg = 'Could not find a state variable with name "%s".' % name suggestions = checker.suggest(name) if len(suggestions) == 1: suggestion, = suggestions error_msg += ' Did you mean to write "%s%s"?' % (suggestion, suffix) elif len(suggestions) > 1: error_msg += (' Did you mean to write any of the following: %s ?' % (', '.join(['"%s%s"' % (suggestion, suffix) for suggestion in suggestions]))) error_msg += (' Use the add_attribute method if you intend to add ' 'a new attribute to the object.') raise AttributeError(error_msg)
def __setattr__(self, name, val, level=0): # attribute access is switched off until this attribute is created by # _enable_group_attributes if not hasattr(self, '_group_attribute_access_active') or name in self.__dict__: object.__setattr__(self, name, val) elif name in self.variables: var = self.variables[name] if not isinstance(val, basestring): fail_for_dimension_mismatch(val, var.unit, 'Incorrect units for setting %s' % name) if var.read_only: raise TypeError('Variable %s is read-only.' % name) # Make the call X.var = ... equivalent to X.var[:] = ... var.get_addressable_value_with_unit(name, self).set_item(slice(None), val, level=level+1) elif len(name) and name[-1]=='_' and name[:-1] in self.variables: # no unit checking var = self.variables[name[:-1]] if var.read_only: raise TypeError('Variable %s is read-only.' % name[:-1]) # Make the call X.var = ... equivalent to X.var[:] = ... var.get_addressable_value(name[:-1], self).set_item(slice(None), val, level=level+1) elif hasattr(self, name) or name.startswith('_'): object.__setattr__(self, name, val) else: # Try to suggest the correct name in case of a typo checker = SpellChecker([varname for varname, var in self.variables.iteritems() if not (varname.startswith('_') or var.read_only)]) if name.endswith('_'): suffix = '_' name = name[:-1] else: suffix = '' error_msg = 'Could not find a state variable with name "%s".' % name suggestions = checker.suggest(name) if len(suggestions) == 1: suggestion, = suggestions error_msg += ' Did you mean to write "%s%s"?' % (suggestion, suffix) elif len(suggestions) > 1: error_msg += (' Did you mean to write any of the following: %s ?' % (', '.join(['"%s%s"' % (suggestion, suffix) for suggestion in suggestions]))) error_msg += (' Use the add_attribute method if you intend to add ' 'a new attribute to the object.') raise AttributeError(error_msg)
def __setattr__(self, name, val, level=0): # attribute access is switched off until this attribute is created by # _enable_group_attributes if not hasattr(self, "_group_attribute_access_active") or name in self.__dict__: object.__setattr__(self, name, val) elif name in self.__getattribute__("__dict__") or name in self.__getattribute__("__class__").__dict__: # Makes sure that classes can override the "variables" mechanism # with instance/class attributes and properties return object.__setattr__(self, name, val) elif name in self.variables: var = self.variables[name] if not isinstance(val, basestring): if var.unit.dim is DIMENSIONLESS: fail_for_dimension_mismatch( val, var.unit, ("%s should be set with a " "dimensionless value, but got " "{value}") % name, value=val, ) else: fail_for_dimension_mismatch( val, var.unit, ("%s should be set with a " "value with units %r, but got " "{value}") % (name, var.unit), value=val, ) if var.read_only: raise TypeError("Variable %s is read-only." % name) # Make the call X.var = ... equivalent to X.var[:] = ... var.get_addressable_value_with_unit(name, self).set_item(slice(None), val, level=level + 1) elif len(name) and name[-1] == "_" and name[:-1] in self.variables: # no unit checking var = self.variables[name[:-1]] if var.read_only: raise TypeError("Variable %s is read-only." % name[:-1]) # Make the call X.var = ... equivalent to X.var[:] = ... var.get_addressable_value(name[:-1], self).set_item(slice(None), val, level=level + 1) elif hasattr(self, name) or name.startswith("_"): object.__setattr__(self, name, val) else: # Try to suggest the correct name in case of a typo checker = SpellChecker( [ varname for varname, var in self.variables.iteritems() if not (varname.startswith("_") or var.read_only) ] ) if name.endswith("_"): suffix = "_" name = name[:-1] else: suffix = "" error_msg = 'Could not find a state variable with name "%s".' % name suggestions = checker.suggest(name) if len(suggestions) == 1: suggestion, = suggestions error_msg += ' Did you mean to write "%s%s"?' % (suggestion, suffix) elif len(suggestions) > 1: error_msg += " Did you mean to write any of the following: %s ?" % ( ", ".join(['"%s%s"' % (suggestion, suffix) for suggestion in suggestions]) ) error_msg += " Use the add_attribute method if you intend to add " "a new attribute to the object." raise AttributeError(error_msg)
def test_spell_check(): checker = SpellChecker(['vm', 'alpha', 'beta']) assert checker.suggest('Vm') == {'vm'} assert checker.suggest('alphas') == {'alpha'} assert checker.suggest('bta') == {'beta'} assert checker.suggest('gamma') == set()
def __setattr__(self, name, val, level=0): # attribute access is switched off until this attribute is created by # _enable_group_attributes if not hasattr( self, '_group_attribute_access_active') or name in self.__dict__: object.__setattr__(self, name, val) elif (name in self.__getattribute__('__dict__') or name in self.__getattribute__('__class__').__dict__): # Makes sure that classes can override the "variables" mechanism # with instance/class attributes and properties return object.__setattr__(self, name, val) elif name in self.variables: var = self.variables[name] if not isinstance(val, basestring): if var.dim is DIMENSIONLESS: fail_for_dimension_mismatch( val, var.dim, ('%s should be set with a ' 'dimensionless value, but got ' '{value}') % name, value=val) else: fail_for_dimension_mismatch( val, var.dim, ('%s should be set with a ' 'value with units %r, but got ' '{value}') % (name, get_unit(var.dim)), value=val) if var.read_only: raise TypeError('Variable %s is read-only.' % name) # Make the call X.var = ... equivalent to X.var[:] = ... var.get_addressable_value_with_unit(name, self).set_item(slice(None), val, level=level + 1) elif len(name) and name[-1] == '_' and name[:-1] in self.variables: # no unit checking var = self.variables[name[:-1]] if var.read_only: raise TypeError('Variable %s is read-only.' % name[:-1]) # Make the call X.var = ... equivalent to X.var[:] = ... var.get_addressable_value(name[:-1], self).set_item(slice(None), val, level=level + 1) elif hasattr(self, name) or name.startswith('_'): object.__setattr__(self, name, val) else: # Try to suggest the correct name in case of a typo checker = SpellChecker([ varname for varname, var in self.variables.items() if not (varname.startswith('_') or var.read_only) ]) if name.endswith('_'): suffix = '_' name = name[:-1] else: suffix = '' error_msg = 'Could not find a state variable with name "%s".' % name suggestions = checker.suggest(name) if len(suggestions) == 1: suggestion, = suggestions error_msg += ' Did you mean to write "%s%s"?' % (suggestion, suffix) elif len(suggestions) > 1: error_msg += ( ' Did you mean to write any of the following: %s ?' % (', '.join([ '"%s%s"' % (suggestion, suffix) for suggestion in suggestions ]))) error_msg += (' Use the add_attribute method if you intend to add ' 'a new attribute to the object.') raise AttributeError(error_msg)