diff --git a/python/slsdet/detector.py b/python/slsdet/detector.py index a513f31f1..e89a45c8f 100755 --- a/python/slsdet/detector.py +++ b/python/slsdet/detector.py @@ -1107,6 +1107,7 @@ class Detector(CppDetectorApi): ut.set_using_dict(self.setDestinationUDPMAC2, mac) @property + @element def udp_srcmac(self): """ Mac address of the receiver (source) udp interface. @@ -1120,7 +1121,7 @@ class Detector(CppDetectorApi): d.udp_srcmac 00:1b:31:01:8a:de """ - return element_if_equal(self.getSourceUDPMAC()) + return self.getSourceUDPMAC() @udp_srcmac.setter def udp_srcmac(self, mac): diff --git a/python/slsdet/utils.py b/python/slsdet/utils.py index 7d99274c1..d9f10be81 100755 --- a/python/slsdet/utils.py +++ b/python/slsdet/utils.py @@ -117,17 +117,22 @@ def make_string_path(path): return _make_string_path(path) def make_ip(arg): - if isinstance(arg, dict): - return {key:_slsdet.IpAddr(value) for key,value in arg.items()} - else: - return _slsdet.IpAddr(arg) + return _make(arg, _slsdet.IpAddr) def make_mac(arg): - if isinstance(arg, dict): - return {key:_slsdet.MacAddr(value) for key,value in arg.items()} - else: - return _slsdet.MacAddr(arg) + return _make(arg, _slsdet.MacAddr) +def _make(arg, transform): + """Helper function for make_mac and make_ip special cases for + dict, list and tuple. Otherwise just calls transform""" + if isinstance(arg, dict): + return {key:transform(value) for key,value in arg.items()} + elif isinstance(arg, list): + return [transform(a) for a in arg] + elif isinstance(arg, tuple): + return tuple(transform(a) for a in arg) + else: + return transform(arg) def set_using_dict(func, args): if isinstance(args, dict) and all(isinstance(k, int) for k in args.keys()): diff --git a/python/tests/test_utils.py b/python/tests/test_utils.py index 862e7b463..63c3fc30d 100755 --- a/python/tests/test_utils.py +++ b/python/tests/test_utils.py @@ -6,82 +6,104 @@ Testing functions from utils.py import pytest from slsdet.utils import * +from slsdet import IpAddr, MacAddr import datetime as dt import pathlib + def test_iterable(): assert is_iterable(5) == False assert is_iterable('abc') == True assert is_iterable([]) == True assert is_iterable(5.9) == False + def test_reduce_time_to_single_value_from_list(): - t = 3*[dt.timedelta(seconds = 1)] + t = 3 * [dt.timedelta(seconds=1)] assert reduce_time(t) == 1 + def test_reduce_time_to_single_value_from_list_of_lists(): - t = 3*[dt.timedelta(seconds = 3.3)] - tt = 5*t + t = 3 * [dt.timedelta(seconds=3.3)] + tt = 5 * t assert reduce_time(tt) == 3.3 + def test_reduce_time_when_sublist_is_different(): - t = [dt.timedelta(seconds = 1), dt.timedelta(seconds = 2), dt.timedelta(seconds = 1)] + t = [ + dt.timedelta(seconds=1), + dt.timedelta(seconds=2), + dt.timedelta(seconds=1) + ] tt = [t for i in range(4)] - assert reduce_time(tt) == [1,2,1] + assert reduce_time(tt) == [1, 2, 1] def test_convert_zero(): assert eiger_register_to_time(0) == 0 + def test_convert_smallest_unit(): assert pytest.approx(eiger_register_to_time(0b1000), 1e-9) == 1e-8 + def test_convert_second_smallest_unit(): assert pytest.approx(eiger_register_to_time(0b10000), 1e-9) == 2e-8 + def test_convert_one_ms_using_exponent(): assert pytest.approx(eiger_register_to_time(0b1101), 1e-9) == 1e-3 + def test_convert_five_seconds(): - assert pytest.approx(eiger_register_to_time(0b1001110001000101), 1e-9) == 5.0 + assert pytest.approx(eiger_register_to_time(0b1001110001000101), + 1e-9) == 5.0 + def test_all_equal_int(): - assert all_equal([5,5]) == True + assert all_equal([5, 5]) == True + def test_all_equal_fails(): - assert all_equal([5,6]) == False + assert all_equal([5, 6]) == False + def test_all_equal_tuple(): assert all_equal(('a', 'a', 'a')) == True + def test_all_equal_str(): assert all_equal('aaa') == True + def test_all_equal_str_fails(): assert all_equal('aaab') == False - def test_element_if_equal_int(): - assert element_if_equal([5,5]) == 5 + assert element_if_equal([5, 5]) == 5 + def test_element_if_equal_str(): assert element_if_equal('hhh') == 'h' + def test_element_if_equal_int_fails(): assert element_if_equal([5, 6, 7]) == [5, 6, 7] + def test_get_set_bits(): - assert(get_set_bits(0) == []) + assert (get_set_bits(0) == []) assert get_set_bits(7) == [0, 1, 2] - + + def test_list_to_mask(): - assert(list_to_bitmask([0,1,2]) == 7) - assert(list_to_bitmask([]) == 0) - assert(list_to_bitmask([0]) == 1) - assert(list_to_bitmask([1]) == 2) - assert(list_to_bitmask([3]) == 8) - assert(list_to_bitmask([1,1,1]) == 2) + assert (list_to_bitmask([0, 1, 2]) == 7) + assert (list_to_bitmask([]) == 0) + assert (list_to_bitmask([0]) == 1) + assert (list_to_bitmask([1]) == 2) + assert (list_to_bitmask([3]) == 8) + assert (list_to_bitmask([1, 1, 1]) == 2) def test_make_timedelta_from_double(): @@ -90,6 +112,7 @@ def test_make_timedelta_from_double(): assert t == r.total_seconds() assert r == dt.timedelta(seconds=t) + def test_make_timedelta_from_timedelta(): t = dt.timedelta(minutes=1) r = make_timedelta(t) @@ -105,6 +128,7 @@ def test_make_string_path_from_Path(): assert r == p.as_posix() assert r == pathstr + def test_make_string_path_expand_user(): pathstr = "~/tmp/virtual.config" home = pathlib.Path.home() @@ -113,4 +137,62 @@ def test_make_string_path_expand_user(): rp = make_string_path(p) rs = make_string_path(pathstr) assert rp == expanded_str - assert rs == expanded_str \ No newline at end of file + assert rs == expanded_str + + +def test_lhex_passing_list(): + values = [0, 1, 2, 3, 4] + assert lhex(values) == ["0x0", "0x1", "0x2", "0x3", "0x4"] + + +def test_lhex_emty_list(): + assert lhex([]) == [] + + +def test_make_ip_from_dict(): + arg = {0: 0, 1: "192.168.1.1"} + res = make_ip(arg) + assert res == {0: IpAddr("0.0.0.0"), 1: IpAddr("192.168.1.1")} + assert res[0].str() == "0.0.0.0" + assert res[1].str() == "192.168.1.1" + + +def test_make_ip_from_str(): + ip = "192.168.1.1" + assert make_ip(ip).str() == ip + + +def test_make_ip_from_list(): + arg = ["192.168.1.1", "192.168.1.2", "127.0.0.1"] + assert make_ip(arg) == [IpAddr(a) for a in arg] + +def test_make_ip_from_tuple(): + arg = ("127.0.0.1") + assert make_ip(arg) == (IpAddr(arg)) + +def test_make_mac_from_dict(): + arg = {6: "84:a9:aa:24:32:88", 12: "84:a9:3e:24:32:aa"} + res = make_mac(arg) + assert res == { + 6: MacAddr("84:a9:aa:24:32:88"), + 12: MacAddr("84:a9:3e:24:32:aa") + } + assert res[6].str() == "84:a9:aa:24:32:88" + assert res[12].str() == "84:a9:3e:24:32:aa" + + +def test_make_mac_from_str(): + mac = "84:a9:aa:24:32:88" + assert make_mac(mac) == MacAddr(mac) + assert make_mac(mac).str() == mac + + +def test_make_mac_from_list(): + arg = ["84:a9:aa:24:32:88", "84:a9:3e:24:32:aa"] + assert make_mac(arg) == [MacAddr(a) for a in arg] + + +def test_make_mac_from_tuple(): + arg = ("84:a9:aa:24:32:88", "84:a9:3e:24:32:aa") + assert make_mac(arg) == (MacAddr("84:a9:aa:24:32:88"), + MacAddr("84:a9:3e:24:32:aa"))