mathmaker
0.6(alpha)
|
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 import error 00024 from common import cfg 00025 import math 00026 import decimal 00027 00028 # -------------------------------------------------------------------------- 00029 ## 00030 # @brief True if argument is a string 00031 # @param objct The object to test 00032 # @return True if argument is a string 00033 def a_string(objct): 00034 if type(objct) == str: 00035 return True 00036 else: 00037 return False 00038 00039 00040 00041 00042 00043 # -------------------------------------------------------------------------- 00044 ## 00045 # @brief True if argument is a string containing only numbers 0-9, -, + or . 00046 # @param objct The object to test 00047 # @return True if argument is a string 00048 def a_numerical_string(objct): 00049 if type(objct) == str and objct != "": 00050 for i in range(len(objct)): 00051 if not(objct[i] == '+' \ 00052 or objct[i] == '-' \ 00053 or objct[i] == '.' \ 00054 or objct[i] == '0' \ 00055 or objct[i] == '1' \ 00056 or objct[i] == '2' \ 00057 or objct[i] == '3' \ 00058 or objct[i] == '4' \ 00059 or objct[i] == '5' \ 00060 or objct[i] == '6' \ 00061 or objct[i] == '7' \ 00062 or objct[i] == '8' \ 00063 or objct[i] == '9'): 00064 #___ 00065 return False 00066 00067 return True 00068 00069 else: 00070 return False 00071 00072 00073 00074 00075 00076 # -------------------------------------------------------------------------- 00077 ## 00078 # @brief True if argument is a list containing only strings 00079 # @param objct The object to test 00080 # @return True if argument is a list containing only strings 00081 def a_string_list(objct): 00082 if type(objct) != list: 00083 return False 00084 00085 for i in range(len(objct)): 00086 if type(objct[i]) != str: 00087 return False 00088 00089 return True 00090 00091 00092 00093 00094 00095 # -------------------------------------------------------------------------- 00096 ## 00097 # @brief True if argument is an ordered Exponented objects list 00098 # @param provided_list The list to check 00099 # @return True if argument is an ordered Exponented objects list 00100 def an_ordered_calculable_objects_list(provided_list): 00101 # Caution, the provided list must contain only Exponented objects that 00102 # can be ordered with the Exponented.alphabetical_order_cmp() function 00103 for i in range(len(provided_list)): 00104 if i < len(provided_list) - 1: 00105 if provided_list[i].alphabetical_order_cmp(provided_list[i+1]) > 0: 00106 return False 00107 00108 return True 00109 00110 00111 00112 00113 00114 # -------------------------------------------------------------------------- 00115 ## 00116 # @brief True if argument is a sign ('+' or '-') 00117 # @param objct The object to test 00118 # @return True if argument is a sign ('+' or '-') 00119 def a_sign(objct): 00120 if (objct == '+') or (objct == '-'): 00121 return True 00122 else: 00123 return False 00124 00125 00126 00127 00128 00129 # -------------------------------------------------------------------------- 00130 ## 00131 # @brief True if the argument is a number 00132 # @param objct The object to test 00133 # @todo Maybe add other kind of objects like fractions ??... 00134 # @return True if the argument is a number 00135 def a_number(objct): 00136 if (type(objct) == float) \ 00137 or (type(objct) == int) \ 00138 or (type(objct) == decimal.Decimal): 00139 return True 00140 else: 00141 return False 00142 00143 00144 00145 00146 00147 # -------------------------------------------------------------------------- 00148 ## 00149 # @brief True if argument is an int or a decimal.Decimal containing an int 00150 # @param objct The object to test 00151 # @return True if argument is an int or a decimal.Decimal containing an int 00152 def an_integer(objct): 00153 if (type(objct) == int): 00154 return True 00155 elif isinstance(objct, decimal.Decimal): 00156 return objct - objct.to_integral_exact() == 0 00157 else: 00158 return False 00159 00160 00161 00162 00163 00164 # -------------------------------------------------------------------------- 00165 ## 00166 # @brief True if argument is an int 00167 # @param objct The object to test 00168 # @return True if argument is an int 00169 def an_int(objct): 00170 if (type(objct) == int): 00171 return True 00172 else: 00173 return False 00174 00175 00176 00177 00178 00179 # -------------------------------------------------------------------------- 00180 ## 00181 # @brief True if the argument is a natural int (e.g. positive) 00182 # @param objct The object to test 00183 # @return True if the argument is a natural int (e.g. positive) 00184 def a_natural_int(objct): 00185 if (type(objct) == int): 00186 if objct >= 0: 00187 return True 00188 else: 00189 return False 00190 else: 00191 return False