From 6a0261c728fbcb790c533d578f7c04f2639ff52f Mon Sep 17 00:00:00 2001 From: Markus Zolliker Date: Wed, 25 May 2022 09:23:16 +0200 Subject: [PATCH] allow to convert numpy arrays to ArrayOf accept all sequences instead of just tuple / list + change Module.announceUpdate to convert value before comparing with previous one (comparing will not work with numpy arrays) Change-Id: I5eceef4297607107e2dde688af2833d3651a8775 Reviewed-on: https://forge.frm2.tum.de/review/c/sine2020/secop/playground/+/28525 Tested-by: Markus Zolliker Reviewed-by: Markus Zolliker --- secop/datatypes.py | 7 ++++--- secop/modules.py | 5 +++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/secop/datatypes.py b/secop/datatypes.py index 3f5f3df..220a3a9 100644 --- a/secop/datatypes.py +++ b/secop/datatypes.py @@ -760,7 +760,7 @@ class ArrayOf(DataType): def __call__(self, value): """validate an external representation to an internal one""" - if isinstance(value, (tuple, list)): + try: # check number of elements if self.minlen is not None and len(value) < self.minlen: raise BadValueError( @@ -771,8 +771,9 @@ class ArrayOf(DataType): 'Array too big, holds at most %d elements!' % self.minlen) # apply subtype valiation to all elements and return as list return tuple(self.members(elem) for elem in value) - raise BadValueError( - 'Can not convert %s to ArrayOf DataType!' % repr(value)) + except TypeError: + raise BadValueError('%s can not be converted to ArrayOf DataType!' + % type(value).__name__) from None def export_value(self, value): """returns a python object fit for serialisation""" diff --git a/secop/modules.py b/secop/modules.py index ae3ea85..3806302 100644 --- a/secop/modules.py +++ b/secop/modules.py @@ -509,10 +509,11 @@ class Module(HasAccessibles): # TODO: remove readerror 'property' and replace value with exception pobj = self.parameters[pname] timestamp = timestamp or time.time() - changed = pobj.value != value try: + value = pobj.datatype(value) + changed = pobj.value != value # store the value even in case of error - pobj.value = pobj.datatype(value) + pobj.value = value except Exception as e: if isinstance(e, DiscouragedConversion): if DiscouragedConversion.log_message: