def get_recommendation(uid): ''' random from a 'pool' of object what is not already in 'control' return None if cannot find one ''' def get_unique_random(control, pool): # loop until while True: # if exhausted of choices, return None if len(pool) <= 0: return None # re-random choice = random.choice(pool) # remove the choice from pool try: pool.remove(choice) except ValueError: pass # or scream: thing not in some_list! except AttributeError: pass # call security, some_list not quacking like a list! # return the choice if unique, otherwise keep looping if (choice not in control): return choice recommendations = [] # -------------------------------------------- # From User Top 5 # -------------------------------------------- # choose more from higher view count category weight = 4; top5 = db.get_top_five_categories(uid) logger.debug('*'* 120) logger.debug('Selecting from User Preferences') logger.debug('*'*120) logger.debug('\n\r') for cat in top5: logger.debug('x'* 100) logger.debug('for categories '+cat['cid']) logger.debug('x'*100) logger.debug('\n\r') logger.debug('Selecting from user with rating = 0') logger.debug('|'*60) # this ensure we have at least one user with 0 rating for each category products = db.queryDB2(get_product_for_cid_with_rating_condition.format(cat['cid'], '= 0')) db.resolveProducts(products) choice = get_unique_random(recommendations, products) logger.debug('Choice:') print_choice(choice) if choice is not None: recommendations.append(choice) logger.debug('Recommendations:') print_products(recommendations) logger.debug('Selecting from user with rating > 0') logger.debug('|'*50) products = db.queryDB2(get_product_for_cid_with_rating_condition.format(cat['cid'], '> 0')) db.resolveProducts(products) logger.debug('Products:') print_products(products) for x in range(0,weight): if len(products) <= 0: break choice = random.choice(products) recommendations.append(choice) # remove the choosen item so the item # cannot appear twice in the same list products.remove(choice) logger.debug('Recommendations:') print_products(recommendations) weight -= 1 # -------------------------------------------- # Fill with Global Top 5 # -------------------------------------------- # choose more from higher view count category if len(recommendations) < 10: weight = 5; global_top5 = db.get_global_top_five() for cat in global_top5: products = db.get_product_category(cat['cid']) for x in range(0,weight): choice = get_unique_random(recommendations, products) if choice is None: break recommendations.append(choice) weight -= 1 # -------------------------------------------- # Fill with Random # -------------------------------------------- # fill remaining space with random products # note that 'if' were used instead of 'while' # to prevent possible infinite loop from # exhaustion of chioce in the entire system if len(recommendations) < 10: products = db.get_random_product() for x in range(0,len(recommendations)): choice = get_unique_random(recommendations, products) if choice is None: break recommendations.append(choice) return recommendations;
def handle_message(msg): roomid = extract_chat_id(msg) msg['from_buyer'] = (msg['buyer_uid'] == msg['sender']) db.storeMessage(msg) emit('msg_receive', msg, room=roomid)
def handle_connection(uid): print('a user has connected [uid]: ' + uid) chatrooms = db.getChatRooms(uid) for cr in chatrooms: roomid = extract_chat_id(cr) join_room(roomid)
def get_category_info(cid): return jsonify({'result': db.get_category_info(cid)})
def post_product(): return jsonify({'resut': db.post_product(request.json)})
def get_Messages(): return db.get_Messages(request.json)
def get_product_count(cid): return jsonify({'result': db.get_product_count(cid)})
def getChatRooms(uid): result = db.getChatRooms(uid) return jsonify({'result': result})
def post_create_chat_room(): return db.post_create_chat_room(request.json)
def get_avg_rating(sid): return db.get_avg_rating(sid)
def post_track(): return db.post_track_user_data(request.json)
def post_submit_feedback(): if request.json['rating'] <= 3: print('Notify Kaidee Stuff!') return db.post_submit_feedback(request.json)
def get_category_list(): return db.get_category_list()
def get_product_category(cid): result = db.get_product_category(cid) return jsonify({'result': result})
def get_Product(iid): return db.get_Product(iid)
def select(): return jsonify(db.selectAll())