Ejemplo n.º 1
0
def submit_it(args):
    data = srbjson.extract_data(Const.cache_dir + '/config',
                                srbjson.global_template)
    u_name = data['user']
    pswd = data['pswd']
    temp_prob = Problem(args['p_name'], args['c_name'], args['c_type'])

    if (not temp_prob.is_good):
        print(Colour.YELLOW + 'Test cases not found locally...' + Colour.END)
        # choice to fetch whole contest or problem
        temp_prob.pull_problem()

    if (not temp_prob.is_good):
        print(Colour.FULLRED +
              'Sorry! Due to Connection problem. Unable to test your file' +
              Colour.END)
        return

    runner = Runner(args, temp_prob)
    runner.run()

    submit = Submit(args['c_name'] + args['p_name'], args['inp'], args['user'],
                    args['pswd'])

    if (runner.result == 'GOOD'):
        submit.submit()
    elif (runner.result == 'CANT_SAY'):
        runner.print_table()
        submit.submit()
    else:
        print(Colour.FULLRED + 'ERROR locally, so wont upload faulty file' +
              Colour.END)
        runner.print_table()
        if (args['force']): submit.submit()
Ejemplo n.º 2
0
    def test_fill_quotes(self):
        questions = [ "Will you descend into the belly of the whale?",
            "Will you integrate your shadow?",
            "Will you clean up your room?",
            "Will you seek out a heavy load?",
            "Will you leave Neverland?",
            "Will you stand up straight with your shoulders back?",
            "Will you say what you think?"]

        for q in questions:
            self.add_problem(Problem(q,"Yes"))
Ejemplo n.º 3
0
def run(args):
    temp_contest = Contest(args['c_name'], args['c_type'])
    if (not temp_contest.is_good):
        temp_contest.pull_contest()

    temp_prob = Problem(args['p_name'], args['c_name'], args['c_type'])
    if (not temp_prob.is_good):
        print(Colour.YELLOW + 'Test cases not found locally...' + Colour.END)
        # choice to fetch whole contest or problem
        temp_prob.pull_problem()

    if (not temp_prob.is_good):
        print(Colour.FULLRED +
              'Sorry! Due to Connection problem. Unable to test your file' +
              Colour.END)
        return

    runner = Runner(args, temp_prob)
    runner.run()
    runner.print_table()
Ejemplo n.º 4
0
    def load(self,filename):
        with open(filename,'r') as f:
            lines = f.readlines()

        self.name = lines[0].rstrip()
        for i in range(1,len(lines)):
            values = lines[i].split("|") #get | seperated values
            p = ""
            if values[0] == "Problem":
                p = Problem(values[1],values[2])
            elif values[0] == "MathProblem":
                p = MathProblem(values[1],values[2],values[3],values[4])
            self.add_problem(p)
Ejemplo n.º 5
0
    def load(self, filename):
        with open(filename, 'r') as f:
            lines = f.readlines()

        self.name = lines[0].rstrip()
        temp_points = [int(num) for num in lines[1].rstrip().split()]
        for i in range(2, len(lines)):
            values = lines[i].split("|")  #get | seperated values
            if values[0] == "Problem":
                p = Problem(values[1], values[2])
            elif values[0] == "MathProblem":
                p = MathProblem(values[1], values[2], values[3], values[4])
            self.add_problem(p, temp_points[i - 2])
