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 get_product_category(cid): result = db.get_product_category(cid) return jsonify({'result': result})