From 97ee4a965c7fe3645d3df2949cefa3f81dec76d2 Mon Sep 17 00:00:00 2001 From: Jack Harper Date: Tue, 13 Feb 2024 12:27:47 +0000 Subject: [PATCH] parse lists and dicts from a string constant --- technique/reflectometry/instrument_constants.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/technique/reflectometry/instrument_constants.py b/technique/reflectometry/instrument_constants.py index ae52244..c2bb913 100644 --- a/technique/reflectometry/instrument_constants.py +++ b/technique/reflectometry/instrument_constants.py @@ -7,6 +7,8 @@ except ImportError: from mocks import g +from ast import literal_eval + class InstrumentConstant(object): """ @@ -80,7 +82,17 @@ def get_reflectometry_value(value_name): """ pv_name = "REFL_01:CONST:{}".format(value_name) value = g.get_pv(pv_name, is_local=True) - if value is None: - raise IOError("PV {} does not exist".format(pv_name)) + + if isinstance(value, str) + try: + # this will try and convert from str to dict/list + value = literal_eval(value) + except ValueError: + # probably OK, just return the original string + return value + except SyntaxError: + # Not ok, raise here, a list has probably not been finished with a ] or similar + raise Exception(f"Constant {value_name} cannot be parsed, evaluating {value} threw an exception") + return value