prepare formulas for py3

This commit is contained in:
Emmanuel Viennet 2021-07-09 20:05:04 +02:00
parent 79b8b3c180
commit 16be3e8fc9
2 changed files with 17 additions and 7 deletions

View File

@ -146,7 +146,7 @@ def compute_user_formula(
nbabs, nbabs_just = 0, 0
try:
moy_val = float(moy)
except:
except ValueError:
moy_val = 0.0 # 0. when no valid value
variables = {
"cmask": coefs_mask, # NoteVector(v=coefs_mask),
@ -162,8 +162,8 @@ def compute_user_formula(
}
try:
formula = formula.replace("\n", "").replace("\r", "")
# log('expression : %s\nvariables=%s\n' % (formula, variables)) # XXX debug
user_moy = sco_formulas.eval_user_expression(context, formula, variables)
# log('expression : %s\nvariables=%s\n' % (formula, variables)) # debug
user_moy = sco_formulas.eval_user_expression(formula, variables)
# log('user_moy=%s' % user_moy)
if user_moy != "NA0" and user_moy != "NA":
user_moy = float(user_moy)

View File

@ -73,7 +73,7 @@ class NoteVector(object):
"""Append a value to the vector."""
try:
v = float(value)
except:
except ValueError:
v = 0.0
self.v.append(v)
if name:
@ -99,10 +99,20 @@ class NoteVector(object):
__rmul__ = __mul__
def __div__(self, x):
return binary_op(self.v, x, operator.div)
return binary_op(self.v, x, operator.truediv)
__truediv__ = __div__ # for py3
def __floordiv__(self, x):
return binary_op(self.v, x, operator.floordiv)
def __rdiv__(self, x):
return binary_op(x, self.v, operator.div)
return binary_op(x, self.v, operator.truediv)
__rtruediv__ = __rdiv__ # for py3
def __rfloordiv__(self, x):
return binary_op(x, self.v, operator.floordiv)
def isScalar(x):
@ -169,7 +179,7 @@ formula_builtins = {
# eval("max(4,5)", {'__builtins__': formula_builtins, {'x' : 1, 'v' : NoteVector(1,2) }, {})
def eval_user_expression(context, expression, variables):
def eval_user_expression(expression, variables):
"""Evalue l'expression (formule utilisateur) avec les variables (dict) données."""
variables["__builtins__"] = formula_builtins
# log('Evaluating %s with %s' % (expression, variables))