def news(offset, limit=20): result = {} items = [] category = request.args.get('category') keyword = request.args.get('keyword') if offset < 0 or limit <= 0: result = Result(code=Result.FAIL, msg='offset或limit参数有误!') return result.to_json() # 筛选字段,过滤 _id(ObjectId) 避免序列化异常 column = {'_id': 0} # 组装查询条件,无法拼接出带模糊查询的 sql,pymongo 就是个垃圾!!! # condition = {} # if category: # condition['news_web_category'] = category # if keyword: # regex = {'$regex': '.*{}.*'.format(keyword)} # condition['new_title'] = regex # print(condition) try: if not category and not keyword: items = list(mongo.collection.find({}, column).sort('news_datetime', -1).skip(offset).limit(limit)) count = mongo.collection.find({}, column).count() result = PageResult(items, count=count) if category and not keyword: items = list(mongo.collection.find({'news_web_category': category}, column).sort('news_datetime', -1).skip( offset).limit(limit)) count = mongo.collection.find({'news_web_category': category}, column).count() result = PageResult(items, count=count) if not category and keyword: items = list( mongo.collection.find({'news_title': re.compile(keyword, re.IGNORECASE)}, column).sort('news_datetime', -1).skip( offset).limit(limit)) count = mongo.collection.find({'news_title': re.compile(keyword, re.IGNORECASE)}, column).count() result = PageResult(items, count=count) if keyword and category: items = list( mongo.collection.find({'news_web_category': category, 'news_title': re.compile(keyword, re.IGNORECASE)}, column).sort('news_datetime', -1).skip(offset).limit(limit)) count = mongo.collection.find( {'news_web_category': category, 'news_title': re.compile(keyword, re.IGNORECASE)}, column).count() result = PageResult(items, count=count) except Exception as e: result = Result(code=Result.FAIL, msg=e) return result.to_json()
# for file in filelist: # src_name = file.split('.')[0] # result = num_recog.main(os.path.join(path,file)) # print src_name,result # if int(src_name) == int(result): # right += 1 # else: # continue # right_rate = float(right)/len(filelist) # print ('rate is {}%'.format(right_rate*100)) if __name__ == "__main__": result = Result() img_type = sys.argv[1] img_file = sys.argv[2] # img_type = "file" # img_file = "D:/machine_learning/recognization/insurance_doc/1.jpg" try: num_recog = Number_recognition() predict_data = num_recog.main(img_file) data = {'captcha': predict_data} result.code = code.SUCCESS result.msg = "success" result.data = data except Exception, e: exstr = traceback.format_exc() logging.info('#####系统异错误error:%s', exstr) result.code = code.FAIL result.msg = sys.exc_info() print result.to_json()