-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·117 lines (92 loc) · 3.71 KB
/
main.py
File metadata and controls
executable file
·117 lines (92 loc) · 3.71 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python
import sys
import os
import time
import random
import tempfile
import argparse
import Menu
class PopQ(object):
resource_prefix = '/tmp/'
temp_file = "popresource.txt"
def _create_nonexisting_resourcefile(self, resource_file_path):
if not os.path.isfile(resource_file_path):
open(resource_file_path).close()
def _configure_popq(self, args):
self.question_files = args.question_files
if args.resource_file:
self.resource_file_path = args.resource_file
else:
self.resource_file_path = self.resource_prefix + self.temp_file
def reset(self, num_correct):
self._correct = num_correct
self.questions = list()
self.checklist = list()
self.fname = '/'.join([tempfile.gettempdir(), self.temp_file]) # Touch a file for first use
self.n_questions = 0
self.total = 1
def read_questions(self, question_files):
questions = []
for filepath in self.question_files:
with open(filepath) as fp:
print("Opening: " + filepath)
fp_code = fp.read()
if fp_code:
content = eval(fp_code)
print("Loaded :",len(content)," questions")
questions += content
else:
print("Error while importing %s" % filepath)
try:
time.ctime(os.path.getmtime(self.fname))
except OSError as e:
with file(self.fname, 'a'):
os.utime(self.fname, None)
print("Tempfile stored in %s" % self.fname)
lasttime = time.ctime(os.path.getmtime(self.fname))
print("last modified: %s" % lasttime)
return questions
def __init__(self, num_correct=10):
self.reset(num_correct)
parser = argparse.ArgumentParser(description = 'A terminal pop-question memory test')
parser.add_argument('-q', '--question_files', nargs='+', required = True, type = str)
parser.add_argument('-r', '--resource_file', type = str)
self._configure_popq(parser.parse_args())
print("Resource file: " + self.resource_file_path)
self._create_nonexisting_resourcefile(self.resource_file_path)
self.questions = self.read_questions(self.question_files)
self.n_questions = len(self.questions)
def new_questionnaire(self):
"""Start a new questionnaire round"""
self.correct = min(self._correct, self.n_questions)
self.total = self.correct
for x in range(0, min(self._correct, self.n_questions)):
try:
rand_num = int(random.uniform(0, len(self.questions)))
while rand_num in self.checklist:
rand_num = int(random.uniform(0, len(self.questions)))
self.checklist.append(rand_num)
randq = self.questions[rand_num]
print(randq[0])
ans = input("> ")
if ans.lower() != randq[1].lower():
print("The answer is: %s" % randq[1])
self.correct -= 1
except KeyboardInterrupt:
os.system("clear")
exit(1)
except EOFError:
os.system("clear")
exit(1)
def __str__(self):
"""Presented score string from latest round"""
return "Score: %1.2f%s [%d/%d]" % (float(self.correct)/self.total*100.,\
'%', self.correct, self.total)
def print_score(self):
"""Print the core from latest round"""
os.system("clear")
print(self.__str__())
if __name__ == "__main__":
Q = PopQ()
Q.new_questionnaire()
Q.print_score()