def coerce(self, value): """Base coercion mechanism for vector-like field types""" if isinstance(value, (str, unicode)): value = [float(x) for x in value.replace(',', ' ').split()] if isinstance(value, (int, long, float)): value = arrays.zeros(self.dimension, self.targetType) value[:] = float(value) elif isinstance(value, arrays.ArrayType): if arrays.typeCode(value) not in self.acceptedTypes: value = value.astype(self.targetType) value = value.reshape(self.dimension) elif isinstance(value, field.SEQUENCE_TYPES): value = arrays.asarray(map(float, collapse(value)), self.targetType) value.reshape(self.dimension) else: try: value = arrays.asarray(value, self.targetType) except Exception: raise ValueError( """Attempted to set value for an %s field which is not compatible: %s""" % (self.typeName(), repr(value))) else: value.reshape(self.dimension) if value.shape != self.dimension: raise ValueError( """%s value of incorrect shape (is %s, should be %s)""" % ( self.__class__.__name__, value.shape, self.dimension, )) value = arrays.contiguous(value) return value
def coerce( self, value ): """Base coercion mechanism for vector-like field types""" if isinstance( value, (str,unicode)): value = [ float(x) for x in value.replace( ',', ' ').split()] if isinstance(value, (int,long,float)): value = arrays.zeros( self.dimension, self.targetType ) value[:] = float(value) elif isinstance( value, arrays.ArrayType ): if arrays.typeCode(value) not in self.acceptedTypes: value = value.astype(self.targetType) value = value.reshape( self.dimension ) elif isinstance( value, field.SEQUENCE_TYPES): value = arrays.asarray( map(float, collapse(value)), self.targetType ) value.reshape( self.dimension ) else: try: value = arrays.asarray( value, self.targetType ) except Exception: raise ValueError( """Attempted to set value for an %s field which is not compatible: %s"""%( self.typeName(), repr(value) )) else: value.reshape( self.dimension ) if value.shape != self.dimension: raise ValueError( """%s value of incorrect shape (is %s, should be %s)"""%( self.__class__.__name__, value.shape, self.dimension, ) ) value = arrays.contiguous( value ) return value
def coerce(self, value): if isinstance(value, (str, unicode)): value = [ float(x) for x in value.replace(',', ' ').replace( '[', ' ').replace(']').split() ] if isinstance(value, arrays.ArrayType): if arrays.typeCode(value) not in self.acceptedTypes: value = value.astype(self.targetType) elif isinstance(value, field.SEQUENCE_TYPES): try: value = arrays.array(value, self.targetType) except ValueError: value = arrays.array( map(float, collapse(value)), self.targetType, ) elif isinstance(value, (int, long, float)): value = arrays.array([value], self.targetType) else: try: value = arrays.asarray(value, self.targetType) except Exception: raise ValueError( """Attempted to set value for an %s field which is not compatible: %s""" % (self.typeName(), repr(value))) # special casing, again, for explicitly structured arrays if not arrays.typeCode(value) == 'V': value = arrays.contiguous(self.reshape(value)) return value
def coerce( self, value ): if isinstance( value, (str,unicode)): value = [ float(x) for x in value.replace( ',', ' ').replace('[',' ').replace(']').split() ] if isinstance( value, arrays.ArrayType ): if arrays.typeCode(value) not in self.acceptedTypes: value = value.astype( self.targetType ) elif isinstance( value, field.SEQUENCE_TYPES): try: value = arrays.array( value, self.targetType) except ValueError: value = arrays.array( map( float, collapse( value) ), self.targetType, ) elif isinstance( value, (int,long,float)): value = arrays.array( [value], self.targetType ) else: try: value = arrays.asarray( value, self.targetType ) except Exception: raise ValueError( """Attempted to set value for an %s field which is not compatible: %s"""%( self.typeName(), repr(value) )) # special casing, again, for explicitly structured arrays if not arrays.typeCode( value ) == 'V': value = arrays.contiguous( self.reshape(value) ) return value