コード例 #1
0
ファイル: models.py プロジェクト: practo/MyCQ
 def save(self):
     if self.id is None:
         DESIRED_QUESTIONS = 20
         self.id = str(uuid4())
         all_questions = user_store.keys('question:*')
         all_questions = [int(x[9:]) for x in all_questions]
         if len(all_questions) < DESIRED_QUESTIONS:
             raise Exception("Not enough questions")
         random_questions = random.sample(all_questions, DESIRED_QUESTIONS)
         for q in random_questions:
             user_store.sadd('user_questions:' + self.id, q)
     user_store.hmset('user:'******'id': self.id,
         'name': self.name,
         'email': self.email,
         'branch': self.branch,
         'roll_no': self.roll_no,
         'score': self.score
     })
コード例 #2
0
ファイル: calculate_score.py プロジェクト: practo/MyCQ
from mycq import user_store
import csv
import sys

def calculate_score(user_id):
    score = 0
    answers = user_store.hgetall('user_answers:' + user_id)
    for qid in answers.keys():
        correct_answer = user_store.hget('question:' + str(qid), 'correct_answer')
        if answers[qid] == correct_answer:
            score += 10
        else:
            score -= 5
    return score

user_keys = user_store.keys('user:*')
user_scores = []
for user_key in user_keys:
    user = user_store.hgetall(user_key)
    user_score = calculate_score(user['id'])
    user_scores.append((user['id'], user['name'], user['email'], user['roll_no'], user['branch'], user_score))
    user_store.hset(user_key, 'score', user_score)

top_users = sorted(user_scores, key=lambda x:x[-1], reverse=True)
cw = csv.writer(sys.stdout)
cw.writerow(('id', 'name', 'email', 'roll_no', 'branch', 'score'))
for user in top_users:
    cw.writerow(user)