2015-10-16 08:50:44 +00:00
|
|
|
# encoding: UTF-8
|
|
|
|
|
|
|
|
"""
|
|
|
|
包含一些开放中常用的函数
|
|
|
|
"""
|
|
|
|
|
|
|
|
import decimal
|
2016-04-28 15:03:36 +00:00
|
|
|
import json
|
2015-10-16 08:50:44 +00:00
|
|
|
|
2016-04-02 09:04:58 +00:00
|
|
|
MAX_NUMBER = 10000000000000
|
2015-10-16 08:50:44 +00:00
|
|
|
MAX_DECIMAL = 4
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
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)
|
|
|
|
|
2016-04-20 15:14:21 +00:00
|
|
|
return unicode(value)
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def loadMongoSetting():
|
|
|
|
"""载入MongoDB数据库的配置"""
|
|
|
|
try:
|
|
|
|
f = file("VT_setting.json")
|
|
|
|
setting = json.load(f)
|
|
|
|
host = setting['mongoHost']
|
|
|
|
port = setting['mongoPort']
|
|
|
|
except:
|
|
|
|
host = 'localhost'
|
|
|
|
port = 27017
|
|
|
|
|
|
|
|
return host, port
|
|
|
|
|
2016-04-28 23:47:29 +00:00
|
|
|
|