2015-10-19 08:42:17 +00:00
|
|
|
# encoding: UTF-8
|
|
|
|
|
|
|
|
"""
|
|
|
|
包含一些开放中常用的函数
|
|
|
|
"""
|
|
|
|
|
|
|
|
import decimal
|
|
|
|
|
|
|
|
MAX_NUMBER = 1000000000
|
|
|
|
MAX_DECIMAL = 4
|
|
|
|
|
2015-11-15 07:53:54 +00:00
|
|
|
# ----------------------------------------------------------------------
|
2015-10-19 08:42:17 +00:00
|
|
|
def safeUnicode(value):
|
|
|
|
"""检查接口数据潜在的错误,保证转化为的字符串正确"""
|
|
|
|
# 检查是数字接近0时会出现的浮点数上限
|
|
|
|
if type(value) is int or type(value) is float:
|
|
|
|
if value > MAX_NUMBER:
|
|
|
|
value = 0
|
|
|
|
|
|
|
|
# 检查防止小数点位过多
|
|
|
|
if type(value) is float:
|
|
|
|
d = decimal.Decimal(str(value))
|
|
|
|
if abs(d.as_tuple().exponent) > MAX_DECIMAL:
|
|
|
|
value = round(value, ndigits=MAX_DECIMAL)
|
|
|
|
|
|
|
|
return unicode(value)
|