From ec2d6c597f12c0cfa60addf8a50e562de785d462 Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Fri, 25 Sep 2020 11:00:14 +0200 Subject: [PATCH] added merge_args --- python/slsdet/detector.py | 5 ++++- python/slsdet/utils.py | 44 +++++++++++++++++++++++++++++++++++++- python/tests/test_utils.py | 13 +++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/python/slsdet/detector.py b/python/slsdet/detector.py index ba3368989..ef8b7a16b 100755 --- a/python/slsdet/detector.py +++ b/python/slsdet/detector.py @@ -2929,7 +2929,10 @@ class Detector(CppDetectorApi): @v_limit.setter def v_limit(self, value): - self.setDAC(dacIndex.V_LIMIT, value, True) + value = ut.merge_args(dacIndex.V_LIMIT, value, True) + print(f'{value=}') + ut.set_using_dict(self.setDAC, *value) + # self.setDAC(dacIndex.V_LIMIT, value, True) @property @element diff --git a/python/slsdet/utils.py b/python/slsdet/utils.py index a1d6627ca..927a815cd 100755 --- a/python/slsdet/utils.py +++ b/python/slsdet/utils.py @@ -206,4 +206,46 @@ def add_argument_before(a, args): ret[key] = (a, value) return (ret,) return a, args - \ No newline at end of file + +def add_argument_after(args, a): + """Add a before the other arguments. Also works with + dict that holds args to several modules. Always puts the + args in a dict to be compatible with set_using_dict""" + if isinstance(args, tuple): + return (*args, a) + elif isinstance(args, dict): + ret = {} + for key, value in args.items(): + if isinstance(value, tuple): + ret[key] = (*value, a) + else: + ret[key] = (value, a) + return (ret,) + return args, a + +def pop_dict(args): + for i,a in enumerate(args): + if isinstance(a, dict): + return args.pop(i), i + + +def merge_args(*args): + #find and pop dict since it holds the arguments + n_dict = sum(isinstance(a, dict) for a in args) + if n_dict == 0: + return args + + elif n_dict == 1: + args = [a for a in args] #these are the args to be added + values,pos = pop_dict(args) + ret = {} + for k, v in values.items(): + if not isinstance(v, tuple): + v = (v,) + items = [a for a in args] + items.insert(pos, *v) + ret[k] = tuple(items) + return (ret,) + + else: + raise ValueError("Multiple dictionaries passes cannot merge args") \ No newline at end of file diff --git a/python/tests/test_utils.py b/python/tests/test_utils.py index bccd2cc01..53244d897 100755 --- a/python/tests/test_utils.py +++ b/python/tests/test_utils.py @@ -321,3 +321,16 @@ def test_add_argument_before_dict(): assert add_argument_before("another", {9: "string"}) == ({ 9: ("another", "string") }, ) + + +def test_add_argument_after(): + assert add_argument_after("a", 5) == ("a", 5) + assert add_argument_after(("a", "b"), 3) == ("a", "b", 3) + +def test_add_argument_after_dict(): + assert add_argument_after({0: "a"}, "b") == ({0: ("a", "b")},) + assert add_argument_after({0: ("a", 1)}, True) == ({0: ("a", 1, True)}, ) + +def test_merge_args(): + assert merge_args("a", "b", 1) == ("a", "b", 1) + assert merge_args({0:1, 1:2}, "a") == ({0: (1, "a"), 1: (2, "a")},) \ No newline at end of file