def readConf(self, file_name, cut = "::"): try: conf = {"beta": [], "fields": [], "alpha": [], "funcs": []} with open(file_name) as file: for line in file: params = line.split("#")[0].strip().split(cut) if len(params) == 2: if params[0] == "score": conf["beta"].append(float(params[1])) conf["alpha"].append([]) conf["funcs"].append([]) else: conf[params[0]] = params[1] elif len(params) == 3: if len(conf["beta"]) == 1: conf["fields"].append(params[0]) conf["alpha"][-1].append(float(params[1])) conf["funcs"][-1].append(params[2]) elif len(params) == 1: continue else: return None return conf except Exception, e: print tool.get_cur_info(), e return None
def test(): try: rank = rankData() conf = rank.readConf("./conf/setting.txt") input = rank.readDB(conf) #input = {"name": 2, "city": 4} #print input output = rank.compute(conf, input) #print output rank.writeDB(conf, output) except Exception, e: print tool.get_cur_info(), e
def writeDB(self, conf, datas): try: client = MongoClient(conf["url"]) tb = client[conf["db_name"]][conf["tb_name"]] for key in datas[0]: tb.ensure_index(key, backgroud=True) for data in datas: tb.save(data) client.close() return True except Exception, e: print tool.get_cur_info(), e return False
def readDB(self, conf): try: client = MongoClient(conf["url"]) tb = client[conf["db_name"]]["ceylon"] datas = [] for data in tb.find(): if len(datas) == 1: break datas.append(data) client.close() return datas except Exception, e: print tool.get_cur_info(), e return None