def isNegInf(val): if sys.version_info < (2, 5) or sys.platform.startswith('win'): import fpconst return fpconst.isNegInf(val) else: return val == float('-inf')
def isNegInf(val): if is_float_broken(): import fpconst return fpconst.isNegInf(val) else: return val == -1e300000
def _serialize_float(d): if fpconst.isNaN(d): return 'd:NAN;' elif fpconst.isPosInf(d): return 'd:INF;' elif fpconst.isNegInf(d): return 'd:-INF;' else: return 'd:%s;' % d
def write_double_workaround(self, d): if fpconst.isNaN(d): self.write('\xff\xf8\x00\x00\x00\x00\x00\x00') elif fpconst.isNegInf(d): self.write('\xff\xf0\x00\x00\x00\x00\x00\x00') elif fpconst.isPosInf(d): self.write('\x7f\xf0\x00\x00\x00\x00\x00\x00') else: write_double_workaround.old_func(self, d)
def test_nan(self): import fpconst x = ByteStream('\xff\xf8\x00\x00\x00\x00\x00\x00') self.assertTrue(fpconst.isNaN(x.read_double())) x = ByteStream('\xff\xf0\x00\x00\x00\x00\x00\x00') self.assertTrue(fpconst.isNegInf(x.read_double())) x = ByteStream('\x7f\xf0\x00\x00\x00\x00\x00\x00') self.assertTrue(fpconst.isPosInf(x.read_double()))
def ic(pwm, bg): """ Calculate the information content of the PWM against the background in nats. Both pwm and bg are expected to be callable objects that return the log likelihood. """ ic = 0. ix = N.zeros(pwm.K, dtype=int) while True: l = pwm(ix) if not fpconst.isNegInf(l): x = N.exp(l) * (l - bg(ix)) assert not fpconst.isNaN(x) ic += x if not inc_index(ix): break return ic
def ic(pwm, bg): """ Calculate the information content of the PWM against the background in nats. Both pwm and bg are expected to be callable objects that return the log likelihood. """ ic = 0.0 ix = N.zeros(pwm.K, dtype=int) while True: l = pwm(ix) if not fpconst.isNegInf(l): x = N.exp(l) * (l - bg(ix)) assert not fpconst.isNaN(x) ic += x if not inc_index(ix): break return ic
def dump_float(self, obj, tag, typed = 1, ns_map = {}): if Config.debug: print "In dump_float." tag = tag or self.gentag() if Config.strict_range: doubleType(obj) if fpconst.isPosInf(obj): obj = "INF" elif fpconst.isNegInf(obj): obj = "-INF" elif fpconst.isNaN(obj): obj = "NaN" else: obj = str(obj) # Note: python 'float' is actually a SOAP 'double'. self.out.append(self.dumper(None, "double", obj, tag, typed, ns_map, self.genroot(ns_map)))
def dump_float(self, obj, tag, typed = 1, ns_map = {}): if Config.debug: print "In dump_float." tag = tag or self.gentag() tag = toXMLname(tag) # convert from SOAP 1.2 XML name encoding if Config.strict_range: doubleType(obj) if fpconst.isPosInf(obj): obj = "INF" elif fpconst.isNegInf(obj): obj = "-INF" elif fpconst.isNaN(obj): obj = "NaN" else: obj = str(obj) # Note: python 'float' is actually a SOAP 'double'. self.out.append(self.dumper(None, "double", obj, tag, typed, ns_map, self.genroot(ns_map)))
def convertToBasicTypes(self, d, t, attrs, config=Config): dnn = d or "" # if Config.debug: # print "convertToBasicTypes:" # print " requested_type=", t # print " data=", d # print "convertToBasicTypes:" # print " requested_type=", t # print " data=", d # print " attrs=", attrs # print " t[0]=", t[0] # print " t[1]=", t[1] # print " in?", t[0] in NS.EXSD_L if t[0] in NS.EXSD_L: if t[1] == "integer": # unbounded integer type try: d = int(d) if len(attrs): d = long(d) except: d = long(d) return d if self.intlimits.has_key(t[1]): # range-bounded integer types l = self.intlimits[t[1]] try: d = int(d) except: d = long(d) if l[1] != None and d < l[1]: raise UnderflowError, "%s too small" % d if l[2] != None and d > l[2]: raise OverflowError, "%s too large" % d if l[0] or len(attrs): return long(d) return d if t[1] == "string": if len(attrs): return unicode(dnn) try: return str(dnn) except: return dnn if t[1] in ("bool", "boolean"): d = d.strip().lower() if d in ("0", "false"): return False if d in ("1", "true"): return True raise AttributeError, "invalid boolean value" if t[1] in ("double", "float"): l = self.floatlimits[t[1]] s = d.strip().lower() # Explicitly check for NaN and Infinities if s == "nan": d = fpconst.NaN elif s[0:2] == "inf" or s[0:3] == "+inf": d = fpconst.PosInf elif s[0:3] == "-inf": d = fpconst.NegInf else: d = float(s) if config.strict_range: if fpconst.isNaN(d): if s[0:2] != "nan": raise ValueError, "invalid %s: %s" % (t[1], s) elif fpconst.isNegInf(d): if s[0:3] != "-inf": raise UnderflowError, "%s too small: %s" % (t[1], s) elif fpconst.isPosInf(d): if s[0:2] != "inf" and s[0:3] != "+inf": raise OverflowError, "%s too large: %s" % (t[1], s) elif d < 0 and d < l[1]: raise UnderflowError, "%s too small: %s" % (t[1], s) elif d > 0 and (d < l[0] or d > l[2]): raise OverflowError, "%s too large: %s" % (t[1], s) elif d == 0: if type(self.zerofloatre) == StringType: self.zerofloatre = re.compile(self.zerofloatre) if self.zerofloatre.search(s): raise UnderflowError, "invalid %s: %s" % (t[1], s) return d if t[1] in ("dateTime", "date", "timeInstant", "time"): return self.convertDateTime(d, t[1]) if t[1] == "decimal": return float(d) if t[1] in ("language", "QName", "NOTATION", "NMTOKEN", "Name", "NCName", "ID", "IDREF", "ENTITY"): return collapseWhiteSpace(d) if t[1] in ("IDREFS", "ENTITIES", "NMTOKENS"): d = collapseWhiteSpace(d) return d.split() if t[0] in NS.XSD_L: if t[1] in ("base64", "base64Binary"): if d: return base64.decodestring(d) else: return "" if t[1] == "hexBinary": if d: return decodeHexString(d) else: return if t[1] == "anyURI": return urllib.unquote(collapseWhiteSpace(d)) if t[1] in ("normalizedString", "token"): return collapseWhiteSpace(d) if t[0] == NS.ENC: if t[1] == "base64": if d: return base64.decodestring(d) else: return "" if t[0] == NS.XSD: if t[1] == "binary": try: e = attrs[(None, "encoding")] if d: if e == "hex": return decodeHexString(d) elif e == "base64": return base64.decodestring(d) else: return "" except: pass raise Error, "unknown or missing binary encoding" if t[1] == "uri": return urllib.unquote(collapseWhiteSpace(d)) if t[1] == "recurringInstant": return self.convertDateTime(d, t[1]) if t[0] in (NS.XSD2, NS.ENC): if t[1] == "uriReference": return urllib.unquote(collapseWhiteSpace(d)) if t[1] == "timePeriod": return self.convertDateTime(d, t[1]) if t[1] in ("century", "year"): return self.convertDateTime(d, t[1]) if t[0] in (NS.XSD, NS.XSD2, NS.ENC): if t[1] == "timeDuration": return self.convertDateTime(d, t[1]) if t[0] == NS.XSD3: if t[1] == "anyURI": return urllib.unquote(collapseWhiteSpace(d)) if t[1] in ("gYearMonth", "gMonthDay"): return self.convertDateTime(d, t[1]) if t[1] == "gYear": return self.convertDateTime(d, t[1]) if t[1] == "gMonth": return self.convertDateTime(d, t[1]) if t[1] == "gDay": return self.convertDateTime(d, t[1]) if t[1] == "duration": return self.convertDateTime(d, t[1]) if t[0] in (NS.XSD2, NS.XSD3): if t[1] == "token": return collapseWhiteSpace(d) if t[1] == "recurringDate": return self.convertDateTime(d, t[1]) if t[1] == "month": return self.convertDateTime(d, t[1]) if t[1] == "recurringDay": return self.convertDateTime(d, t[1]) if t[0] == NS.XSD2: if t[1] == "CDATA": return collapseWhiteSpace(d) raise UnknownTypeError, "unknown type `%s'" % (str(t[0]) + ":" + t[1])
def convertToBasicTypes(self, d, t, attrs, config=Config): dnn = d or '' #if Config.debug: #print "convertToBasicTypes:" #print " requested_type=", t #print " data=", d # print "convertToBasicTypes:" # print " requested_type=", t # print " data=", d # print " attrs=", attrs # print " t[0]=", t[0] # print " t[1]=", t[1] # print " in?", t[0] in NS.EXSD_L if t[0] in NS.EXSD_L: if t[1] == "integer": # unbounded integer type try: d = int(d) if len(attrs): d = long(d) except: d = long(d) return d if self.intlimits.has_key(t[1]): # range-bounded integer types l = self.intlimits[t[1]] try: d = int(d) except: d = long(d) if l[1] != None and d < l[1]: raise UnderflowError, "%s too small" % d if l[2] != None and d > l[2]: raise OverflowError, "%s too large" % d if l[0] or len(attrs): return long(d) return d if t[1] == "string": if len(attrs): return unicode(dnn) try: return str(dnn) except: return dnn if t[1] in ("bool", "boolean"): d = d.strip().lower() if d in ('0', 'false'): return False if d in ('1', 'true'): return True raise AttributeError, "invalid boolean value" if t[1] in ('double', 'float'): l = self.floatlimits[t[1]] s = d.strip().lower() # Explicitly check for NaN and Infinities if s == "nan": d = fpconst.NaN elif s[0:2] == "inf" or s[0:3] == "+inf": d = fpconst.PosInf elif s[0:3] == "-inf": d = fpconst.NegInf else: d = float(s) if config.strict_range: if fpconst.isNaN(d): if s[0:2] != 'nan': raise ValueError, "invalid %s: %s" % (t[1], s) elif fpconst.isNegInf(d): if s[0:3] != '-inf': raise UnderflowError, "%s too small: %s" % (t[1], s) elif fpconst.isPosInf(d): if s[0:2] != 'inf' and s[0:3] != '+inf': raise OverflowError, "%s too large: %s" % (t[1], s) elif d < 0 and d < l[1]: raise UnderflowError, "%s too small: %s" % (t[1], s) elif d > 0 and (d < l[0] or d > l[2]): raise OverflowError, "%s too large: %s" % (t[1], s) elif d == 0: if type(self.zerofloatre) == StringType: self.zerofloatre = re.compile(self.zerofloatre) if self.zerofloatre.search(s): raise UnderflowError, "invalid %s: %s" % (t[1], s) return d if t[1] in ("dateTime", "date", "timeInstant", "time"): return self.convertDateTime(d, t[1]) if t[1] == "decimal": return float(d) if t[1] in ("language", "QName", "NOTATION", "NMTOKEN", "Name", "NCName", "ID", "IDREF", "ENTITY"): return collapseWhiteSpace(d) if t[1] in ("IDREFS", "ENTITIES", "NMTOKENS"): d = collapseWhiteSpace(d) return d.split() if t[0] in NS.XSD_L: if t[1] in ("base64", "base64Binary"): if d: return base64.decodestring(d) else: return '' if t[1] == "hexBinary": if d: return decodeHexString(d) else: return if t[1] == "anyURI": return urllib.unquote(collapseWhiteSpace(d)) if t[1] in ("normalizedString", "token"): return collapseWhiteSpace(d) if t[0] == NS.ENC: if t[1] == "base64": if d: return base64.decodestring(d) else: return '' if t[0] == NS.XSD: if t[1] == "binary": try: e = attrs[(None, 'encoding')] if d: if e == 'hex': return decodeHexString(d) elif e == 'base64': return base64.decodestring(d) else: return '' except: pass raise Error, "unknown or missing binary encoding" if t[1] == "uri": return urllib.unquote(collapseWhiteSpace(d)) if t[1] == "recurringInstant": return self.convertDateTime(d, t[1]) if t[0] in (NS.XSD2, NS.ENC): if t[1] == "uriReference": return urllib.unquote(collapseWhiteSpace(d)) if t[1] == "timePeriod": return self.convertDateTime(d, t[1]) if t[1] in ("century", "year"): return self.convertDateTime(d, t[1]) if t[0] in (NS.XSD, NS.XSD2, NS.ENC): if t[1] == "timeDuration": return self.convertDateTime(d, t[1]) if t[0] == NS.XSD3: if t[1] == "anyURI": return urllib.unquote(collapseWhiteSpace(d)) if t[1] in ("gYearMonth", "gMonthDay"): return self.convertDateTime(d, t[1]) if t[1] == "gYear": return self.convertDateTime(d, t[1]) if t[1] == "gMonth": return self.convertDateTime(d, t[1]) if t[1] == "gDay": return self.convertDateTime(d, t[1]) if t[1] == "duration": return self.convertDateTime(d, t[1]) if t[0] in (NS.XSD2, NS.XSD3): if t[1] == "token": return collapseWhiteSpace(d) if t[1] == "recurringDate": return self.convertDateTime(d, t[1]) if t[1] == "month": return self.convertDateTime(d, t[1]) if t[1] == "recurringDay": return self.convertDateTime(d, t[1]) if t[0] == NS.XSD2: if t[1] == "CDATA": return collapseWhiteSpace(d) raise UnknownTypeError, "unknown type `%s'" % (str(t[0]) + ':' + t[1])
def convertToBasicTypes(self, d, t, attrs, config=Config): dnn = d or '' #if Config.debug: #print "convertToBasicTypes:" #print " requested_type=", t #print " data=", d if t[0] in NS.EXSD_L: if t[1] == "integer": try: d = int(d) if len(attrs): d = long(d) except: d = long(d) return d if self.intlimits.has_key (t[1]): # integer types l = self.intlimits[t[1]] try: d = int(d) except: d = long(d) if l[1] != None and d < l[1]: raise UnderflowError, "%s too small" % d if l[2] != None and d > l[2]: raise OverflowError, "%s too large" % d if l[0] or len(attrs): return long(d) return d if t[1] == "string": if len(attrs): return unicode(dnn) try: return str(dnn) except: return dnn if t[1] == "boolean": d = d.strip().lower() if d in ('0', 'false'): return 0 if d in ('1', 'true'): return 1 raise AttributeError, "invalid boolean value" if t[1] in ('double','float'): l = self.floatlimits[t[1]] s = d.strip().lower() d = float(s) if config.strict_range: if d < l[1]: raise UnderflowError if d > l[2]: raise OverflowError else: # some older SOAP impementations (notably SOAP4J, # Apache SOAP) return "infinity" instead of "INF" # so check the first 3 characters for a match. if s == "nan": return fpconst.NaN elif s[0:3] in ("inf", "+inf"): return fpconst.PosInf elif s[0:3] == "-inf": return fpconst.NegInf if fpconst.isNaN(d): if s != 'nan': raise ValueError, "invalid %s: %s" % (t[1], s) elif fpconst.isNegInf(d): if s != '-inf': raise UnderflowError, "%s too small: %s" % (t[1], s) elif fpconst.isPosInf(d): if s != 'inf': raise OverflowError, "%s too large: %s" % (t[1], s) elif d < 0 and d < l[1]: raise UnderflowError, "%s too small: %s" % (t[1], s) elif d > 0 and ( d < l[0] or d > l[2] ): raise OverflowError, "%s too large: %s" % (t[1], s) elif d == 0: if type(self.zerofloatre) == StringType: self.zerofloatre = re.compile(self.zerofloatre) if self.zerofloatre.search(s): raise UnderflowError, "invalid %s: %s" % (t[1], s) return d if t[1] in ("dateTime", "date", "timeInstant", "time"): return self.convertDateTime(d, t[1]) if t[1] == "decimal": return float(d) if t[1] in ("language", "QName", "NOTATION", "NMTOKEN", "Name", "NCName", "ID", "IDREF", "ENTITY"): return collapseWhiteSpace(d) if t[1] in ("IDREFS", "ENTITIES", "NMTOKENS"): d = collapseWhiteSpace(d) return d.split() if t[0] in NS.XSD_L: if t[1] in ("base64", "base64Binary"): if d: return base64.decodestring(d) else: return '' if t[1] == "hexBinary": if d: return decodeHexString(d) else: return if t[1] == "anyURI": return urllib.unquote(collapseWhiteSpace(d)) if t[1] in ("normalizedString", "token"): return collapseWhiteSpace(d) if t[0] == NS.ENC: if t[1] == "base64": if d: return base64.decodestring(d) else: return '' if t[0] == NS.XSD: if t[1] == "binary": try: e = attrs[(None, 'encoding')] if d: if e == 'hex': return decodeHexString(d) elif e == 'base64': return base64.decodestring(d) else: return '' except: pass raise Error, "unknown or missing binary encoding" if t[1] == "uri": return urllib.unquote(collapseWhiteSpace(d)) if t[1] == "recurringInstant": return self.convertDateTime(d, t[1]) if t[0] in (NS.XSD2, NS.ENC): if t[1] == "uriReference": return urllib.unquote(collapseWhiteSpace(d)) if t[1] == "timePeriod": return self.convertDateTime(d, t[1]) if t[1] in ("century", "year"): return self.convertDateTime(d, t[1]) if t[0] in (NS.XSD, NS.XSD2, NS.ENC): if t[1] == "timeDuration": return self.convertDateTime(d, t[1]) if t[0] == NS.XSD3: if t[1] == "anyURI": return urllib.unquote(collapseWhiteSpace(d)) if t[1] in ("gYearMonth", "gMonthDay"): return self.convertDateTime(d, t[1]) if t[1] == "gYear": return self.convertDateTime(d, t[1]) if t[1] == "gMonth": return self.convertDateTime(d, t[1]) if t[1] == "gDay": return self.convertDateTime(d, t[1]) if t[1] == "duration": return self.convertDateTime(d, t[1]) if t[0] in (NS.XSD2, NS.XSD3): if t[1] == "token": return collapseWhiteSpace(d) if t[1] == "recurringDate": return self.convertDateTime(d, t[1]) if t[1] == "month": return self.convertDateTime(d, t[1]) if t[1] == "recurringDay": return self.convertDateTime(d, t[1]) if t[0] == NS.XSD2: if t[1] == "CDATA": return collapseWhiteSpace(d) raise UnknownTypeError, "unknown type `%s'" % (str(t[0]) + ':' + t[1])
def convertToBasicTypes(self, d, t, attrs, config=Config): dnn = d or '' if t[0] in NS.EXSD_L: if t[1] == "integer": try: d = int(d) if len(attrs): d = long(d) except: d = long(d) return d if self.intlimits.has_key(t[1]): # integer types l = self.intlimits[t[1]] try: d = int(d) except: d = long(d) if l[1] != None and d < l[1]: raise UnderflowError, "%s too small" % d if l[2] != None and d > l[2]: raise OverflowError, "%s too large" % d if l[0] or len(attrs): return long(d) return d if t[1] == "string": if len(attrs): return unicode(dnn) try: return str(dnn) except: return dnn if t[1] == "boolean": d = d.strip().lower() if d in ('0', 'false'): return 0 if d in ('1', 'true'): return 1 raise AttributeError, "invalid boolean value" if t[1] in ('double', 'float'): l = self.floatlimits[t[1]] s = d.strip().lower() d = float(s) if config.strict_range: if d < l[1]: raise UnderflowError if d > l[2]: raise OverflowError else: # some older SOAP impementations (notably SOAP4J, # Apache SOAP) return "infinity" instead of "INF" # so check the first 3 characters for a match. if s == "nan": return fpconst.NaN elif s[0:3] in ("inf", "+inf"): return fpconst.PosInf elif s[0:3] == "-inf": return fpconst.NegInf if fpconst.isNaN(d): if s != 'nan': raise ValueError, "invalid %s: %s" % (t[1], s) elif fpconst.isNegInf(d): if s != '-inf': raise UnderflowError, "%s too small: %s" % (t[1], s) elif fpconst.isPosInf(d): if s != 'inf': raise OverflowError, "%s too large: %s" % (t[1], s) elif d < 0 and d < l[1]: raise UnderflowError, "%s too small: %s" % (t[1], s) elif d > 0 and (d < l[0] or d > l[2]): raise OverflowError, "%s too large: %s" % (t[1], s) elif d == 0: if type(self.zerofloatre) == StringType: self.zerofloatre = re.compile(self.zerofloatre) if self.zerofloatre.search(s): raise UnderflowError, "invalid %s: %s" % (t[1], s) return d if t[1] in ("dateTime", "date", "timeInstant", "time"): return self.convertDateTime(d, t[1]) if t[1] == "decimal": return float(d) if t[1] in ("language", "QName", "NOTATION", "NMTOKEN", "Name", "NCName", "ID", "IDREF", "ENTITY"): return collapseWhiteSpace(d) if t[1] in ("IDREFS", "ENTITIES", "NMTOKENS"): d = collapseWhiteSpace(d) return d.split() if t[0] in NS.XSD_L: if t[1] in ("base64", "base64Binary"): if d: return base64.decodestring(d) else: return '' if t[1] == "hexBinary": if d: return decodeHexString(d) else: return if t[1] == "anyURI": return urllib.unquote(collapseWhiteSpace(d)) if t[1] in ("normalizedString", "token"): return collapseWhiteSpace(d) if t[0] == NS.ENC: if t[1] == "base64": if d: return base64.decodestring(d) else: return '' if t[0] == NS.XSD: if t[1] == "binary": try: e = attrs[(None, 'encoding')] if d: if e == 'hex': return decodeHexString(d) elif e == 'base64': return base64.decodestring(d) else: return '' except: pass raise Error, "unknown or missing binary encoding" if t[1] == "uri": return urllib.unquote(collapseWhiteSpace(d)) if t[1] == "recurringInstant": return self.convertDateTime(d, t[1]) if t[0] in (NS.XSD2, NS.ENC): if t[1] == "uriReference": return urllib.unquote(collapseWhiteSpace(d)) if t[1] == "timePeriod": return self.convertDateTime(d, t[1]) if t[1] in ("century", "year"): return self.convertDateTime(d, t[1]) if t[0] in (NS.XSD, NS.XSD2, NS.ENC): if t[1] == "timeDuration": return self.convertDateTime(d, t[1]) if t[0] == NS.XSD3: if t[1] == "anyURI": return urllib.unquote(collapseWhiteSpace(d)) if t[1] in ("gYearMonth", "gMonthDay"): return self.convertDateTime(d, t[1]) if t[1] == "gYear": return self.convertDateTime(d, t[1]) if t[1] == "gMonth": return self.convertDateTime(d, t[1]) if t[1] == "gDay": return self.convertDateTime(d, t[1]) if t[1] == "duration": return self.convertDateTime(d, t[1]) if t[0] in (NS.XSD2, NS.XSD3): if t[1] == "token": return collapseWhiteSpace(d) if t[1] == "recurringDate": return self.convertDateTime(d, t[1]) if t[1] == "month": return self.convertDateTime(d, t[1]) if t[1] == "recurringDay": return self.convertDateTime(d, t[1]) if t[0] == NS.XSD2: if t[1] == "CDATA": return collapseWhiteSpace(d) raise UnknownTypeError, "unknown type `%s'" % (t[0] + ':' + t[1])