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
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 floating point field types""" if isinstance( value, (str,unicode)): value = [ float(x) for x in value.replace( ',', ' ').split()] if isinstance(value, field.NUMERIC_TYPES): return arrays.array([float(value)],self.targetType) elif isinstance( value, arrays.ArrayType ): if arrays.typeCode(value) not in self.acceptedTypes: value = value.astype(self.targetType) return arrays.contiguous( arrays.ravel(value) ) elif isinstance( value, field.SEQUENCE_TYPES): return arrays.array( map( float, collapse( value) ), self.targetType, ) elif not value: return arrays.array([],self.targetType) raise ValueError( """Attempted to set value for an %s field which is not compatible: %s"""%( self.typeName(), repr(value) ))
def coerce(self, value): """Base coercion mechanism for floating point field types""" if isinstance(value, (str, unicode)): value = [float(x) for x in value.replace(',', ' ').split()] if isinstance(value, field.NUMERIC_TYPES): return arrays.array([float(value)], self.targetType) elif isinstance(value, arrays.ArrayType): if arrays.typeCode(value) not in self.acceptedTypes: value = value.astype(self.targetType) return arrays.contiguous(arrays.ravel(value)) elif isinstance(value, field.SEQUENCE_TYPES): return arrays.array( map(float, collapse(value)), self.targetType, ) elif not value: return arrays.array([], self.targetType) raise ValueError( """Attempted to set value for an %s field which is not compatible: %s""" % (self.typeName(), repr(value)))
def coerce( self, value ): """Base coercion mechanism for multiple-value integer fields""" if isinstance( value, (str,unicode)): value = [ self.base_converter(x) for x in value.replace( ',', ' ').split() ] if isinstance(value, field.NUMERIC_TYPES): return arrays.array([int(value)],self.arrayDataType) elif isinstance( value, arrays.ArrayType ): if arrays.typeCode(value) not in self.acceptedTypes: value = value.astype( self.arrayDataType ) return arrays.contiguous( arrays.ravel(value) ) elif isinstance( value, field.SEQUENCE_TYPES): return arrays.array( [int(obj) for obj in value], self.arrayDataType, ) elif not value: return arrays.array([],self.arrayDataType) raise ValueError( """Attempted to set value for an %s field which is not compatible: %s"""%( self.typeName(), repr(value) ))