Ejemplo n.º 6
0
    def _fetch_contest_home(self):
        '''
        tries to fetch these things and also dump them if fetched
            contest:
                ann_arr
                c_title
                prob_mapp
                num_prob
                p_name_list

            problem:
                p_title
                subm

        CAN BE CALLED TO FORCEFULLY UPDATE DATA, say subm during the contest
        '''
        soup = Soup.get_soup("https://codeforces.com/" + self.c_type + "/" +
                             self.c_name)

        if (soup is None):
            return

        # title
        rtable = soup.findAll('table', {'class': 'rtable'})[0]
        self.c_title = rtable.findAll('a')[0].get_text().strip()

        # prob table
        prob_table = soup.findAll('table', {'class': 'problems'})[0]
        prob_list = prob_table.findAll('tr')[1:]
        p_name_list = []

        for problem in prob_list:
            p_name = problem.findAll('td')[0].get_text().strip()
            p_name_list.append(p_name)
            p_title = problem.findAll('td')[1].findAll(
                'a')[0].get_text().strip()
            subm = problem.findAll('td')[3].get_text().strip().split('x')[-1]
            if (not p_name in self.prob_mapp.keys()):
                self.prob_mapp[p_name] = Problem(p_name, self.c_name,
                                                 self.c_type, p_title)
            self.prob_mapp[p_name].p_title = p_title
            self.prob_mapp[p_name].subm = subm
        self.num_prob = len(self.prob_mapp)
        self.p_name_list = p_name_list

        # announcements
        atable = soup.findAll('table', {'class': 'problem-questions-table'})[0]
        announce_arr = atable.findAll('tr')[1:]
        for ann in announce_arr:
            ann = ann.findAll('td')[-1].get_text().strip()
            self.announce_arr += [ann]
Ejemplo n.º 7
0
 def setUp(self):
     self.name = "Exam"
     self.exam = Exam(self.name)
     self.exam.test_fill_quotes()
     #self.exam.save("setup")
     self.problemA = Problem("Question A", "Answer A")
     self.problemB = Problem("Question B", "Answer B")
     self.problemC = Problem("Question C", "Answer C")
Ejemplo n.º 8
0
    def setUp(self):
        self.name = "Problem_Set"
        self.pset = ProblemSet(self.name)
        self.pset.test_fill_quotes()
        self.pset.test_fill_math(10)
        self.pset.test_fill(10)

        self.problemA = Problem("Question A", "Answer A")
        self.problemB = Problem("Question B", "Answer B")
        self.problemC = Problem("Question C", "Answer C")
Ejemplo n.º 9
0
    def _load_contest(self):
        '''
        loads contest from cache if exists else create an empty contest and loads it up
        also cleans useless folders in prob folder if they aren't in config of p_name_list
        '''
        data = srbjson.extract_data(self.dir + '/config',
                                    srbjson.contest_template)

        self.announce_arr = data['ann_arr']
        self.c_title = data['c_title']
        self.hash = data['hash']
        self.is_good = data['is_good']
        self.num_prob = data['num_prob']
        self.p_name_list = data['p_name_list']

        # problems
        path = self.dir + '/prob/'
        verify_folder(path)
        good_probs = 0
        prob_dir_list = [(a if os.path.isdir(path + a) else None)
                         for a in os.listdir(path)]
        prob_dirs = []
        for prob in prob_dir_list:  # keep only folders
            if (prob): prob_dirs += [prob]

        for a in prob_dirs:
            self.prob_mapp[a] = Problem(a, self.c_name, self.c_type)
            if (self.prob_mapp[a].is_good
                    and a in self.p_name_list):  # remove waste folders
                good_probs += 1
            else:
                print(Colour.RED + 'Removed Bad Problem : ' + a + Colour.END)
                shutil.rmtree(self.prob_mapp[a].dir)
                del self.prob_mapp[a]

        if (self.num_prob == -1):
            print(Colour.YELLOW + 'Contest not configured yet' + Colour.END)
            self.is_good = False
        elif (good_probs != self.num_prob):
            print(Colour.YELLOW + 'expected', self.num_prob, 'probs got',
                  good_probs, 'good probs', Colour.END)
            self.is_good = False
