<commit_before>from utils import node, SEP, CHOICE_PREFIX
from survey_element import SurveyElement

def _overlay(over, under):
    if type(under)==dict:
        result = under.copy()
        result.update(over)
        return result
    return over if over else under

class Question(SurveyElement):
    def get_type_definition(self):
        qtd = self.get_question_type_dictionary()
        question_type_str = self._dict[self.TYPE]
        return qtd.get_definition(question_type_str)

    def get(self, key):
        """
        Overlay this questions binding attributes on type of the
        attributes from this question type.
        """
        question_type_dict = self.get_type_definition()
        under = question_type_dict.get(key, None)
        over = SurveyElement.get(self, key)
        if not under: return over
        return _overlay(over, under)

    def xml_instance(self):
        return node(self.get_name())
        
    def xml_control(self):
        return None


class InputQuestion(Question):
    """
    This control string is the same for: strings, integers, decimals,
    dates, geopoints, barcodes ...
    """
    def xml_control(self):
        return node(u"input", ref=self.get_xpath(), *self.xml_label_and_hint())


class UploadQuestion(Question):
    def _get_media_type(self):
        return self.get_control()[u"mediatype"]
        
    def xml_control(self):
        return node(
            u"upload",
            ref=self.get_xpath(),
            mediatype=self._get_media_type(),
            *self.xml_label_and_hint()
            )


class Option(SurveyElement):
    VALUE = u"value"

    def __init__(self, *args, **kwargs):
        # if there's no value key then we'll use the name
        # the value and name will be used interchangeably
        value = kwargs.get(self.VALUE,
                           kwargs.get(self.NAME,
                                      None))
        if value is None:
            raise Exception("Did not specify value for multiple choice option", kwargs)
        d = {
            self.LABEL : kwargs[self.LABEL],
            self.NAME : value,
            }
        SurveyElement.__init__(self, **d)
        self._dict[self.VALUE] = value

    def get_value(self):
        return self._dict[self.VALUE]
    
    def xml_value(self):
        return node(u"value", self.get_value())

    def xml(self):
        item = node(u"item")
        item.append(self.xml_label())
        item.append(self.xml_value())
        return item


class MultipleChoiceQuestion(Question):
    def __init__(self, *args, **kwargs):
        kwargs_copy = kwargs.copy()
        choices = kwargs_copy.pop(u"choices", []) + \
            kwargs_copy.pop(u"children", [])
        Question.__init__(self, *args, **kwargs_copy)
        for choice in choices:
            self.add_choice(**choice)
        
    def validate(self):
        Question.validate(self)
        for choice in self.iter_children():<commit_msg>Simplified options for multiple choice questions to only have a name, rather than an identical name and value. This will break a few tests.<commit_after>from utils import node, SEP, CHOICE_PREFIX
from survey_element import SurveyElement

def _overlay(over, under):
    if type(under)==dict:
        result = under.copy()
        result.update(over)
        return result
    return over if over else under

class Question(SurveyElement):
    def get_type_definition(self):
        qtd = self.get_question_type_dictionary()
        question_type_str = self._dict[self.TYPE]
        return qtd.get_definition(question_type_str)

    def get(self, key):
        """
        Overlay this questions binding attributes on type of the
        attributes from this question type.
        """
        question_type_dict = self.get_type_definition()
        under = question_type_dict.get(key, None)
        over = SurveyElement.get(self, key)
        if not under: return over
        return _overlay(over, under)

    def xml_instance(self):
        return node(self.get_name())
        
    def xml_control(self):
        return None


class InputQuestion(Question):
    """
    This control string is the same for: strings, integers, decimals,
    dates, geopoints, barcodes ...
    """
    def xml_control(self):
        return node(u"input", ref=self.get_xpath(), *self.xml_label_and_hint())


class UploadQuestion(Question):
    def _get_media_type(self):
        return self.get_control()[u"mediatype"]
        
    def xml_control(self):
        return node(
            u"upload",
            ref=self.get_xpath(),
            mediatype=self._get_media_type(),
            *self.xml_label_and_hint()
            )


class Option(SurveyElement):

    def __init__(self, *args, **kwargs):
        d = {
            self.LABEL : kwargs[self.LABEL],
            self.NAME : kwargs[self.NAME],
            }
        SurveyElement.__init__(self, **d)
        self._dict[self.VALUE] = value

    def xml_value(self):
        return node(u"value", self.get_name())

    def xml(self):
        item = node(u"item")
        item.append(self.xml_label())
        item.append(self.xml_value())
        return item


class MultipleChoiceQuestion(Question):
    def __init__(self, *args, **kwargs):
        kwargs_copy = kwargs.copy()
        choices = kwargs_copy.pop(u"choices", []) + \
            kwargs_copy.pop(u"children", [])
        Question.__init__(self, *args, **kwargs_copy)
        for choice in choices:
            self.add_choice(**choice)
        
    def validate(self):
        Question.validate(self)
        for choice in self.iter_children():
{'fraction_non_alphanumeric': 0.10343599786362827, 'fraction_numerical': 0.0, 'mean_word_length': 2.346039309112567, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 20}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '593461', 'n_tokens_mistral': 1627, 'n_tokens_neox': 1608, 'n_words': 396}