mathmaker  0.6(alpha)
mamk_misc/doc/mathmaker4doxygen/sheet/exercise/question/Q_Structure.py
00001 # -*- coding: utf-8 -*-
00002 
00003 # Mathmaker creates automatically maths exercises sheets
00004 # with their answers
00005 # Copyright 2006-2014 Nicolas Hainaux <nico_h@users.sourceforge.net>
00006 
00007 # This file is part of Mathmaker.
00008 
00009 # Mathmaker is free software; you can redistribute it and/or modify
00010 # it under the terms of the GNU General Public License as published by
00011 # the Free Software Foundation; either version 3 of the License, or
00012 # any later version.
00013 
00014 # Mathmaker is distributed in the hope that it will be useful,
00015 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 # GNU General Public License for more details.
00018 
00019 # You should have received a copy of the GNU General Public License
00020 # along with Mathmaker; if not, write to the Free Software
00021 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023 from lib import error
00024 # ------------------------------------------------------------------------------
00025 # --------------------------------------------------------------------------
00026 # ------------------------------------------------------------------------------
00027 ##
00028 # @class Q_Structure
00029 # @brief Contains the method to be reimplemented by any question.*
00030 class Q_Structure(object):
00031 
00032 
00033 
00034 
00035 
00036     # --------------------------------------------------------------------------
00037     ##
00038     #   @brief /!\ Must be redefined. Constructor.
00039     #   @warning Exception NotInstanciableObject.
00040     #   @param embedded_machine The machine to be used
00041     #   @param **options Any options
00042     def __init__(self, embedded_machine,
00043                  q_kind, AVAILABLE_Q_KIND_VALUES,
00044                  **options):
00045         try:
00046             self.derived
00047         except AttributeError:
00048             raise error.NotInstanciableObject(self)
00049 
00050         self.machine = embedded_machine.clone(embedded_machine.language_code)
00051         self.machine.set_redirect_output_to_str(True)
00052 
00053         # OPTIONS -------------------------------------------------------------
00054         # It is necessary to define an options field to pass the
00055         # possibly modified value to the child class
00056         self.options = options
00057 
00058         # That's the number of the question, not of the expressions it might
00059         # contain !
00060         self.number = ""
00061         if 'number_of_the_question' in options:
00062             self.number = options['number_of_the_question']
00063 
00064         try:
00065             AVAILABLE_Q_KIND_VALUES[q_kind]
00066         except KeyError:
00067             raise error.OutOfRangeArgument(q_kind, str(AVAILABLE_Q_KIND_VALUES))
00068 
00069         self.displayable_number = ""
00070 
00071         if self.number != "":
00072             self.displayable_number = \
00073                 self.machine.write(str(self.number) + ". ", emphasize='bold')
00074 
00075         q_subkind = 'default'
00076         if 'q_subkind' in options:
00077             q_subkind = options['q_subkind']
00078             # let's remove this option from the options
00079             # since we re-use it recursively
00080             temp_options = dict()
00081             for key in options:
00082                 if key != 'q_subkind':
00083                     temp_options[key] = options[key]
00084             self.options = temp_options
00085 
00086         if not q_subkind in AVAILABLE_Q_KIND_VALUES[q_kind]:
00087             raise error.OutOfRangeArgument(q_subkind,
00088                                            str(AVAILABLE_Q_KIND_VALUES[q_kind]))
00089 
00090         # these two fields for the case of needing to know the them in the
00091         # answer_to_str() especially
00092         self.q_kind = q_kind
00093         self.q_subkind = q_subkind
00094 
00095 
00096 
00097 
00098 
00099 
00100 
00101     # --------------------------------------------------------------------------
00102     ##
00103     #   Redirects to text_to_str() or answer_to_str()
00104     def to_str(self, ex_or_answers):
00105         if ex_or_answers == 'exc':
00106             return self.text_to_str()
00107 
00108         elif ex_or_answers == 'ans':
00109             return self.answer_to_str()
00110 
00111         else:
00112             raise error.OutOfRangeArgument(ex_or_answers, 'exc|ans')
00113 
00114 
00115 
00116 
00117 
00118 
00119     # --------------------------------------------------------------------------
00120     ##
00121     #   @brief /!\ Must be redefined.
00122     #   Returns a str
00123     #   @warning Exception NotInstanciableObject.
00124     def text_to_str(self, **options):
00125         raise error.MethodShouldBeRedefined(self, 'text_to_str')
00126 
00127 
00128 
00129 
00130 
00131 
00132     # --------------------------------------------------------------------------
00133     ##
00134     #   @brief /!\ Must be redefined.
00135     #   Writes the answers of the questions to the output.
00136     #   @warning Exception NotInstanciableObject.
00137     def answer_to_str(self, **options):
00138         raise error.MethodShouldBeRedefined(self, 'answer_to_str')
00139 
00140 
00141 
00142 
00143