def generate_dc_questions(challenge_name, lex, challenge_date): """ Generate the questions for a daily challenge. Returns: A tuple (questions, time_secs) questions is of type Questions """ logger.debug('Trying to create challenge {} for {} ({})'.format( challenge_name, lex, challenge_date)) db = WordDB(lex.lexiconName) # capture number. first try to match to today's lists m = re.match("Today's (?P<length>[0-9]+)s", challenge_name.name) if m: word_length = int(m.group('length')) if word_length < 2 or word_length > 15: return None # someone is trying to break my server >:( logger.info('Generating daily challenges %s %d', lex, word_length) min_p = 1 # lengthCounts is a dictionary of strings as keys max_p = json.loads(lex.lengthCounts)[str(word_length)] r = range(min_p, max_p + 1) random.shuffle(r) # Just the first 50 elements for the daily challenge. alphagrams = db.alphagrams_by_probability_list(r[:50], word_length) return db.get_questions(alphagrams), challenge_name.timeSecs # There was no match, check other possible challenge names. if challenge_name.name == DailyChallengeName.WEEKS_BINGO_TOUGHIES: alphagrams = generate_toughies_challenge(lex, challenge_date) random.shuffle(alphagrams) return db.get_questions(alphagrams), challenge_name.timeSecs elif challenge_name.name == DailyChallengeName.BLANK_BINGOS: questions = generate_blank_bingos_challenge(lex, challenge_date) questions.shuffle() return questions, challenge_name.timeSecs elif challenge_name.name == DailyChallengeName.BINGO_MARATHON: questions = Questions() for lgt in (7, 8): min_p = 1 max_p = json.loads(lex.lengthCounts)[str(lgt)] r = range(min_p, max_p + 1) random.shuffle(r) questions.extend(db.get_questions( db.alphagrams_by_probability_list(r[:50], lgt))) return questions, challenge_name.timeSecs # elif challenge_name.name in (DailyChallengeName.COMMON_SHORT, # DailyChallengeName.COMMON_LONG): # questions = generate_common_words_challenge( # challenge_name.name) # random.shuffle(questions) # return questions, challenge_name.timeSecs return None
def generate_blank_bingos_challenge(lex, ch_date): """ Contact blank challenges server and generate said challenges. """ bingos = Questions() logger.debug('in generate_blank_bingos_challenge') for length in (7, 8): try: challs = gen_blank_challenges(length, lex.lexiconName, 2, 25, 5) except MacondoError: logger.exception(u'[event=macondoerror]') return bingos for chall in challs: question = Question(Alphagram(chall['q']), []) question.set_answers_from_word_list(chall['a']) bingos.append(question) return bingos
def get_questions_by_condition(db, min_prob, max_prob, length, condition, condition_type='alphagram'): """ Get all questions that match the condition. Return a to_python representation, ready for saving into the list. """ qs = Questions() to_filter = db.get_questions_for_probability_range(min_prob, max_prob, length) for q in to_filter.questions_array(): if condition_type == Condition.ALPHAGRAM: test = condition(q.alphagram.alphagram) elif condition_type == Condition.WORD: test = any(condition(word) for word in q.answers) if test: # print 'passed test', q, q['q'], q['a'][0].word qs.append(q) return qs.to_python()