Ejemplo n.º 10
0
class ProblemSetTest(unittest.TestCase):
    def setUp(self):
        self.name = "Problem_Set"
        self.pset = ProblemSet(self.name)
        self.pset.test_fill_quotes()
        self.pset.test_fill_math(10)
        self.pset.test_fill(10)

        self.problemA = Problem("Question A", "Answer A")
        self.problemB = Problem("Question B", "Answer B")
        self.problemC = Problem("Question C", "Answer C")

    def test_get_name(self):
        self.assertEqual(self.name, self.pset.get_name())

    def test_set_name(self):
        self.pset.set_name("My Problem Set")
        self.assertEqual("My Problem Set", self.pset.get_name())

    def test_add_problem(self):
        self.pset.add_problem(self.problemA)
        self.assertEqual(self.pset.get_problem(self.pset.get_length() - 1),
                         self.problemA)

    def test_find_problem(self):
        self.assertEqual(self.pset.find_problem(self.problemA), -1)
        self.pset.add_problem(self.problemA)
        self.pset.add_problem(self.problemB)
        self.pset.add_problem(self.problemC)
        ind_probA = self.pset.find_problem(self.problemA)
        ind_probB = self.pset.find_problem(self.problemB)
        ind_probC = self.pset.find_problem(self.problemC)
        self.assertEqual(self.pset.get_problem(ind_probA), self.problemA)
        self.assertEqual(self.pset.get_problem(ind_probB), self.problemB)
        self.assertEqual(self.pset.get_problem(ind_probC), self.problemC)

    def test_remove_problem(self):
        self.assertEqual(self.pset.remove_problem(self.problemA), -1)

        p = self.pset.get_problem(0)
        self.assertEqual(self.pset.remove_problem(p), 0)
        self.assertEqual(self.pset.find_problem(p), -1)

    def test_replace_problem(self):
        p = self.pset.get_problem(0)
        self.pset.replace_problem(0, self.problemA)
        self.assertEqual(self.pset.find_problem(p), -1)
        self.assertEqual(self.pset.find_problem(self.problemA), 0)

    def test_insert_problem(self):
        p = self.pset.get_problem(5)
        self.pset.insert_problem(5, self.problemA)
        self.assertEqual(self.pset.get_problem(5), self.problemA)
        self.assertEqual(self.pset.get_problem(6), p)

    def test_swap_problem(self):
        self.assertEqual(self.pset.swap_problem(-1, 1), -1)
        self.assertEqual(self.pset.swap_problem(0, 0), -1)
        self.assertEqual(self.pset.swap_problem(1, -1), -1)

        p0 = self.pset.get_problem(0)  #problem in position zero orginally
        p1 = self.pset.get_problem(1)  #problem in position one  orginally
        self.assertEqual(self.pset.swap_problem(0, 1), 0)
        self.assertEqual(self.pset.get_problem(0), p1)  #p1 should be in pos 0
        self.assertEqual(self.pset.get_problem(1), p0)  #p0 should be in pos 1

    def test_move_problem(self):
        #dst > org
        ind_org = 0
        ind_dst = 10
        prob_org = self.pset.get_problem(ind_org)
        prob_dst = self.pset.get_problem(ind_dst)
        self.assertEqual(self.pset.move_problem(ind_org, ind_dst), 0)
        self.assertEqual(self.pset.get_problem(ind_dst), prob_org)
        self.assertEqual(self.pset.get_problem(ind_dst - 1), prob_dst)

        #dst < org
        ind_org = 12
        ind_dst = 1
        prob_org = self.pset.get_problem(ind_org)
        prob_dst = self.pset.get_problem(ind_dst)
        self.assertEqual(self.pset.move_problem(ind_org, ind_dst), 0)
        self.assertEqual(self.pset.get_problem(ind_dst), prob_org)
        self.assertEqual(self.pset.get_problem(ind_dst + 1), prob_dst)

    def test_pop_problem(self):
        pset_length = self.pset.get_length()
        last_problem = self.pset.get_problem(pset_length - 1)
        self.pset.pop_problem()
        self.assertEqual(self.pset.find_problem(last_problem), -1)
        self.assertEqual(self.pset.get_length(), pset_length - 1)

    def test_copy(self):
        pset_copy = self.pset.copy()
        self.assertFalse(pset_copy == self.pset)
        self.assertEqual(pset_copy.as_string(), self.pset.as_string())

    def test_consolidate(self):
        p1 = ProblemSet("", self.problemA, self.problemB)
        p2 = ProblemSet("", self.problemB, self.problemC)
        p3 = ProblemSet("", self.problemC, self.problemA)
        test_string = p1.as_string().rstrip() + p2.as_string().rstrip(
        ) + p3.as_string()
        p1.consolidate(p2, p3)
        self.assertEqual(p1.as_string(), test_string)

    def test_check_index(self):
        test = ProblemSet("", self.problemA, self.problemB, self.problemC)
        self.assertFalse(test.check_index(-1))
        self.assertFalse(test.check_index(test.get_length()))
        self.assertTrue(test.check_index(0))
        self.assertTrue(test.check_index(test.get_length() - 1))

    def test_as_string(self):
        ps = ProblemSet("ProblemSet")
        ps.add_problem(self.problemA)
        ps.add_problem(self.problemB)
        ps.add_problem(self.problemC)
        comp_str = "ProblemSet" + "\n" + self.problemA.as_string() + "\n"
        comp_str += self.problemB.as_string() + "\n" + self.problemC.as_string(
        ) + "\n"
        self.assertEqual(ps.as_string(), comp_str)

    def test_save_load(self):
        self.pset.save("deleteme")
        load_set = ProblemSet()
        load_set.load("deleteme")
        self.assertEqual(self.pset.as_string(), load_set.as_string())

    def test_txt(self):
        self.pset.as_text_file("tester")
