예제 #1
0
def evaluate_test(ans, att):
	ans = ans.strip("\[")
	ans = ans.strip("\]")
	ans = ans.replace("{","")
	ans = ans.replace("}","")
	att = att.strip("'")
	try:
		update_sql = """UPDATE eval_info 
						SET attempt = %s
						SET answer = %s"""
		db_cursor.execute(update_sql,(att,ans))
		db.commit()
	except:
		db.rollback()
		print "Database has been rolled back because of an Exception !!!"
		#print(traceback.format_exc())  

	p = make_params(ans, att)
	if p == {}:
		#logger.info("param empty from evaluate")
		return False
	att_value = get_numerical_answer(p['att_tree'])
	ans_value = get_numerical_answer(p['ans_tree'])
	final_pairs = find_matches(p)
	#logger.info("output matching: {0}.".format(final_pairs))

	if len(final_pairs) == 1 and final_pairs[0][0] == 'R':
		return True
	elif check_w_tol(ans_value, att_value):
		return True
	else:
		return False
예제 #2
0
def get_hint_wo_uni(ans, att, hint_text_id, i):
	ans = ans.replace("{","")
	ans = ans.replace("}","")
	param = make_params(ans, att)
	if param == {}:
		return ""
	hint_text, hint_answer = get_conditional_hints(hint_text_id, i, param)
	if hint_text and hint_answer:
		hint = hint_format.format_c_hint(hint_text, hint_text_id, hint_answer, i)
		return hint

	return ""
예제 #3
0
def get_hint_wo_uni(ans, att, hint_text_id, i):
    ans = ans.replace("{", "")
    ans = ans.replace("}", "")
    param = make_params(ans, att)
    if param == {}:
        return ""
    hint_text, hint_answer = get_conditional_hints(hint_text_id, i, param)
    if hint_text and hint_answer:
        hint = hint_format.format_c_hint(hint_text, hint_text_id, hint_answer,
                                         i)
        return hint

    return ""
예제 #4
0
def get_hint_wo_cond(ans, att, hint_text_id, i):
	ans = ans.replace("{","")
  	ans = ans.replace("}","")
	param = make_params(ans, att)
	if param == {}:
		return ""
	hint = get_first_universal_hints(param)
	if hint:
		hint = hint_format.format_u_hint(hint, hint_text_id, i)
		return hint

	hint = get_last_universal_hints(param)
	if hint:
		hint = hint_format.format_u_hint(hint, hint_text_id, i)
		return hint

	return ""
예제 #5
0
def get_hint_wo_cond(ans, att, hint_text_id, i):
    ans = ans.replace("{", "")
    ans = ans.replace("}", "")
    param = make_params(ans, att)
    if param == {}:
        return ""
    hint = get_first_universal_hints(param)
    if hint:
        hint = hint_format.format_u_hint(hint, hint_text_id, i)
        return hint

    hint = get_last_universal_hints(param)
    if hint:
        hint = hint_format.format_u_hint(hint, hint_text_id, i)
        return hint

    return ""
예제 #6
0
def evaluate(ans, att):
	ans = ans.strip("\[")
  	ans = ans.strip("\]")
  	ans = ans.replace("{","")
  	ans = ans.replace("}","")
  	att = att.strip("'")
  	#logger.info("evaluating attempt: {0}, answer: {1}.".format(att, ans))
	p = make_params(ans, att)
	if p == {}:
		#logger.info("param empty from evaluate")
		return False
	att_value = get_numerical_answer(p['att_tree'])
	ans_value = get_numerical_answer(p['ans_tree'])
	final_pairs = find_matches(p)
	#logger.info("output matching: {0}.".format(final_pairs))

	if len(final_pairs) == 1 and final_pairs[0][0] == 'R':
		return True
	elif check_w_tol(ans_value, att_value):
		return True
	else:
		return False
