Exemplo n.º 1
0
def format_size(n, _=lambda s: s):
    """Format the given number of bytes.

    Return a size, given as a number of bytes, properly formatted
    using the most appropriate size unit. Always use three
    significant digits.

    """
    if n == 0:
        return '0 B'

    # Use the last unit that's smaller than n
    try:
        unit_index = next(i for i, x in enumerate(DIMS) if n < x) - 1
    except StopIteration:
        unit_index = -1
    n = n / DIMS[unit_index]

    if n < 10:
        d = 2
    elif n < 100:
        d = 1
    else:
        d = 0
    return locale_format(_, "{0:g} {1}", round(n, d), UNITS[unit_index])
Exemplo n.º 2
0
    def format_score(score,
                     max_score,
                     unused_score_details,
                     score_precision,
                     _=lambda s: s):
        """Produce the string of the score that is shown in CWS.

        In the submission table in the task page of CWS the global
        score of the submission is shown (the sum of all subtask and
        testcases). This method is in charge of producing the actual
        text that is shown there. It can be overridden to provide a
        custom message (e.g. "Accepted"/"Rejected").

        score (float): the global score of the submission.
        max_score (float): the maximum score that can be achieved.
        unused_score_details (string): the opaque data structure that
            the ScoreType produced for the submission when scoring it.
        score_precision (int): the maximum number of digits of the
            fractional digits to show.
        _ (function): translation function.

        return (string): the message to show.

        """
        return locale_format(_, "{0:g} / {1:g}", round(score, score_precision),
                             round(max_score, score_precision))
Exemplo n.º 3
0
def format_size(n, _=lambda s: s):
    """Format the given number of bytes.

    Return a size, given as a number of bytes, properly formatted
    using the most appropriate size unit. Always use three
    significant digits.

    """
    if n == 0:
        return '0 B'

    # Use the last unit that's smaller than n
    try:
        unit_index = next(i for i, x in enumerate(DIMS) if n < x) - 1
    except StopIteration:
        unit_index = -1
    n = float(n) / DIMS[unit_index]

    if n < 10:
        d = 2
    elif n < 100:
        d = 1
    else:
        d = 0
    return locale_format(_, "{0:g} {1}", round(n, d), UNITS[unit_index])
Exemplo n.º 4
0
    def get(self, task_name, user_test_num):
        participation = self.current_user

        if not self.r_params["testing_enabled"]:
            raise tornado.web.HTTPError(404)

        try:
            task = self.contest.get_task(task_name)
        except KeyError:
            raise tornado.web.HTTPError(404)

        user_test = self.sql_session.query(UserTest)\
            .filter(UserTest.participation == participation)\
            .filter(UserTest.task == task)\
            .order_by(UserTest.timestamp)\
            .offset(int(user_test_num) - 1)\
            .first()
        if user_test is None:
            raise tornado.web.HTTPError(404)

        ur = user_test.get_result(task.active_dataset)

        # TODO: use some kind of constants to refer to the status.
        data = dict()
        if ur is None or not ur.compiled():
            data["status"] = 1
            data["status_text"] = self._("Compiling...")
        elif ur.compilation_failed():
            data["status"] = 2
            data["status_text"] = "%s <a class=\"details\">%s</a>" % (
                self._("Compilation failed"), self._("details"))
        elif not ur.evaluated():
            data["status"] = 3
            data["status_text"] = self._("Executing...")
        else:
            data["status"] = 4
            data["status_text"] = "%s <a class=\"details\">%s</a>" % (
                self._("Executed"), self._("details"))
            if ur.execution_time is not None:
                data["time"] = locale_format(self._,
                                             self._("{seconds:0.3f} s"),
                                             seconds=ur.execution_time)
            else:
                data["time"] = None
            if ur.execution_memory is not None:
                data["memory"] = format_size(ur.execution_memory, self._)
            else:
                data["memory"] = None
            data["output"] = ur.output is not None

        self.write(data)
Exemplo n.º 5
0
    def get(self, task_name, user_test_num):
        participation = self.current_user

        if not self.r_params["testing_enabled"]:
            raise tornado.web.HTTPError(404)

        try:
            task = self.contest.get_task(task_name)
        except KeyError:
            raise tornado.web.HTTPError(404)

        user_test = self.sql_session.query(UserTest)\
            .filter(UserTest.participation == participation)\
            .filter(UserTest.task == task)\
            .order_by(UserTest.timestamp)\
            .offset(int(user_test_num) - 1)\
            .first()
        if user_test is None:
            raise tornado.web.HTTPError(404)

        ur = user_test.get_result(task.active_dataset)

        # TODO: use some kind of constants to refer to the status.
        data = dict()
        if ur is None or not ur.compiled():
            data["status"] = 1
            data["status_text"] = self._("Compiling...")
        elif ur.compilation_failed():
            data["status"] = 2
            data["status_text"] = "%s <a class=\"details\">%s</a>" % (
                self._("Compilation failed"), self._("details"))
        elif not ur.evaluated():
            data["status"] = 3
            data["status_text"] = self._("Executing...")
        else:
            data["status"] = 4
            data["status_text"] = "%s <a class=\"details\">%s</a>" % (
                self._("Executed"), self._("details"))
            if ur.execution_time is not None:
                data["time"] = locale_format(self._,
                    self._("{seconds:0.3f} s"), seconds=ur.execution_time)
            else:
                data["time"] = None
            if ur.execution_memory is not None:
                data["memory"] = format_size(ur.execution_memory, self._)
            else:
                data["memory"] = None
            data["output"] = ur.output is not None

        self.write(data)
Exemplo n.º 6
0
    def format_score(score, max_score, unused_score_details,
                     score_precision, _=lambda s: s):
        """Produce the string of the score that is shown in CWS.

        In the submission table in the task page of CWS the global
        score of the submission is shown (the sum of all subtask and
        testcases). This method is in charge of producing the actual
        text that is shown there. It can be overridden to provide a
        custom message (e.g. "Accepted"/"Rejected").

        score (float): the global score of the submission.
        max_score (float): the maximum score that can be achieved.
        unused_score_details (string): the opaque data structure that
            the ScoreType produced for the submission when scoring it.
        score_precision (int): the maximum number of digits of the
            fractional digits to show.
        _ (function): translation function.

        return (string): the message to show.

        """
        return locale_format(_, "{0:g} / {1:g}",
            round(score, score_precision), round(max_score, score_precision))