Browse Source

[IMP] make SimpleArray produce DataError in memberwise operations

Put DataError and mis_safe_eval in their own files
pull/189/head
Stéphane Bidoul 9 years ago
parent
commit
f8a6daa430
  1. 11
      mis_builder/models/data_error.py
  2. 38
      mis_builder/models/mis_safe_eval.py
  3. 19
      mis_builder/models/simple_array.py
  4. 1
      mis_builder/tests/__init__.py

11
mis_builder/models/data_error.py

@ -0,0 +1,11 @@
# -*- coding: utf-8 -*-
# © 2016 ACSONE SA/NV (<http://acsone.eu>)
# © 2016 Akretion (<http://akretion.com>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
class DataError(Exception):
def __init__(self, name, msg):
self.name = name
self.msg = msg

38
mis_builder/models/mis_safe_eval.py

@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-
# © 2016 ACSONE SA/NV (<http://acsone.eu>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
import traceback
from openerp.tools.safe_eval import test_expr, _SAFE_OPCODES, _BUILTINS
from .data_error import DataError
__all__ = ['mis_safe_eval']
def mis_safe_eval(expr, locals_dict):
""" Evaluate an expression using safe_eval
Returns the evaluated value or DataError.
Raises NameError if the evaluation depends on a variable that is not
present in local_dict.
"""
try:
c = test_expr(expr, _SAFE_OPCODES, mode='eval')
globals_dict = {'__builtins__': _BUILTINS}
val = eval(c, globals_dict, locals_dict)
except NameError:
raise
except ZeroDivisionError:
val = DataError('#DIV/0', traceback.format_exc())
except:
val = DataError('#ERR', traceback.format_exc())
return val
if __name__ == '__main__':
import doctest
doctest.testmod()

19
mis_builder/models/simple_array.py

@ -46,9 +46,16 @@ SimpleArray((2.0, 2.5, 3.0))
SimpleArray((8.0, 10.0, 12.0)) SimpleArray((8.0, 10.0, 12.0))
>>> b += 2 ; b >>> b += 2 ; b
SimpleArray((6.0, 7.0, 8.0)) SimpleArray((6.0, 7.0, 8.0))
>>> a / ((1.0, 0.0, 1.0))
SimpleArray((1.0, DataError(), 3.0))
>>> a / 0.0
SimpleArray((DataError(), DataError(), DataError()))
""" """
import operator import operator
import traceback
from .data_error import DataError
__all__ = ['SimpleArray'] __all__ = ['SimpleArray']
@ -57,12 +64,20 @@ __all__ = ['SimpleArray']
class SimpleArray(tuple): class SimpleArray(tuple):
def _op(self, op, other): def _op(self, op, other):
def _o2(x, y):
try:
return op(x, y)
except ZeroDivisionError:
return DataError('#DIV/0', traceback.format_exc())
except:
return DataError('#ERR', traceback.format_exc())
if isinstance(other, tuple): if isinstance(other, tuple):
if len(other) != len(self): if len(other) != len(self):
raise TypeError("tuples must have same length for %s" % op) raise TypeError("tuples must have same length for %s" % op)
return SimpleArray(map(op, self, other))
return SimpleArray(map(_o2, self, other))
else: else:
return SimpleArray(map(lambda x: op(x, other), self))
return SimpleArray(map(lambda z: _o2(z, other), self))
def __add__(self, other): def __add__(self, other):
return self._op(operator.add, other) return self._op(operator.add, other)

1
mis_builder/tests/__init__.py

@ -6,4 +6,5 @@ from . import test_accounting_none
from . import test_aep from . import test_aep
from . import test_aggregate from . import test_aggregate
from . import test_mis_builder from . import test_mis_builder
from . import test_mis_safe_eval
from . import test_simple_array from . import test_simple_array
Loading…
Cancel
Save