def print_rule_resolution(rule, points): if verbose_print: indent = ' ' rule_name = as_purple(rule.name or 'Unnamed Rule') print('{}Rule: "{}" gave {} pts'.format(indent, rule_name, as_green(points))) util.cond([(is_uppgift, print_uppgift_details)])(rule)
def scrape_site(url, cookies, collector, destructor): headers = get_headers() response = requests.get(url, cookies=cookies, headers=headers) soup = BeautifulSoup(response.text, 'lxml') table = soup.find_all('table', class_='table-submissions')[0] rows = table.find_all('tbody')[0].find_all('tr') def has_no_entries(): return not rows def process_row(row): data_items = row.find_all('td') # This is how the html is divided in the table cells. # Submission ID | Time | Name + link to problem | Status | runtime | language # 0 | 1 | 2 | 3 | 4 | 5 problem_id = data_items[2].a['href'].split('/')[-1] problem_name = data_items[2].a.text status = data_items[3]['class'] time = data_items[1].text return { 'problem_id': problem_id, 'problem_name': problem_name, 'status': status, 'time': time } def process_all_rows(): data = util.map_now(process_row, rows) util.map_now(collector, data) return util.cond([(has_no_entries, destructor), (util.truthy, process_all_rows)])()
def resolver(context, tree): items = tree['late'] before = context.value_expression(items['before']) after = context.value_expression(items['after']) deadline = items['deadline'] problem_id = items['problem'] return util.cond([(ac_before_deadline, util.constant(before)), (ac_after_deadline, util.constant(after)), (util.truthy, util.constant(0))])(problem_id, deadline)
def add_student_session_problems(starttime, student, problems): is_student = lambda s: s.username == student student_data = util.find(is_student, result.students) student_not_exist = util.constant(not bool(student_data)) is_solved = lambda problem: 'solve_time' in problem add_problem = lambda problem: add_session_problem( student_data, problem, starttime) handle_single_problem = util.cond([(student_not_exist, util.noop), (is_solved, add_problem)]) util.map_now(handle_single_problem, problems)
def make_credentials(username, password=None, token=None, loginurl=''): def has_password(): return bool(password) def has_token(): return bool(token) def return_with_password(): return Credentials(username, 'password', password, loginurl) def return_with_token(): return Credentials(username, 'token', token, loginurl) def crash(): crash_on(True, bad_credentials_messages) return util.cond([(has_password, return_with_password), (has_token, return_with_token), (util.truthy, crash)])()
def get_status_row(problem_id, points, deadline): no_subs = len(kattis.attempts_for(problem_id)) == 0 if no_subs: return '[ - ]' AC = len(kattis.solutions_for(problem_id)) > 0 WA_3 = len(kattis.WA_for(problem_id)) >= 3 before_deadline = kattis.solved_before(problem_id, deadline) C = lambda b: util.cond([(lambda: b, lambda: check), (lambda: not b, lambda: cross)])() if not AC: points = 0 if not before_deadline: points /= 2.0 pts = as_green(points) if points > 0 else as_red(int(points)) a = '{:16}'.format(pts) b = '{}'.format(C(AC)) c = '{}'.format(C(WA_3)) d = '{}'.format(C(before_deadline)) message = '[ Pts? {} AC? {} +3WA? {} Before Deadline? {} ]' return message.format(a, b, c, d)
def make_uppgift_handler(kattis): Problem = collections.namedtuple('Problem', 'id points') def is_string(e): return isinstance(e, str) def is_dict(e): return isinstance(e, dict) def problem_from_string(problem_id): return Problem(problem_id, 1) def problem_from_dict(obj): return Problem(obj['id'], obj['points']) make_problem = util.cond([(is_string, problem_from_string), (is_dict, problem_from_dict)]) def count_for_single_problem(problem, deadline): solutions = kattis.solutions_for(problem.id) problem_solved = len(solutions) > 0 before_deadline = problem_solved and any( time_compare(s.time, deadline) for s in solutions) return (0.5 * problem_solved + 0.5 * before_deadline) * problem.points def checker(context, tree): return isinstance(tree, dict) and 'uppgift' in tree def resolver(context, tree): # If a problem does not have a deadline, then assume it is a lab # and move the deadline to something like 100 years from now. problems = util.map_now(make_problem, tree['uppgift']['problems']) deadline = tree['uppgift'].get('deadline', '01-01-2117 08:00') points = lambda problem: count_for_single_problem(problem, deadline) return sum(map(points, problems)) return checker, resolver
def process_item(index): connection_count[index] -= 1 util.cond([(should_add_to_queue, add_to_queue)])(index)
def find_expression_rec(tree): util.cond([(is_correct_expression, util.combine(add_expression, find_in_children)), (is_expression, find_in_children), (is_list, search_in_list)])(tree)
def handle_expression(self, tree): handler = util.cond(self.function_handlers) return handler(self, tree)