示例#1
0
def handle_valueless_unit_tags(arg: object, sentence: str):
    r"""
    Each {\*_unit} tag triggers an arg.\*_unit attribute to be randomly set.

    For instance, if {length_unit} is found, then arg.length_unit will get a
    random length unit.
    Moreover, if {area_unit} or {volume_unit} are found, arg.length_unit is
    set accordingly too. If arg.length_unit does already exist,
    then arg.area_unit will be set accordingly (and not randomly any more).

    {\*_unitN}, <\*_unit> and <\*_unitN> tags will be handled the same way
    by this function.

    If the tag embbeds a value, like in {capacity_unit=dL}, then it's ignored
    by this function. If arg already has an attribute matching the tag, then
    it's also ignored by this function.

    :param arg: the object that attributes must be checked and possibly set
    :type arg: object
    :param sentence: the sentence where to look for "unit" tags.
    :type sentence: str
    :rtype: None
    """
    valueless_unitblocks = [w[1:-1] for w in sentence.split()
                            if ('=' not in w
                                and (is_wrapped(w)
                                     or is_wrapped(w, braces='<>'))
                                and (w[:-1].endswith('_unit')
                                     or w[:-2].endswith('_unit')))] \
        + [w[1:-2] for w in sentence.split()
           if ('=' not in w
               and (is_wrapped_P(w) or is_wrapped_P(w, braces='<>'))
               and (w[:-2].endswith('_unit') or w[:-3].endswith('_unit')))]
    d = copy.deepcopy(UNIT_KINDS)
    d.update({'area': COMMON_LENGTH_UNITS, 'volume': COMMON_LENGTH_UNITS})
    area_or_volume_tags = []
    for vu in valueless_unitblocks:
        unit_kind, unit_id = vu.split(sep="_")
        if not hasattr(arg, vu):
            if unit_kind == 'currency':
                val = CURRENCIES_DICT[settings.currency]
                setattr(arg, vu, val)
            elif unit_kind in ['length', 'mass', 'capacity']:
                val = random.choice(d[unit_kind])
                setattr(arg, vu, val)
            elif unit_kind in ['area', 'volume']:
                area_or_volume_tags.append(vu)
            else:
                raise error.OutOfRangeArgument(unit_kind, str(d.keys()))
    for vu in area_or_volume_tags:
        unit_kind, unit_id = vu.split(sep="_")
        val = random.choice(d['length'])
        if hasattr(arg, 'length_' + unit_id):
            val = getattr(arg, 'length_' + unit_id)
        else:
            setattr(arg, 'length_' + unit_id, val)
        setattr(arg, vu, val)
示例#2
0
    def to_str(self, ex_or_answers):
        if ex_or_answers == 'exc':
            return self.text_to_str()

        elif ex_or_answers == 'ans':
            return self.answer_to_str()

        elif ex_or_answers == 'hint':
            return self.hint_to_str()

        else:
            raise error.OutOfRangeArgument(ex_or_answers, 'exc|ans')
示例#3
0
文件: LaTeX.py 项目: o2edu/mathmaker
 def set_redirect_output_to_str(self, arg):
     if type(arg) == bool:
         self.redirect_output_to_str = arg
     else:
         raise error.OutOfRangeArgument(arg, " boolean ")
示例#4
0
    def __init__(self,
                 x_kind,
                 AVAILABLE_X_KIND_VALUES,
                 X_LAYOUTS,
                 X_LAYOUT_UNIT,
                 number_of_questions=6,
                 **options):
        try:
            self.derived
        except AttributeError:
            raise error.NotInstanciableObject(self)

        self.questions_list = list()

        # OPTIONS -------------------------------------------------------------
        # It is necessary to define an options field to pass the
        # possibly modified value to the child class
        self.options = options

        try:
            AVAILABLE_X_KIND_VALUES[x_kind]
        except KeyError:
            raise error.OutOfRangeArgument(x_kind,
                                           str(AVAILABLE_X_KIND_VALUES))

        x_subkind = 'default'
        if 'x_subkind' in options:
            x_subkind = options['x_subkind']
            # let's remove this option from the options
            # since we re-use it recursively
            temp_options = dict()
            for key in options:
                if key != 'x_subkind':
                    temp_options[key] = options[key]
            self.options = temp_options

        if x_subkind not in AVAILABLE_X_KIND_VALUES[x_kind]:
            raise error.OutOfRangeArgument(
                x_subkind, str(AVAILABLE_X_KIND_VALUES[x_kind]))

        self.x_kind = x_kind
        self.x_subkind = x_subkind

        # Start number
        self.start_number = 0
        if 'start_number' in options:
            if not is_.an_integer(options['start_number']):
                raise error.UncompatibleType(options['start_number'],
                                             "integer")
            if not (options['start_number'] >= 1):
                raise error.OutOfRangeArgument(options['start_number'],
                                               "should be >= 1")

            self.start_number = options['start_number']

        # Number of questions
        if (not isinstance(number_of_questions, int)
                and number_of_questions >= 1):
            # __
            raise ValueError("The number_of_questions keyword argument should "
                             "be an int and greater than 6.")
        self.q_nb = number_of_questions

        self.layout = options.get('layout', 'default')
        self.x_layout_unit = X_LAYOUT_UNIT

        if (self.x_kind, self.x_subkind) in X_LAYOUTS:
            self.x_layout = X_LAYOUTS[(self.x_kind, self.x_subkind)]

        else:
            self.x_layout = X_LAYOUTS[self.layout]

        # The slideshow option (for MentalCalculation sheets)
        self.slideshow = options.get('slideshow', False)
