def submit_solution(solution, problem_id, problem_pool):
    connection = redis.Redis(connection_pool=problem_pool)
    with RedisWriteLock(connection, problem_id, RETRY_INTERVAL, EXPIRE_TIME):
        problem = Problem.fromString(connection.get(problem_id).decode())
        result = problem.submit_solution(solution)
        connection.set(problem_id, str(problem))
        return result
def display_problems_per_user(user_stats, parsed_problem_file):
    table_to_display = []
    for name, problem_string in parsed_problem_file:
        display_name = Problem.fromString(problem_string).display_name
        answer = Problem.fromString(problem_string).answer
        row = []
        row.append(display_name)
        #row.append(problem_string)

        if name in user_stats['problems_solved']:
            row.append("Yes")
        else:
            row.append("No")

        if name in user_stats['problems_solved'] and (not name in user_stats['solutions_bought']):
            row.append("Yes")
        else:
            row.append("No")

        if name in user_stats['solutions_bought']:
            row.append("Yes")
        else:
            row.append("No")

        if name in (i[0] for i in user_stats['solutions_sold']):
            row.append("Yes")
        else:
            row.append("No")

        if name in user_stats['problems_solved']:
            row.append(answer)
        else:
            row.append("n/a")

        table_to_display.append(row)

    return table_to_display
def categorize_problems(user_stats, parsed_problem_file):
    categories = {"to_solve": [], "to_sell": [], "on_market": []}
    problems = [(name, Problem.fromString(problem_string)) for name, problem_string in parsed_problem_file]
    for name, problem in problems:

        if not name in user_stats['problems_solved']:
            if not problem.dependencies:
                categories["to_solve"].append((name, problem))

            else:
                for dependency in problem.dependencies:
                    if dependency in user_stats['problems_solved']:
                        categories["to_solve"].append((name, problem))
                        break

        if name in user_stats['problems_solved'] and not name in (i[0] for i in user_stats['solutions_sold']):
            categories["to_sell"].append((name, problem))

        if name in user_stats['problems_solved'] and name in (i[0] for i in user_stats['solutions_sold']):
            categories["on_market"].append((name, problem))

    return categories
def get_problem_from_redis(problem_id, problem_pool):
    connection = redis.Redis(connection_pool=problem_pool)
    with RedisWriteLock(connection, problem_id, RETRY_INTERVAL, EXPIRE_TIME):
        result = Problem.fromString(connection.get(problem_id).decode())
        return result