def regularize_extra(self, val): if isinstance(val, datetime.datetime): tt = val.utctimetuple() val = calendar.timegm(tt) + val.microsecond * 1e-6 elif isinstance(val, datetime.date): tt = val.timetuple() val = float(calendar.timegm(tt)) elif isinstance(val, str) or isinstance(val, unicode): val = val.strip() val = re.sub(r'(Z|\+00(:?00)?)$', '', val) if val[10] == 'T': val = val.replace('T', ' ', 1) try: val = util.str_to_time(val) except util.TimeStrError: year = int(val[:4]) if year > this_year + 100: return None # StationXML contained a dummy end date raise elif isinstance(val, int): val = float(val) else: raise ValidationError('%s: cannot convert "%s" to float' % (self.xname(), val)) return val
def validate_extra(self, val): if self.dtype is not None and self.dtype != val.dtype: raise ValidationError( 'array not of required type: need %s, got %s' % (self.dtype, val.dtype)) if self.shape is not None: la, lb = len(self.shape), len(val.shape) if la != lb: raise ValidationError( 'array dimension mismatch: need %i, got %i' % (la, lb)) for a, b in zip(self.shape, val.shape): if a is not None: if a != b: raise ValidationError( 'array shape mismatch: need %s, got: %s' % (self.shape, val.shape))
def validate(self, val, regularize=False, depth=-1): if regularize: try: val = str(val) except ValueError: raise ValidationError('%s: cannot convert to string %s' % (self.xname, repr(val))) val = self.dummy_cls.replacements.get(val, val) self.validate_extra(val) return val
def regularize_extra(self, val): if isinstance(val, datetime.datetime): tt = val.utctimetuple() val = calendar.timegm(tt) + val.microsecond * 1e-6 elif isinstance(val, datetime.date): tt = val.timetuple() val = float(calendar.timegm(tt)) elif isinstance(val, (str, newstr)): val = val.strip() tz_offset = 0 m = re_tz.search(val) if m: sh = m.group(2) sm = m.group(4) tz_offset = (int(sh)*3600 if sh else 0) \ + (int(sm)*60 if sm else 0) val = re_tz.sub('', val) if val[10] == 'T': val = val.replace('T', ' ', 1) try: val = util.str_to_time(val) - tz_offset except util.TimeStrError: year = int(val[:4]) if sys.maxsize > 2**32: # if we're on 64bit if year > this_year + 100: return None # StationXML contained a dummy date else: # 32bit end of time is in 2038 if this_year < 2037 and year > 2037 or year < 1903: return None # StationXML contained a dummy date raise elif isinstance(val, int): val = float(val) else: raise ValidationError( '%s: cannot convert "%s" to float' % (self.xname(), val)) return val
def regularize_extra(self, val): if isinstance(val, datetime.datetime): tt = val.utctimetuple() val = calendar.timegm(tt) + val.microsecond * 1e-6 elif isinstance(val, datetime.date): tt = val.timetuple() val = float(calendar.timegm(tt)) elif isinstance(val, (str, newstr)): val = val.strip() val = re.sub(r'(Z|\+00(:?00)?)$', '', val) if val[10] == 'T': val = val.replace('T', ' ', 1) try: val = util.str_to_time(val) except util.TimeStrError: year = int(val[:4]) if sys.maxsize > 2**32: # if we're on 64bit if year > this_year + 100: return None # StationXML contained a dummy date else: # 32bit end of time is in 2038 if this_year < 2037 and year > 2037 or year < 1903: return None # StationXML contained a dummy date raise elif isinstance(val, int): val = float(val) else: raise ValidationError('%s: cannot convert "%s" to float' % (self.xname(), val)) return val
def validate_extra(self, val): if val.y >= val.x: raise ValidationError('x must be greater than y')
def validate_extra(self, val): if val.y <= val.x: raise ValidationError('y must be greater than x')
def validate_extra(self, val): if val < self.min or self.max < val: raise ValidationError('out of range [%g, %g]: %g' % ( self.min, self.max, val))
def regularize_extra(self, val): if isinstance(val, basestring): ndim = None if self.shape: ndim = len(self.shape) if self.serialize_as == 'table': val = num.loadtxt(StringIO(str(val)), dtype=self.dtype, ndmin=ndim) elif self.serialize_as == 'base64': data = b64decode(val) val = num.fromstring( data, dtype=self.serialize_dtype).astype(self.dtype) elif self.serialize_as == 'npy': data = b64decode(val) try: val = num.load(StringIO(str(data)), allow_pickle=False) except TypeError: # allow_pickle only available in newer NumPy val = num.load(StringIO(str(data))) elif isinstance(val, dict): if self.serialize_as == 'base64+meta': if not sorted(val.keys()) == ['data', 'dtype', 'shape']: raise ValidationError( 'array in format "base64+meta" must have keys ' '"data", "dtype", and "shape"') shape = val['shape'] if not isinstance(shape, list): raise ValidationError('invalid shape definition') for n in shape: if not isinstance(n, int): raise ValidationError('invalid shape definition') serialize_dtype = val['dtype'] allowed = list(restricted_dtype_map_rev.keys()) if self.serialize_dtype is not None: allowed.append(self.serialize_dtype) if serialize_dtype not in allowed: raise ValidationError( 'only the following dtypes are allowed: %s' % ', '.join(sorted(allowed))) data = val['data'] if not isinstance(data, basestring): raise ValidationError( 'data must be given as a base64 encoded string') data = b64decode(data) dtype = self.dtype or \ restricted_dtype_map_rev[serialize_dtype] val = num.fromstring(data, dtype=serialize_dtype).astype(dtype) if val.size != num.product(shape): raise ValidationError('size/shape mismatch') val = val.reshape(shape) else: val = num.asarray(val, dtype=self.dtype) return val