From 16be3e8fc9c391e22e3905343405996140240838 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Fri, 9 Jul 2021 20:05:04 +0200 Subject: [PATCH] prepare formulas for py3 --- app/scodoc/sco_compute_moy.py | 6 +++--- app/scodoc/sco_formulas.py | 18 ++++++++++++++---- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/app/scodoc/sco_compute_moy.py b/app/scodoc/sco_compute_moy.py index d9012c4b..cb0f2738 100644 --- a/app/scodoc/sco_compute_moy.py +++ b/app/scodoc/sco_compute_moy.py @@ -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) diff --git a/app/scodoc/sco_formulas.py b/app/scodoc/sco_formulas.py index f463c4db..2a4e49ec 100644 --- a/app/scodoc/sco_formulas.py +++ b/app/scodoc/sco_formulas.py @@ -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))