Ejemplo n.º 1
0
class ParameterTypeChoice(ParameterType):
    """Type for a parameter giving a choice among a finite number of items."""

    TEMPLATE = GLOBAL_ENVIRONMENT.from_string("""
<select name="{{ prefix ~ parameter.short_name }}">
{% for choice_value, choice_description in parameter.values.items() %}
  <option value="{{ choice_value }}"
          {% if choice_value == previous_value %}selected{% endif %}>
    {{ choice_description }}
  </option>
{% endfor %}
</select>
""")

    def __init__(self, name, short_name, description, values):
        """Initialization.

        values (dict): dictionary mapping each choice to a short description.

        """
        super().__init__(name, short_name, description)
        self.values = values

    def validate(self, value):
        # Convert to string to avoid TypeErrors on unhashable types.
        if str(value) not in self.values:
            raise ValueError("Invalid choice %s for parameter %s" %
                             (value, self.name))

    def parse_string(self, value):
        if value not in self.values:
            raise ValueError("Value %s doesn't match any allowed choice." %
                             value)
        return value
Ejemplo n.º 2
0
    def __init__(self, parameters, public_testcases):
        """Initializer.

        parameters (object): format is specified in the subclasses.
        public_testcases (dict): associate to each testcase's codename
                                 a boolean indicating if the testcase
                                 is public.

        """
        self.parameters = parameters
        self.public_testcases = public_testcases

        # Preload the maximum possible scores.
        try:
            self.max_score, self.max_public_score, self.ranking_headers = \
                self.max_scores()

            self.public_score_header, self.private_score_header = \
                self.score_column_headers()
        except Exception as e:
            raise ValueError(
                "Unable to instantiate score type (probably due to invalid "
                "values for the score type parameters): %s." % e)

        self.template = GLOBAL_ENVIRONMENT.from_string(self.TEMPLATE)
Ejemplo n.º 3
0
    def __init__(self, parameters, public_testcases):
        """Initializer.

        parameters (object): format is specified in the subclasses.
        public_testcases (dict): associate to each testcase's codename
                                 a boolean indicating if the testcase
                                 is public.

        """
        self.parameters = parameters
        self.public_testcases = public_testcases

        # Preload the maximum possible scores.
        try:
            self.max_score, self.max_public_score, self.ranking_headers = \
                self.max_scores()

            self.public_score_header, self.private_score_header = \
                self.score_column_headers()
        except Exception as e:
            raise ValueError(
                "Unable to instantiate score type (probably due to invalid "
                "values for the score type parameters): %s." % e)

        self.template = GLOBAL_ENVIRONMENT.from_string(self.TEMPLATE)
Ejemplo n.º 4
0
class ParameterTypeInt(ParameterType):
    """Type for an integer parameter."""

    TEMPLATE = GLOBAL_ENVIRONMENT.from_string("""
<input type="text"
       name="{{ prefix ~ parameter.short_name }}"
       value="{{ previous_value }}" />
""")

    def validate(self, value):
        if not isinstance(value, int):
            raise ValueError("Invalid value for int parameter %s" % self.name)

    def parse_string(self, value):
        return int(value)
Ejemplo n.º 5
0
class ParameterTypeCollection(ParameterType):
    """Type of a parameter containing a tuple of sub-parameters."""

    TEMPLATE = GLOBAL_ENVIRONMENT.from_string("""
<table>
{% for subp in parameter.subparameters %}
  {% set subp_prefix = "%s%s_%d_"|format(prefix, parameter.short_name,
                                         loop.index0) %}
  {% set subp_previous_value = (previous_value[loop.index0]
                                if previous_value is not none else none) %}
  <tr>
    <td>{{ subp.name }}</td>
    <td>{{ subp.render(subp_prefix, subp_previous_value) }}</td>
  </tr>
{% endfor %}
</table>
""")

    def __init__(self, name, short_name, description, subparameters):
        """Initialization.

        subparameters ([ParameterType]): list of types of each sub-parameter.

        """
        super(ParameterTypeCollection, self).__init__(name, short_name,
                                                      description)
        self.subparameters = subparameters

    def validate(self, value):
        if not isinstance(value, list):
            raise ValueError("Parameter %s should be a list" % self.name)
        if len(value) != len(self.subparameters):
            raise ValueError("Invalid value for parameter %s" % self.name)
        for subvalue, subparameter in zip(value, self.subparameters):
            subparameter.validate(subvalue)

    def parse_string(self, value):
        raise NotImplementedError(
            "parse_string is not implemented for composite parameter types.")

    def parse_handler(self, handler, prefix):
        parsed_values = []
        for i in range(len(self.subparameters)):
            new_prefix = "%s%s_%d_" % (prefix, self.short_name, i)
            parsed_values.append(self.subparameters[i].parse_handler(
                handler, new_prefix))
        return parsed_values
Ejemplo n.º 6
0
    def __init__(self, parameters, public_testcases):
        """Initializer.

        parameters (object): format is specified in the subclasses.
        public_testcases (dict): associate to each testcase's codename
                                 a boolean indicating if the testcase
                                 is public.

        """
        self.parameters = parameters
        self.public_testcases = public_testcases

        # Preload the maximum possible scores.
        self.max_score, self.max_public_score, self.ranking_headers = \
            self.max_scores()

        self.template = GLOBAL_ENVIRONMENT.from_string(self.TEMPLATE)
Ejemplo n.º 7
0
    def __init__(self, parameters, public_testcases):
        """Initializer.

        parameters (object): format is specified in the subclasses.
        public_testcases (dict): associate to each testcase's codename
                                 a boolean indicating if the testcase
                                 is public.

        """
        self.parameters = parameters
        self.public_testcases = public_testcases

        # Preload the maximum possible scores.
        self.max_score, self.max_public_score, self.ranking_headers = \
            self.max_scores()

        self.template = GLOBAL_ENVIRONMENT.from_string(self.TEMPLATE)