示例#5
0
    def __str__(self):
        result = ""
        if self.layout_type == 'std' or self.layout_type == 'equations':
            result += shared.machine.write_document_header()
            result += shared.machine.write_document_begins()
            result += self.sheet_header_to_str()
            result += self.sheet_title_to_str()
            result += self.sheet_text_to_str()
            result += self.texts_to_str('exc', 0)
            result += shared.machine.write_jump_to_next_page()
            result += self.answers_title_to_str()
            result += self.texts_to_str('ans', 0)
            result += shared.machine.write_document_ends()

        elif self.layout_type == 'short_test':
            result += shared.machine.write_document_header()
            result += shared.machine.write_document_begins()

            n = 1
            if self.write_texts_twice:
                n = 2

            for i in range(n):
                result += self.sheet_header_to_str()
                result += self.sheet_title_to_str()
                result += self.sheet_text_to_str()
                result += self.texts_to_str('exc', 0)
                result += shared.machine.write_new_line_twice()

                result += self.sheet_header_to_str()
                result += self.sheet_title_to_str()
                result += self.sheet_text_to_str()
                result += self.texts_to_str('exc',
                                            len(self.exercises_list) // 2)
                result += shared.machine.write_new_line_twice()

                if n == 2 and i == 0:
                    result += shared.machine.insert_dashed_hline()
                    result += shared.machine.write_new_line()
                    result += shared.machine.insert_vspace()
                    result += shared.machine.write_new_line_twice()

            result += shared.machine.write_jump_to_next_page()

            result += self.answers_title_to_str()
            result += self.texts_to_str('ans', 0)
            result += shared.machine.write_jump_to_next_page()
            result += self.answers_title_to_str()
            result += self.texts_to_str('ans', len(self.exercises_list) // 2)
            result += shared.machine.write_document_ends()

        elif self.layout_type == 'mini_test':
            result += shared.machine.write_document_header()
            result += shared.machine.write_document_begins()

            for i in range(3):
                result += self.sheet_header_to_str()
                result += self.sheet_title_to_str()
                result += self.sheet_text_to_str()
                result += self.texts_to_str('exc', 0)
                result += shared.machine.write_new_line_twice()
                result += self.sheet_header_to_str()
                result += self.sheet_title_to_str()
                result += self.sheet_text_to_str()
                result += self.texts_to_str('exc',
                                            len(self.exercises_list) // 2)
                result += shared.machine.write_new_line_twice()

            # result += self.sheet_header_to_str()
            # result += self.sheet_title_to_str()
            # result += self.sheet_text_to_str()
            # result += self.texts_to_str('exc', len(self.exercises_list)/2)
            # result += shared.machine.write_new_line_twice()
            # result += self.sheet_header_to_str()
            # result += self.sheet_title_to_str()
            # result += self.sheet_text_to_str()
            # result += self.texts_to_str('exc', 3*len(self.exercises_list)/4)

            result += shared.machine.write_jump_to_next_page()

            result += self.answers_title_to_str()
            result += self.texts_to_str('ans', 0)
            result += shared.machine.write_jump_to_next_page()
            result += self.answers_title_to_str()
            result += self.texts_to_str('ans', len(self.exercises_list) // 2)

            # result += shared.machine.write_jump_to_next_page()
            # result += self.answers_title_to_str()
            # result += self.texts_to_str('ans', len(self.exercises_list)/2)
            # result += shared.machine.write_jump_to_next_page()
            # result += self.answers_title_to_str()
            # result += self.texts_to_str('ans', 3*len(self.exercises_list)/4)

            result += shared.machine.write_document_ends()

        elif self.layout_type == 'mini_training':
            result += shared.machine.write_document_header()
            result += shared.machine.write_document_begins()

            for i in range(6):
                result += self.texts_to_str('exc', 0)
                result += shared.machine.write_new_line_twice()

            result += shared.machine.write_jump_to_next_page()

            result += self.answers_title_to_str()
            result += self.texts_to_str('ans', 0)

            result += shared.machine.write_document_ends()

        elif self.layout_type == 'mental':
            # if self.slideshow:
            #    result += shared.machine.write_document_header(slideshow=True)
            #    result += shared.machine.write_document_begins()
            #    result += self.sheet_header_to_str()
            #    result += self.sheet_title_to_str()
            #    result += self.sheet_text_to_str()
            #    result += self.texts_to_str('exc', 0)
            #    result += shared.machine.write_document_ends()
            # else:
            result += shared.machine.write_document_header()
            result += shared.machine.write_document_begins()
            result += self.sheet_header_to_str()
            result += self.sheet_title_to_str()
            result += self.sheet_text_to_str()
            result += self.texts_to_str('exc', 0)
            result += shared.machine.write_jump_to_next_page()
            result += self.answers_title_to_str()
            result += self.texts_to_str('ans', 0)
            result += shared.machine.write_document_ends()

        else:
            raise error.OutOfRangeArgument(
                self.layout_type,
                "std|short_test|mini_test|equations|mental")

        return result