Ejemplo n.º 11
0
 def setUp(self):
     self.q = "Question"
     self.a = "Answer"
     self.p  = Problem(self.q,self.a)
Ejemplo n.º 12
0
class ProblemTest(unittest.TestCase):

    def setUp(self):
        self.q = "Question"
        self.a = "Answer"
        self.p  = Problem(self.q,self.a)

    def test_get_question(self):
        self.assertEqual(self.p.get_question(),self.q)

    def test_get_answer(self):
        self.assertEqual(self.p.get_answer(),self.a)

    def test_set_question(self):
        self.p.set_question("New Question")
        self.assertEqual(self.p.get_question(),"New Question")

    def test_set_answer(self):
        self.p.set_answer("New Answer")
        self.assertEqual(self.p.get_answer(),"New Answer")

    def test_check_answer(self):
        self.assertTrue(self.p.check_answer(self.a))
        self.assertFalse(self.p.check_answer("not the answer"))

    def test_as_string(self):
        test_string = "Problem|Question|Answer|"
        self.assertEqual(self.p.as_string(),test_string)

    def test_copy(self):
        copy = self.p.copy()
        self.assertFalse(self.p is copy)
        self.assertEqual(self.p.as_string(),copy.as_string())
Ejemplo n.º 13
0
 def test_fill(self,n):
     for i in range(n):
         ind = str(self.get_length()+1)
         self.add_problem(Problem("Q"+str(ind) , "A"+str(ind)))
Ejemplo n.º 14
0
 def __init__(self, question="", answer="", solution="", hint=""):
     self.solution = solution
     self.hint = hint
     Problem.__init__(self, question, answer)
     self.type = "MathProblem"