예제 #7
0
def evaluate(ans, att, tol=1 + 1e-5):
    ans = ans.strip("\[")
    ans = ans.strip("\]")
    ans = ans.replace("{", "")
    ans = ans.replace("}", "")
    att = att.strip("'")
    #logger.info("evaluating attempt: {0}, answer: {1}.".format(att, ans))
    p = make_params(ans, att)
    if p == {}:
        #logger.info("param empty from evaluate")
        return False
    att_value = get_numerical_answer(p['att_tree'])
    ans_value = get_numerical_answer(p['ans_tree'])
    final_pairs = find_matches(p, tol)
    #logger.info("output matching: {0}.".format(final_pairs))

    if len(final_pairs) == 1 and final_pairs[0][0] == 'R':
        return True
    elif check_w_tol(ans_value, att_value, tol):
        return True
    else:
        return False
예제 #8
0
def single_hint_test(path, new_class_name, testdata):
    first_u_hints, hint_classes, last_u_hints = get_all_classes(path)

    try:
        hint_classes.remove(new_class_name)
    except:
        sys.exit('ERROR: Cannot find the input class name')
    
    i = 1

    # Try new hint
    folder_name = path
    package_name = folder_name.replace('/', '.')
    class_address = package_name + "." + new_class_name + "." + new_class_name
    try:
        cond_ClassName = locate(class_address)
    except:
        traceback.print_exc()
        sys.exit("ERROR: syntax error in hint class {0}.".format(class_address))

    try:
        cond_hint_instance = cond_ClassName()
    except TypeError:
        sys.exit("ERROR: name of the HINT CLASS has to be the same as the name of the FILE {0}.".format(cond_ClassName))

    try:
        pro_list = cond_hint_instance.get_problems()
    except:
        sys.exit("ERROR: no function get_problems in hint class {0}.".format(class_address))

    all_pros = json.loads(open("problems_mapping.json").read())
    for p in pro_list:
        if not '/part' in p:
            sys.exit("ERROR: please include part id in the get_problems.")
        par_index = p.index('/part')
        if not any(p[:par_index] == pros[1] for pros in all_pros.items()):
            sys.exit("ERROR: wrong problem name in get_problems.")



    for test in testdata:
        attempt = test['attempt']
        answer = test['answer']
        if not attempt:
            print "Attempt is empty"
            continue

        params = make_params(answer,attempt)

        if params == {}:
            continue

        match = find_matches(params)
        ans_tree = params['ans_tree'][0]
        if match==ans_tree or check_final_answer(params):
            #print "Correct answer!"
            continue

        hint = ""
        # try first universal hints
        package_name = 'hint_class.first_Universal'
        if len(params['att_tree']) > 1:
            for f_name in first_u_hints:
                f_address = package_name + "." + f_name
                try:
                    uni_f = locate(f_address)
                except:
                    #traceback.print_exc()
                    sys.exit("ERROR: syntax error in universal hint function {0}.".format(f_address))

                try:
                    hint = uni_f.check_attempt(params)
                except:
                    #traceback.print_exc()
                    sys.exit("ERROR: syntax error in universal hint function {0}.".format(f_address))

                if hint:
                    break
            if hint:
                continue

        # try conditional hints
        folder_name = path
        package_name = folder_name.replace('/', '.')
        for class_name in hint_classes:
            class_address = package_name + "." + class_name + "." + class_name
            try:
                ClassName = locate(class_address)
            except:
                traceback.print_exc()
                sys.exit("ERROR: syntax error in HINT CLASS {0}.".format(ClassName))

            try:
                hint_instance = ClassName()
            except TypeError:
                sys.exit("ERROR: name of the HINT CLASS has to be the same as the name of the FILE {0}.".format(class_address))

            try:
                hint, hint_ans = hint_instance.check_attempt(params)
            except:
                traceback.print_exc()
                sys.exit("ERROR: syntax error in HINT CLASS {0}.".format(class_address))

            if hint and not hint_ans:
                sys.exit("ERROR: you can't leave the second returned string empty. Please ask a question in your hint.")
                
            if hint:
                break

        if hint:
            continue


        print "\nUserID:",i
        i+=1
        print "attempt:",attempt
        print "answer:",answer

        # Try new hint
        class_address = package_name + "." + new_class_name + "." + new_class_name
        try:
            hint, hint_ans = cond_hint_instance.check_attempt(params)
        except:
            traceback.print_exc()
            sys.exit("ERROR: syntax error in HINT CLASS {0}.".format(class_address))
        if hint and not hint_ans:
                sys.exit("ERROR: you can't leave the second returned string empty. Please ask a question in your hint.")

        if hint:
            print "hint:", hint
            print "solution:", hint_ans
            continue


        # Try last universal hint
        package_name = 'hint_class.last_Universal'
        last_universal_hint = ""
        for f_name in last_u_hints:
            f_address = package_name + "." + f_name
            try:
                uni_f = locate(f_address)
            except:
                traceback.print_exc()
                sys.exit("ERROR: syntax error in universal hint function {0}.".format(f_address))

            try:
                hint = uni_f.check_attempt(params)
            except:
                traceback.print_exc()
                sys.exit("ERROR: syntax error in universal hint function {0}.".format(f_address))

            if hint:
                print "hint:", hint
                break

        if not hint:
            print "Hint does not hit!"
