-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathserver.py
80 lines (63 loc) · 3.39 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import random
from flask import Flask, request, jsonify
def take_exam(tasks):
answers = {}
for task in tasks:
question = task['question']
if question['type'] == 'choice':
# pick a random answer
choice = random.choice(question['choices'])
answer = choice['id']
elif question['type'] == 'multiple_choice':
# pick a random number of random choices
min_choices = question.get('min_choices', 1)
max_choices = question.get('max_choices', len(question['choices']))
n_choices = random.randint(min_choices, max_choices)
random.shuffle(question['choices'])
answer = [
choice['id']
for choice in question['choices'][:n_choices]
]
elif question['type'] == 'matching':
# match choices at random
random.shuffle(question['choices'])
answer = {
left['id']: choice['id']
for left, choice in zip(question['left'], question['choices'])
}
elif question['type'] == 'text':
if question.get('restriction') == 'word':
# pick a random word from the text
words = [word for word in task['text'].split() if len(word) > 1]
answer = random.choice(words)
else:
# random text generated with https://fish-text.ru
answer = (
'Для современного мира реализация намеченных плановых заданий позволяет '
'выполнить важные задания по разработке новых принципов формирования '
'материально-технической и кадровой базы. Господа, реализация намеченных '
'плановых заданий играет определяющее значение для модели развития. '
'Сложно сказать, почему сделанные на базе интернет-аналитики выводы призывают '
'нас к новым свершениям, которые, в свою очередь, должны быть в равной степени '
'предоставлены сами себе. Ясность нашей позиции очевидна: базовый вектор '
'развития однозначно фиксирует необходимость существующих финансовых и '
'административных условий.'
)
else:
raise RuntimeError('Unknown question type: {}'.format(question['type']))
answers[task['id']] = answer
return answers
app = Flask(__name__)
@app.route('/ready')
def http_ready():
return 'OK'
@app.route('/take_exam', methods=['POST'])
def http_take_exam():
request_data = request.get_json()
tasks = request_data['tasks']
answers = take_exam(tasks)
return jsonify({
'answers': answers
})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000)