Ejemplo n.º 15
0
class ExamTest(unittest.TestCase):
    def setUp(self):
        self.name = "Exam"
        self.exam = Exam(self.name)
        self.exam.test_fill_quotes()
        #self.exam.save("setup")
        self.problemA = Problem("Question A", "Answer A")
        self.problemB = Problem("Question B", "Answer B")
        self.problemC = Problem("Question C", "Answer C")

    def tearDown(self):
        del self.exam
        del self.problemA
        del self.problemB
        del self.problemC

    def test_get_name(self):
        self.assertEqual(self.name, self.exam.get_name())

    def test_set_name(self):
        self.exam.set_name("MyExam")
        self.assertEqual("MyExam", self.exam.get_name())

    def test_find_problem(self):
        self.assertEqual(self.exam.find_problem(self.problemA), -1)
        self.exam.add_problem(self.problemA, 1)
        self.exam.add_problem(self.problemB, 1)
        self.exam.add_problem(self.problemC, 1)
        indA = self.exam.find_problem(self.problemA)
        indB = self.exam.find_problem(self.problemB)
        indC = self.exam.find_problem(self.problemC)
        self.assertEqual(
            self.exam.get_problem(indA).as_string(), self.problemA.as_string())
        self.assertEqual(
            self.exam.get_problem(indB).as_string(), self.problemB.as_string())
        self.assertEqual(
            self.exam.get_problem(indC).as_string(), self.problemC.as_string())

    def test_remove_problem(self):
        self.assertEqual(self.exam.remove_problem(self.problemA), -1)

        p = self.exam.get_problem(0)
        self.assertEqual(self.exam.remove_problem(p), 0)
        self.assertEqual(self.exam.find_problem(p), -1)

    def test_replace_problem(self):
        p = self.exam.get_problem(0)
        self.assertTrue(self.exam.check_index(0))
        self.assertEqual(self.exam.replace_problem(0, self.problemA, 1), 0)
        self.assertEqual(self.exam.find_problem(p), -1)
        self.assertEqual(self.exam.find_problem(self.problemA), 0)

    def test_insert_problem(self):
        p = self.exam.get_problem(5)
        self.exam.insert_problem(5, self.problemA, 1)
        self.assertEqual(self.exam.get_problem(5), self.problemA)
        self.assertEqual(self.exam.get_problem(6), p)

    def test_swap_problem(self):
        self.assertEqual(self.exam.swap_problem(-1, 1), -1)
        self.assertEqual(self.exam.swap_problem(0, 0), -1)
        self.assertEqual(self.exam.swap_problem(1, -1), -1)

        p0 = self.exam.get_problem(0)  #problem in position zero orginally
        p1 = self.exam.get_problem(1)  #problem in position one  orginally
        self.assertEqual(self.exam.swap_problem(0, 1), 0)
        self.assertEqual(self.exam.get_problem(0), p1)  #p1 should be in pos 0
        self.assertEqual(self.exam.get_problem(1), p0)  #p0 should be in pos 1

    def test_move_problem(self):
        #dst > org
        ind_org = 0
        ind_dst = 5
        prob_org = self.exam.get_problem(ind_org)
        prob_dst = self.exam.get_problem(ind_dst)
        self.assertEqual(self.exam.move_problem(ind_org, ind_dst), 0)
        self.assertEqual(self.exam.get_problem(ind_dst), prob_org)
        self.assertEqual(self.exam.get_problem(ind_dst - 1), prob_dst)

        #dst < org
        ind_org = 5
        ind_dst = 1
        prob_org = self.exam.get_problem(ind_org)
        prob_dst = self.exam.get_problem(ind_dst)
        self.assertEqual(self.exam.move_problem(ind_org, ind_dst), 0)
        self.assertEqual(self.exam.get_problem(ind_dst), prob_org)
        self.assertEqual(self.exam.get_problem(ind_dst + 1), prob_dst)

    def test_pop_problem(self):
        exam_length = self.exam.get_length()
        last_problem = self.exam.get_problem(exam_length - 1)
        self.exam.pop_problem()
        self.assertEqual(self.exam.find_problem(last_problem), -1)
        self.assertEqual(self.exam.get_length(), exam_length - 1)

    def test_copy(self):
        exam_copy = self.exam.copy()
        self.assertFalse(exam_copy == self.exam)
        self.assertEqual(exam_copy.as_string(), self.exam.as_string())

    def test_check_index(self):
        self.assertFalse(self.exam.check_index(-1))
        self.assertFalse(self.exam.check_index(self.exam.get_length()))
        self.assertTrue(self.exam.check_index(0))
        self.assertTrue(self.exam.check_index(self.exam.get_length() - 1))

    def test_as_string(self):
        ex = Exam("Exam", [1, 1, 1],
                  ProblemSet("", self.problemA, self.problemB, self.problemC))
        points = ' '.join([str(i) for i in ex.get_points()])
        comp_str = "Exam" + "\n" + points + '\n' + self.problemA.as_string(
        ) + "\n"
        comp_str += self.problemB.as_string() + "\n" + self.problemC.as_string(
        ) + "\n"
        self.assertEqual(ex.as_string(), comp_str)

    def test_save_load(self):
        self.exam.save("deleteme")
        load_set = Exam()
        load_set.load("deleteme")
        self.assertEqual(self.exam.as_string(), load_set.as_string())

    def test_consolidate(self):
        test = Exam()
        #test is easy if we eliminate the name otherwise we have to do a lot of str manip
        self.exam.set_name("")
        test.consolidate(self.exam, self.exam, self.exam)
        test_string = '\n' + ' '.join(
            [str(i) for i in (self.exam.get_points() * 3)])
        test_string += super(Exam, self.exam).as_string().rstrip() * 3 + '\n'
        self.assertEqual(test.as_string(), test_string)

    def test_txt(self):
        #self.exam.as_text_file("tester")
        pass
Ejemplo n.º 16
0
def create_problem():
    u_question = get_user_input("question")
    u_answer = get_user_input("answer")
    return Problem(u_question, u_answer)