Esempio n. 1
0
    def find_match(self):
        candidate_beers = [] # empty array which will contain the cadidates
        # Each bear is coupled to a meal(category). If a beer's meal(category)
        # is the same as the user input it will be considered a candidate.

        # First, we search meal names:
        query = Beer.select().where(Beer.meal == self.input)
        if len(query) > 0: # if the query yields some beers...
            for candidate in query:
                if candidate.name not in [beer.name for beer in candidate_beers]:
                    candidate_beers.append(candidate) #...add those beers to the candidate list

        # Second, we search meal categories:
        query = Beer.select().where(Beer.mealcategory == self.input)
        if len(query) > 0: # if the query yields some beers...
            for candidate in query:
                if candidate.name not in [beer.name for beer in candidate_beers]:
                    candidate_beers.append(candidate) #...add those beers to the candidate list

        # And lastly, we return the best choice:
        if len(candidate_beers) > 0: # if there are any candidate beers...
            candidate_beers = self.assign_scores(candidate_beers) # assign penalty scores to those beers
            candidate_beers.sort(key=lambda x: x.penalties) # sort the candidate list ascending by penalty count

            # This code block prints all the found candidates and their penalty
            # scores to the terminal. Uncomment for debugging purposes, or if
            # you're just curious. ;-)
            for beer in candidate_beers:
               print beer.name.encode('utf8', 'replace')
               print beer.penalties

            return candidate_beers[0] # return the first (best) candidate

        else: # if there are no candidate beers...
            self.emergency_plan(sirens=True) # ...sound the alarm!
            return None
Esempio n. 2
0
 def check_database_for(self, meal_name=None, beer_name=None,
                        category_name=None, user_chat_id=None):
     """Checks the database for various things. Returns truth value."""
     if meal_name:
         query = Meal.select().where(Meal.name == meal_name)
     elif beer_name:
         query = Beer.select().where(Beer.name == beer_name)
     elif category_name:
         query = MealCategory.select().where(MealCategory.name == category_name)
     elif user_chat_id:
         query = User.select().where(User.chat_id == user_chat_id)
     else:
         print 'No query specified!'
         return False
     if len(query) > 0:
         return True # return true when results are found
     return False