예제 #9
0
def all_hints_test(path,testdata):
    first_u_hints, hint_classes, last_u_hints = get_all_classes(path)

    i = 1

    for test in testdata:
        print "\nUserID:",i
        i+=1
        attempt = test['attempt']
        answer = test['answer']
        print "attempt:",attempt
        print "answer:",answer

        if not attempt:
            print "Attempt is empty"
            continue

        params = make_params(answer,attempt)


        if params == {}:
            continue
            
        match = find_matches(params)
        ans_tree = params['ans_tree'][0]
        if match==ans_tree or check_final_answer(params):
            print "Correct answer!"
            continue

        # try first universal hints
        package_name = 'hint_class.first_Universal'
        universal_hint = ""
        if len(params['att_tree']) > 1:
            for f_name in first_u_hints:
                f_address = package_name + "." + f_name
                try:
                    uni_f = locate(f_address)
                except:
                    traceback.print_exc()
                    sys.exit("ERROR: syntax error in universal hint function!!")

                try:
                    universal_hint = uni_f.check_attempt(params)
                except:
                    traceback.print_exc()
                    sys.exit("ERROR: syntax error in universal hint function!!")

                if universal_hint:
                    print universal_hint
                    break
            if universal_hint:
                continue


        # try conditional hints
        folder_name = path
        package_name = folder_name.replace('/', '.')#"".join(package_name.split('/')[-1])

        hint = ""
        for class_name in hint_classes:
            class_address = package_name + "." + class_name + "." + class_name
            try:
                ClassName = locate(class_address)
            except:
                traceback.print_exc()
                sys.exit("ERROR: syntax error in HINT CLASS!!")

            try:
                hint_instance = ClassName()
            except TypeError:
                sys.exit("ERROR: name of the HINT CLASS has to be the same as the name of the FILE !!")

            try:
                hint, hint_ans = hint_instance.check_attempt(params)
            except:
                traceback.print_exc()
                sys.exit("ERROR: syntax error in HINT CLASS!!")

            if hint and not hint_ans:
                sys.exit("ERROR: you can't leave the second returned string empty. Please ask a question in your hint.")

            if hint:
                print hint, hint_ans
                break
        if hint:
            continue


        # Try last universal hint
        package_name = 'hint_class.last_Universal'
        last_universal_hint = ""
        for f_name in last_u_hints:
            f_address = package_name + "." + f_name
            try:
                uni_f = locate(f_address)
            except:
                traceback.print_exc()
                sys.exit("ERROR: syntax error in universal hint function!!")

            try:
                last_universal_hint = uni_f.check_attempt(params)
            except:
                traceback.print_exc()
                sys.exit("ERROR: syntax error in universal hint function!!")

            if last_universal_hint:
                print "hint:", last_universal_hint
                break

        if not hint:
            print "Hint does not hit!"