def info(self): aClass = self.aClass if type(aClass) is not str: aClass = aClass.__name__ result = class_of(aClass) if self.or_none is None: return result + ' or None' return result
def info ( self ): aClass = self.aClass if type( aClass ) is not str: aClass = aClass.__name__ result = class_of( aClass ) if self.or_none is None: return result + ' or None' return result
def set_args ( self ): if self.desc is None: extra = '' else: extra = ' specifies %s and' % self.desc obj = getattr( self, 'object', None ) if obj is not None: self.args = ( "%s '%s' trait of %s instance%s must be %s, " "but a value of %s was specified." % ( self.prefix, self.name, class_of( obj ), extra, self.info, self.value ) ) else: self.args = ( "%s '%s' trait%s must be %s, but a value of %s was " "specified." % ( self.prefix, self.name, extra, self.info, self.value ) )
def validate(self, object, name, value): # If the value is already the desired type, then return it: if type(value) is self.aType: return value # Else try to cast it to the specified type: try: return self.aType(value) except: # Otherwise, raise an exception: tv = type(value) if tv is InstanceType: kind = class_of(value) else: kind = repr(value) self.error(object, name, '%s (i.e. %s)' % (str(tv)[1:-1], kind))
def validate ( self, object, name, value ): # If the value is already the desired type, then return it: if type( value ) is self.aType: return value # Else try to cast it to the specified type: try: return self.aType( value ) except: # Otherwise, raise an exception: tv = type( value ) if tv is InstanceType: kind = class_of( value ) else: kind = repr( value ) self.error( object, name, '%s (i.e. %s)' % ( str( tv )[1:-1], kind ) )
def set_args(self): if self.desc is None: extra = '' else: extra = ' specifies %s and' % self.desc obj = getattr(self, 'object', None) # Note: self.args must be a tuple so be sure to leave the trailing # commas. if obj is not None: self.args = ("%s '%s' trait of %s instance%s must be %s, " "but a value of %s was specified." % (self.prefix, self.name, class_of(obj), extra, self.info, self.value)), else: self.args = ( "%s '%s' trait%s must be %s, but a value of %s was " "specified." % (self.prefix, self.name, extra, self.info, self.value)),
def validate ( self, object, name, value ): fv = self.fast_validate tv = type( value ) # If the value is already the desired type, then return it: if tv is fv[1]: return value # Else see if it is one of the coercable types: for typei in fv[2:]: if tv is typei: # Return the coerced value: return fv[1]( value ) # Otherwise, raise an exception: if tv is InstanceType: kind = class_of( value ) else: kind = repr( value ) self.error( object, name, '%s (i.e. %s)' % ( str( tv )[1:-1], kind ) )
def validate(self, object, name, value): fv = self.fast_validate tv = type(value) # If the value is already the desired type, then return it: if tv is fv[1]: return value # Else see if it is one of the coercable types: for typei in fv[2:]: if tv is typei: # Return the coerced value: return fv[1](value) # Otherwise, raise an exception: if tv is InstanceType: kind = class_of(value) else: kind = repr(value) self.error(object, name, '%s (i.e. %s)' % (str(tv)[1:-1], kind))
def _write_only ( object, name ): raise TraitError, "The '%s' trait of %s instance is 'write only'." % ( name, class_of( object ) )
def _read_only(object, name, value): raise TraitError, "The '%s' trait of %s instance is 'read only'." % ( name, class_of(object))
def _undefined_get(object, name): raise TraitError, ("The '%s' trait of %s instance is a property that has " "no 'get' or 'set' method") % (name, class_of(object))
def keyword_error(self, method, object, name, value): raise TraitError, ( "The '%s' keyword argument of the %s method of " "%s instance must be %s, but a value of %s was " "specified." % (name, method.tm_name, class_of(object), self.info(), value))
def _write_only(object, name): raise TraitError, "The '%s' trait of %s instance is 'write only'." % ( name, class_of(object))
def return_error ( self, method, object, value ): raise TraitError, ("The result of the %s method of %s instance must " "be %s, but a value of %s was returned." % ( method.tm_name, class_of( object ), self.info(), value ) )
def keyword_error ( self, method, object, name, value ): raise TraitError, ("The '%s' keyword argument of the %s method of " "%s instance must be %s, but a value of %s was " "specified." % ( name, method.tm_name, class_of( object ), self.info(), value ) )
def validate ( self, object, name, value ): raise TraitError, ( "The '%s' trait of %s instance has an unknown type. " "Contact the developer to correct the problem." % ( name, class_of( object ) ) )
def len_error ( self, len ): raise TraitError, ( "The '%s' trait of %s instance must be %s, " "but you attempted to change its length to %d element%s." % ( self.name, class_of( self.object ), self.trait.info(), len, 's'[ len == 1: ] ) )
def len_error(self, len): raise TraitError, ( "The '%s' trait of %s instance must be %s, " "but you attempted to change its length to %d element%s." % (self.name, class_of( self.object), self.trait.info(), len, 's'[len == 1:]))
def dup_arg_error ( self, method, arg_num, object, name ): raise TraitError, ("The '%s' parameter (argument %d) of the %s method " "of %s instance was specified as both a positional " "and keyword value." % ( name, arg_num, method.tm_name, class_of( object ) ) )
def return_error(self, method, object, value): raise TraitError, ( "The result of the %s method of %s instance must " "be %s, but a value of %s was returned." % (method.tm_name, class_of(object), self.info(), value))
def dup_arg_error(self, method, arg_num, object, name): raise TraitError, ("The '%s' parameter (argument %d) of the %s method " "of %s instance was specified as both a positional " "and keyword value." % (name, arg_num, method.tm_name, class_of(object)))
def missing_arg_error(self, method, arg_num, object, name): raise TraitError, ( "The '%s' parameter (argument %d) of the %s method " "of %s instance must be specified, but was omitted." % (name, arg_num, method.tm_name, class_of(object)))
def _read_only ( object, name, value ): raise TraitError, "The '%s' trait of %s instance is 'read only'." % ( name, class_of( object ) )
def validate(self, object, name, value): raise TraitError, ( "The '%s' trait of %s instance has an unknown type. " "Contact the developer to correct the problem." % (name, class_of(object)))
def _undefined_get ( object, name ): raise TraitError, ("The '%s' trait of %s instance is a property that has " "no 'get' or 'set' method") % ( name, class_of( object ) )
def arg_error(self, method, arg_num, object, name, value): raise TraitError, ("The '%s' parameter (argument %d) of the %s method " "of %s instance must be %s, but a value of %s was " "specified." % (name, arg_num, method.tm_name, class_of(object), self.info(), value))
def missing_arg_error ( self, method, arg_num, object, name ): raise TraitError, ("The '%s' parameter (argument %d) of the %s method " "of %s instance must be specified, but was omitted." % ( name, arg_num, method.tm_name, class_of( object ) ) )
def arg_error ( self, method, arg_num, object, name, value ): raise TraitError, ("The '%s' parameter (argument %d) of the %s method " "of %s instance must be %s, but a value of %s was " "specified." % ( name, arg_num, method.tm_name, class_of( object ), self.info(), value ) )