Ejemplo n.º 1
0
def dump_submissions_to(solution_dir, start=0):
    for problem in (fetch_problem_list() > pipe
                    | where(X.paid_only == False)
                    | where(X.id >= start)
                    | sort_by(X.id)):
        submissions = fetch_problem_submissions(problem.title_slug)

        if len(submissions) == 0:
            continue

        problem_dir = solution_dir / problem.dir_name()
        problem_dir.mkdir(exist_ok=True)
        print(
            f'{len(submissions)} submissions for problem {problem.id}. {problem.title}'
        )

        for submission in submissions:
            code = fetch_submission_code(submission.url)
            problem_submission = ProblemSubmission(problem=problem,
                                                   submission=submission)
            problem_file = problem_dir / problem_submission.file_name()
            with problem_file.open('w') as f:
                print(code, file=f)

        time.sleep(0.3)
Ejemplo n.º 2
0
def update_car_status(unit_id, entries):
    """
    Updates Car model from `entries` received through the API.
    """
    car = CarUnit.objects.get(unit_id=unit_id).car
    car and (entries > pipe
             | foreach(with_parsed_timestamp)
             | sort_by(X['timestamp'])
             | first_of
             | (update_car, car))
Ejemplo n.º 3
0
    def test_descending(self):

        result = zip('what', [1, 2, 3, 4]) > sort_by(X[1]).descending

        assert result == [
            ('t', 4),
            ('a', 3),
            ('h', 2),
            ('w', 1),
        ]
Ejemplo n.º 4
0
    def test_x(self):

        result = sort_by(-X[1])(zip('what', [1, 2, 3, 4]))

        assert result == [
            ('t', 4),
            ('a', 3),
            ('h', 2),
            ('w', 1),
        ]
Ejemplo n.º 5
0
    def test_descending(self):

        result = zip('what', [1, 2, 3, 4]) > sort_by(X[1]).descending

        assert result == [
            ('t', 4),
            ('a', 3),
            ('h', 2),
            ('w', 1),
        ]
Ejemplo n.º 6
0
    def test_x(self):

        result = sort_by(-X[1])(zip('what', [1, 2, 3, 4]))

        assert result == [
            ('t', 4),
            ('a', 3),
            ('h', 2),
            ('w', 1),
        ]
Ejemplo n.º 7
0
def update_car_status(unit_id, entries):
    """
    Updates Car model from `entries` received through the API.
    """
    car = CarUnit.objects.get(unit_id=unit_id).car
    car and (entries > pipe
        | foreach(with_parsed_timestamp)
        | sort_by(X['timestamp'])
        | first_of
        | (update_car, car))
Ejemplo n.º 8
0
    def test_not_empty(self):
        car1, car2 = cars_testing_data.create()['cars'][:2]
        unit1 = unit(123, car1)
        unit2 = unit(456, car2)

        geotrack_data = {
            123: {
                'location': 'LOC1',
                'timestamp': 'TS1'
            },
            456: {
                'location': 'LOC2',
                'timestamp': 'TS2'
            },
        }

        result = _current_position_data(geotrack_data)
        expected = (
            {
                'location': 'LOC1',
                'timestamp': 'TS1',
                'car': car1
            },
            {
                'location': 'LOC2',
                'timestamp': 'TS2',
                'car': car2
            },
        )

        self.assertEqual(
            # don't know (or care about) the output order,
            # so we have to somehow sort them
            result > sort_by(X['timestamp']) | tuple,
            expected > sort_by(X['timestamp']) | tuple,
        )
        unit1.delete()
        unit2.delete()
Ejemplo n.º 9
0
    def test_not_empty(self):
        car1, car2 = cars_testing_data.create()['cars'][:2]
        unit1 = unit(123, car1)
        unit2 = unit(456, car2)

        geotrack_data = {
            123: {'location': 'LOC1', 'timestamp': 'TS1'},
            456: {'location': 'LOC2', 'timestamp': 'TS2'},
        }

        result = _current_position_data(geotrack_data)
        expected = (
            {'location': 'LOC1', 'timestamp': 'TS1', 'car': car1},
            {'location': 'LOC2', 'timestamp': 'TS2', 'car': car2},
        )

        self.assertEqual(
            # don't know (or care about) the output order,
            # so we have to somehow sort them
            result > sort_by(X['timestamp']) | tuple,
            expected > sort_by(X['timestamp']) | tuple,
        )
        unit1.delete()
        unit2.delete()
Ejemplo n.º 10
0
def render_readme():
    problems = fetch_problem_list()

    problems_for_render = (problems > pipe
                           | sort_by(X.id)
                           | foreach(ProblemForRender.from_problem)
                           | list)

    env = Environment(loader=PackageLoader('coder', 'templates'),
                      trim_blocks=True)

    template = env.get_template('README.md')
    rendered = template.render(problems=problems_for_render)

    with open('README.md', 'w') as f:
        print(rendered, file=f)
Ejemplo n.º 11
0
 def link_to_solutions(problem: Problem):
     solution_dir = Path('solutions')
     problem_dir = solution_dir / problem.dir_name()
     if not problem_dir.exists():
         return []
     links = (
         problem_dir.iterdir() > pipe
         | foreach(X.name)
         | foreach(SubmissionFileName.from_str)
         # Select one newest submission from each language
         | group_by(X.language)
         | foreach(X[1])
         | foreach(lambda submissions: max(submissions,
                                           key=lambda s: s.timestamp))
         # Sort
         | sort_by(X.language.order())
         # Format to links
         | foreach(
             lambda file:
             f'[{file.language.display()}]({problem_dir / file.to_str()})'))
     links = list(links)
     return links
Ejemplo n.º 12
0
 def test_ds_builder(self):
     f = sort_by([X.attr, X * 2])
     assert repr(f) == 'sort_by([X.attr, X * 2])'
Ejemplo n.º 13
0
 def test_ds_builder(self):
     f = sort_by([X.attr, X * 2])
     assert f.__name__ == 'sort_by([X.attr, X * 2])'
Ejemplo n.º 14
0
 def test_ds_builder(self):
     f = sort_by([X.attr, X * 2])
     assert f.__name__ == 'sort_by([X.attr, X * 2])'