mathmaker  0.4(alpha)
mathmaker_dev/sheet/exercise/X_Equation.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 *
00024 from X_Structure import X_Structure
00025 import question
00026 
00027 # Here the list of available values for the parameter x_kind='' and the
00028 # matching x_subkind values
00029 # Note : the bypass value allows to give the value of *x_subkind* directly to
00030 # the matching question Constructor, bypassing the action of the present class
00031 AVAILABLE_X_KIND_VALUES = \
00032     {'short_test' : ['basic', 'classic', 'classic_harder', 'harder',
00033                      'harder_harder'],
00034      'preformatted' : ['basic_additions', 'any_basic', 'basic_multiplications',
00035                        'classics', 'classic_xtwice_and_any'],
00036      'bypass' : ['any_simple_expandable']
00037     }
00038 
00039 X_LAYOUT_UNIT = "cm"
00040 
00041 STD_LAYOUT_for_9_equations = \
00042               { 'exc' : [ None,                    'all'
00043                         ],
00044                 'ans' : [ [1,         6, 6, 6],    (3, 3, 3)
00045                         ]
00046               }
00047 
00048 # ----------------------  lines_nb    col_widths   questions
00049 X_LAYOUTS = {'default' :
00050               { 'exc' : [ None,                    'all'
00051                         ],
00052                 'ans' : [ [2,         6, 6, 6],    (1, 1, 1,
00053                                                     1, 1, 1)
00054                         ]
00055               },
00056 
00057              ('short_test', 'basic') :
00058               { 'exc' : [ None,                    'all'
00059                         ],
00060                 'ans' : [ [2,         4.5, 4.5],   (1, 1,
00061                                                     1, 1)
00062                         ]
00063               },
00064 
00065              ('short_test', 'classic') :
00066               { 'exc' : [ None,                    'all'
00067                         ],
00068                 'ans' : [ [1,         4.5, 4.5],   (1, 1)
00069                         ]
00070               },
00071 
00072              ('short_test', 'harder') :
00073               { 'exc' : [ None,                    'all'
00074                         ],
00075                 'ans' : [ [1,             6, 6],   (1, 1)
00076                         ]
00077               },
00078 
00079              ('short_test', 'harder_harder') :
00080               { 'exc' : [ None,                    'all'
00081                         ],
00082                 'ans' : [ [1,         7, 11],      (1, 1)
00083                         ]
00084               },
00085 
00086              ('short_test', 'classic_harder') :
00087               { 'exc' : [ None,                    'all'
00088                         ],
00089                 'ans' : [ [1,         5, 5, 7],    (2, 1, 1)
00090                         ]
00091               },
00092 
00093              ('preformatted', 'basic_additions') : STD_LAYOUT_for_9_equations,
00094 
00095              ('preformatted', 'any_basic') : STD_LAYOUT_for_9_equations,
00096 
00097              ('preformatted', 'basic_multiplications') : \
00098                                                     STD_LAYOUT_for_9_equations,
00099 
00100              #('preformatted', 'classic_xtwice_and_any') : EMPTY_LAYOUT,
00101 
00102              ('bypass', 'any_simple_expandable') :
00103               { 'exc' : [ None,                    'all'
00104                         ],
00105                 'ans' : [ [1,            9, 9],    (1, 1),
00106                           [1,            9, 9],    (1, 1),
00107                           [1,            9, 9],    (1, 1)
00108                         ]
00109               }
00110 
00111 
00112             }
00113 
00114 # ------------------------------------------------------------------------------
00115 # --------------------------------------------------------------------------
00116 # ------------------------------------------------------------------------------
00117 ##
00118 # @class X_Equation
00119 # @brief All exercises related with equations resolution.
00120 class X_Equation(X_Structure):
00121 
00122 
00123 
00124 
00125 
00126     # --------------------------------------------------------------------------
00127     ##
00128     #   @brief Constructor.
00129     #   @param embedded_machine The machine that will be used to write output.
00130     #   @param **options Options detailed below :
00131     #          - start_number=<integer>
00132     #                         (should be >= 1)
00133     #          - number_of_questions=<integer>
00134     #            /!\ only useful if you use x_kind and not preformatted
00135     #                         (should be >= 1)
00136     #          - x_kind=<string>
00137     #                         ...
00138     #                         ...
00139     #          - preformatted=<string>
00140     #            /!\ preformatted is useless with short_test
00141     #            /!\ number_of_questions is useless with preformatted
00142     #            /!\ if you use it with the x_kind option, ensure there's a
00143     #                preformatted possibility with this option
00144     #                         'yes'
00145     #                         'OK'
00146     #                         any other value will be understood as 'no'
00147     #          - short_test=<string>
00148     #            /!\ the x_kind option above can't be used along this option
00149     #                         'yes'
00150     #                         'OK'
00151     #                         any other value will be understood as 'no'
00152     #   @return One instance of exercise.Model
00153     def __init__(self, embedded_machine, x_kind='default_nothing', **options):
00154         self.derived = True
00155         X_Structure.__init__(self, embedded_machine,
00156                              x_kind, AVAILABLE_X_KIND_VALUES, X_LAYOUTS,
00157                              X_LAYOUT_UNIT, **options)
00158         # The purpose of this next line is to get the possibly modified
00159         # value of **options
00160         options = self.options
00161 
00162         default_question = question.Q_Equation
00163 
00164         # FINALLY DEPRECATED :)
00165         # this field, which is specific to equation exercises, is used
00166         # in the write_answer() method from the StructureEquation class.
00167         # it can be changed depending on the x_kind of exercise desired.
00168         # should be useful as long as write_answer() is better implemented
00169         # to manage itself the displaying choices... maybe with the help
00170         # of a self.x_kind field ?
00171         #self.number_of_equations_per_column = 2
00172 
00173         # TEXTS OF THE EXERCISE
00174         self.text = {'exc' : "",
00175                      'ans'   : ""
00176                     }
00177 
00178         # alternate texts section
00179         #if self.x_kind == '...':
00180             #self.text = ...
00181 
00182         # PREFORMATTED EXERCISES
00183         if self.x_kind == 'short_test':
00184             if self.x_subkind == 'basic':
00185                 self.questions_list.append(default_question(
00186                                            self.machine,
00187                                            q_kind='basic_addition',
00188                                            expression_number=0
00189                                                             )
00190                                            )
00191                 self.questions_list.append(default_question(
00192                                            self.machine,
00193                                            q_kind='basic_addition_r',
00194                                            expression_number=1
00195                                                             )
00196                                            )
00197                 self.questions_list.append(default_question(
00198                                            self.machine,
00199                                            q_kind='basic_multiplication',
00200                                            expression_number=2
00201                                                             )
00202                                            )
00203                 self.questions_list.append(default_question(
00204                                            self.machine,
00205                                            q_kind='basic_multiplication_r',
00206                                            expression_number=3
00207                                                             )
00208                                            )
00209             elif self.x_subkind == 'classic':
00210                 self.questions_list.append(default_question(
00211                                            self.machine,
00212                                            q_kind='classic',
00213                                            expression_number=4
00214                                                             )
00215                                            )
00216                 self.questions_list.append(default_question(
00217                                            self.machine,
00218                                            q_kind='classic_r',
00219                                            expression_number=5
00220                                                             )
00221                                            )
00222 
00223             elif self.x_subkind == 'classic_harder':
00224             #___
00225                 self.questions_list.append(default_question(
00226                                            self.machine,
00227                                            q_kind='classic',
00228                                            expression_number=4
00229                                                             )
00230                                            )
00231                 self.questions_list.append(default_question(
00232                                            self.machine,
00233                                            q_kind='classic_r',
00234                                            expression_number=5
00235                                                             )
00236                                            )
00237                 self.questions_list.append(default_question(
00238                                            self.machine,
00239                                            q_kind='classic_x_twice',
00240                                            expression_number=6
00241                                                             )
00242                                            )
00243                 self.questions_list.append(default_question(
00244                                            self.machine,
00245                                            q_kind='classic_with_fractions',
00246                                            expression_number=7
00247                                                             )
00248                                            )
00249 
00250             elif self.x_subkind == 'harder':
00251                 self.questions_list.append(default_question(
00252                                            self.machine,
00253                                            q_kind='classic_x_twice',
00254                                            expression_number=6
00255                                                             )
00256                                            )
00257                 self.questions_list.append(default_question(
00258                                            self.machine,
00259                                            q_kind='any_simple_expandable',
00260                                            expression_number=7
00261                                                             )
00262                                            )
00263 
00264             elif self.x_subkind == 'harder_harder':
00265                 #self.number_of_equations_per_column = 1
00266                 self.questions_list.append(default_question(
00267                                            self.machine,
00268                                            q_kind='any_simple_expandable',
00269                                            expression_number=8
00270                                                             )
00271                                            )
00272                 self.questions_list.append(default_question(
00273                                            self.machine,
00274                                            q_kind='any_double_expandable',
00275                                            expression_number=9
00276                                                             )
00277                                            )
00278 
00279         elif self.x_kind == 'preformatted':
00280             if self.x_subkind == 'basic_additions':
00281                 #self.number_of_equations_per_column = 3
00282                 for i in xrange(3):
00283                     self.questions_list.append(default_question(
00284                                                self.machine,
00285                                                q_kind='basic_addition',
00286                                                expression_number=i
00287                                                                 )
00288                                                )
00289                 for i in xrange(2):
00290                     self.questions_list.append(default_question(
00291                                                self.machine,
00292                                                q_kind='basic_addition_r',
00293                                                expression_number=i+3
00294                                                                 )
00295                                                )
00296                 for i in xrange(4):
00297                     self.questions_list.append(default_question(
00298                                                self.machine,
00299                                                q_kind='any_basic_addition',
00300                                                expression_number=i+5
00301                                                                 )
00302                                                )
00303             elif self.x_subkind == 'any_basic':
00304                 #self.number_of_equations_per_column = 3
00305                 for i in xrange(9):
00306                     self.questions_list.append(default_question(
00307                                                self.machine,
00308                                                q_kind='any_basic',
00309                                                expression_number=i
00310                                                                 )
00311                                                )
00312 
00313             elif self.x_subkind == 'basic_multiplications':
00314                 #self.number_of_equations_per_column = 3
00315                 for i in xrange(4):
00316                     self.questions_list.append(default_question(
00317                                                self.machine,
00318                                                q_kind='basic_multiplication',
00319                                                expression_number=i
00320                                                                 )
00321                                                )
00322                 for i in xrange(2):
00323                     self.questions_list.append(default_question(
00324                                                self.machine,
00325                                                q_kind='basic_multiplication_r',
00326                                                expression_number=i+3
00327                                                                 )
00328                                                )
00329                 for i in xrange(3):
00330                     self.questions_list.append(default_question(
00331                                                self.machine,
00332                                                q_kind='any_basic_multiplication',
00333                                                expression_number=i+5
00334                                                                 )
00335                                                )
00336 
00337             elif self.x_subkind == 'classics':
00338                 for i in xrange(3):
00339                     self.questions_list.append(default_question(
00340                                                self.machine,
00341                                                q_kind='classic',
00342                                                expression_number=i
00343                                                                 )
00344                                                )
00345                 for i in xrange(3):
00346                     self.questions_list.append(default_question(
00347                                                self.machine,
00348                                                q_kind='classic_r',
00349                                                expression_number=i+3
00350                                                                 )
00351                                                )
00352 
00353             elif self.x_subkind == 'classic_xtwice_and_any':
00354                 for i in xrange(3):
00355                     self.questions_list.append(default_question(
00356                                                self.machine,
00357                                                q_kind='classic_x_twice',
00358                                                expression_number=i
00359                                                                 )
00360                                                )
00361                 for i in xrange(3):
00362                     self.questions_list.append(default_question(
00363                                                self.machine,
00364                                                q_kind='any_classic',
00365                                                expression_number=i+3
00366                                                                 )
00367                                                )
00368 
00369 
00370         # OTHER EXERCISES
00371         # Take care : the displaying of the answers if
00372         # number_of_questions is different from 6 is not implemented yet !
00373         else:
00374             for i in xrange(self.q_nb):
00375                 self.questions_list.append(                                   \
00376                              default_question(self.machine,
00377                                         q_kind=self.x_subkind,
00378                                         expression_number=i+self.start_number,
00379                                         **options)
00380                                               )
00381 
00382 
00383 
00384 
00385 
00386     # INSTRUCTIONS TO CREATE A NEW EXERCISE -----------------------------------
00387     # - Indicate its name in the header comment
00388     #   the one of documentation (@class)
00389     # - Write the @brief description
00390     # - Replace the Model class name by the chosen one
00391     # - In the constructor comment, replace Model with the chosen name
00392     #   at the @return line
00393     # - Write the class name of the default_question. You must mention it
00394     #   because it will be used in the OTHER EXERCISES section.
00395     # - The different sections to rewrite are :
00396     #   * TEXTS OF THE EXERCISE:
00397     #       default text for all exercises of this class
00398     #   * alternate texts section:
00399     #       if you want to specify a different text for any particular kind
00400     #       of exercise
00401     #   * PREFORMATTED EXERCISES
00402     #       that's where preformatted exercises are described (the ones that
00403     #       won't repeat n times the same kind of randomly question)
00404     #   * OTHER EXERCISES section is meant to all exercises that repeat
00405     #       the same (maybe randomly chosen among many) kind of question.
00406     #       shouldn't be rewritten
00407     # - Finally, if the write_* methods from the exercise.Structure don't
00408     #   match your needs, copy & modify or rewrite them