def validate(self, value): """Verify value can be iterated and cast elements to strings Args: value (list): value to be validated Returns: StringArray: the validated value """ if value is None: return StringArray() else: return StringArray(value)
def test_non_strings_raise(self): with self.assertRaises(ValueError) as cm: StringArray(1, 2, 3) self.assertEqual( str(cm.exception), 'Expected StringArray(s1, s2, ...) or StringArray(seq). ' 'Got StringArray(1, 2, 3)')
def validate(self, value): """Verify value can be iterated and cast elements to choices Args: value (list): Value to be validated Returns: StringArray: The validated value """ if value is None: return StringArray() elif isinstance(value, str_): raise ValueError("Expected iterable of strings, got %r" % value) else: for i, choice in enumerate(value): if choice not in self.choices: raise ValueError("%s is not a valid value for element %s" % (choice, i)) return StringArray(value)
def setUp(self): self.o = PvaUtil() self.d = OrderedDict([ ('generator', OrderedDict([ ('excluders', []), ('generators', [{ 'typeid': 'scanpointgenerator:generator/LineGenerator:1.0', 'name': ['SampleY'], 'stop': [-3.0010000000000003], 'alternate_direction': False, 'start': [-3.099], 'num': 50, 'units': 'mm' }, { 'typeid': 'scanpointgenerator:generator/LineGenerator:1.0', 'name': ['SampleX'], 'stop': [0.099], 'alternate_direction': False, 'start': [0.001], 'num': 50, 'units': 'mm' }]), ('mutators', [{ 'duration': 0.049602, 'typeid': 'scanpointgenerator:mutator/FixedDurationMutator:1.0' }]) ])), ('empty', StringArray()), ('go', np.array([False, True])), ('stop', np.array([1, 2])), ('axesToMove', StringArray('SampleX', 'SampleY')), ('fileDir2', '/dls/i08/data/2017/cm16789-1/nexus/i08-4352'), ('fileDir', '/dls/i08/data/2017/cm16789-1/nexus/i08-4351') ])
def test_dict_to_stucture(self): self.PVA = PvaServerComms(self.p) val_dict = OrderedDict() val_dict["typeid"] = "type1" val_dict["val1"] = "1" val_dict["val2"] = np.int32(2) val_dict["val3"] = True val_dict["val4"] = np.int64(0) val_dict["val5"] = np.float64(0.5) val_dict["val6"] = StringArray('', '') val_dict["val7"] = np.array([5, 1], dtype=np.int32) val_dict["val8"] = [True, False] val_dict["val9"] = np.array([0, 1], dtype=np.int64) val_dict["val10"] = np.array([0.2, 0.6], dtype=np.float64) val = self.PVA.pva_structure_from_value(val_dict) test_dict = OrderedDict() test_dict["val1"] = pvaccess.STRING test_dict["val2"] = pvaccess.INT test_dict["val3"] = pvaccess.BOOLEAN test_dict["val4"] = pvaccess.LONG test_dict["val5"] = pvaccess.DOUBLE test_dict["val6"] = [pvaccess.STRING] test_dict["val7"] = [pvaccess.INT] test_dict["val8"] = [pvaccess.BOOLEAN] test_dict["val9"] = [pvaccess.LONG] test_dict["val10"] = [pvaccess.DOUBLE] test_val = pvaccess.PvObject(test_dict, "type1") self.assertEquals(val, test_val) # Test the variant union array type val = self.PVA.pva_structure_from_value( {"union_array": [{ "val1": 1 }, { "val2": "2" }]}) test_dict = OrderedDict() test_dict["union_array"] = [()] test_val = pvaccess.PvObject(test_dict, "") self.assertEquals(val, test_val) val = self.PVA.pva_structure_from_value({"union_array": []}) test_dict = OrderedDict() test_dict["union_array"] = [()] test_val = pvaccess.PvObject(test_dict, "") self.assertEquals(val, test_val)
def test_dict_to_pv(self): val_dict = OrderedDict() val_dict["typeid"] = "type1" val_dict["val1"] = StringArray('', '') val_dict["val2"] = np.array((1, 2)) val_dict["val3"] = dict(a=43) val_dict["val4"] = [True, False] val_dict["val5"] = [dict(a=43), dict(b=44)] val_dict["val6"] = "s" actual = dict_to_pv_object(val_dict) assert actual._type == "type1" assert actual._dict["val1"] == ["", ""] assert actual._dict["val2"] == [1, 2] assert actual._dict["val3"] == dict(a=43) assert actual._dict["val4"] == [True, False] assert len(actual._dict["val5"]) == 2 assert actual._dict["val5"][0]._dict == dict(a=43) assert actual._dict["val5"][1]._dict == dict(b=44) assert actual._dict["val6"] == "s"
def test_dict_to_pv(self): self.PVA = PvaServerComms(self.p) val_dict = OrderedDict() val_dict["typeid"] = "type1" val_dict["val1"] = StringArray('', '') val_dict["val2"] = np.array((1, 2)) val_dict["val3"] = dict(a=43) val_dict["val4"] = [True, False] val_dict["val5"] = [dict(a=43), dict(b=44)] val_dict["val6"] = "s" actual = self.PVA.dict_to_pv_object(val_dict) self.assertEqual(actual._type, "type1") self.assertEqual(actual._dict["val1"], ["", ""]) self.assertEqual(actual._dict["val2"], [1, 2]) self.assertEqual(actual._dict["val3"], dict(a=43)) self.assertEqual(actual._dict["val4"], [True, False]) self.assertEqual(len(actual._dict["val5"]), 2) self.assertEqual(actual._dict["val5"][0]._dict, dict(a=43)) self.assertEqual(actual._dict["val5"][1]._dict, dict(b=44)) self.assertEqual(actual._dict["val6"], "s")
def test_strings_instance(self): a = StringArray("boo", "bar") self.assertIsInstance(a, tuple) self.assertIsInstance(a, StringArray) assert a == ("boo", "bar")
def test_tuple(self): a = StringArray(("boo", "bar")) assert a == ("boo", "bar")
def test_iterable(self): a = StringArray("a%s" % i for i in range(3)) assert a == ("a0", "a1", "a2")
def test_single_element(self): a = StringArray("boo") assert a == ("boo", )
def test_immutable(self): a = StringArray("boo", "bar") with self.assertRaises(TypeError): a[0] = "bat"
def set_choices(self, choices): """Set the choices list""" choices = StringArray(deserialize_object(c, str_) for c in choices) # TODO: what if the value is no longer in the list? return self.set_endpoint_data("choices", choices)
def test_tuple(self): a = StringArray(("boo", "bar")) self.assertEqual(a, ("boo", "bar"))
def test_iterable(self): a = StringArray("a%s" % i for i in range(3)) self.assertEqual(a, ("a0", "a1", "a2"))
def test_single_element(self): a = StringArray("boo") self.assertEqual(a, ("boo",))