def test_keys(self): xam = lpx.XAttrMap() xam['a'] = lpx.XAttr('a', -1) xam['b'] = lpx.XAttr('b', lpx.FloatVector([1.5, 2])) xad = XAttrDict(xam) assert set(xad.keys()) == set(['a', 'b'])
def test_to_dict(self): xam = lpx.XAttrMap() xam['a'] = lpx.XAttr('a', -1) xam['b'] = lpx.XAttr('b', lpx.IntVector([1, 2])) xam['c'] = lpx.XAttr( 'c', lpx.IntVector2D([lpx.IntVector([1, 2]), lpx.IntVector([0, 0])])) xam['d'] = lpx.XAttr('d', -1.5) xam['e'] = lpx.XAttr('e', lpx.FloatVector([1.5, 2])) xam['f'] = lpx.XAttr('f', 'f') xam['g'] = lpx.XAttr('g', lpx.StrVector(['gg', 'gg'])) xam['h'] = lpx.XAttr('h', False) xad = XAttrDict(xam) xad['i'] = {'i': ['i1', 'i2']} xad['j'] = {'j': 'j1'} d = xad.to_dict() assert isinstance(d['a'], int) assert isinstance(d['b'], list) assert isinstance(d['c'], list) assert isinstance(d['d'], float) assert isinstance(d['e'], list) assert isinstance(d['f'], str) assert isinstance(d['g'], list) assert isinstance(d['h'], bool) assert isinstance(d['i'], dict) assert isinstance(d['i']['i'], list) assert isinstance(d['j'], dict) assert isinstance(d['j']['j'], str) assert d['j']['j'] == 'j1'
def test_update(self): xam = lpx.XAttrMap() xam['a'] = lpx.XAttr('a', -1) xam['b'] = lpx.XAttr('b', lpx.FloatVector([1.5, 2])) xad = XAttrDict(xam) xad2 = XAttrDict(lpx.XAttrMap()) xad2['b'] = [-1.5, -2.] xad2['c'] = ['a', 'b'] xad.update(xad2) assert len(xad) == 3 assert xad['a'] == -1 assert xad['b'] == [-1.5, -2.] assert xad['c'] == ['a', 'b'] xad.update({'d': [[1, 2], [3, 4]]}) assert len(xad) == 4 assert xad['d'] == [[1, 2], [3, 4]] # Change type of 'b' attribute and add another xad.update({'b': ['a', 'b', 'c'], 'e': [1, 2]}) assert len(xad) == 5 assert xad['b'] == ['a', 'b', 'c'] assert xad['e'] == [1, 2]
def test_getitem(self): xam = lpx.XAttrMap() xam['a'] = lpx.XAttr('a', -1) xam['b'] = lpx.XAttr('b', lpx.IntVector([1, 2])) xam['c'] = lpx.XAttr( 'c', lpx.IntVector2D([lpx.IntVector([1, 2]), lpx.IntVector([0, 0])])) xam['d'] = lpx.XAttr('d', -1.5) xam['e'] = lpx.XAttr('e', lpx.FloatVector([1.5, 2])) xam['f'] = lpx.XAttr('f', 'f') xam['g'] = lpx.XAttr('g', lpx.StrVector(['gg', 'gg'])) xam['h'] = lpx.XAttr('h', True) xam['i'] = lpx.XAttr('i', lpx.MapStrVectorStr()) xad = XAttrDict(xam) xad['i'] = {'i': ['i1', 'i2']} xad['j'] = {'j': 'j1'} assert xad['a'] == -1 assert xad['b'] == [1, 2] assert xad['c'] == [[1, 2], [0, 0]] assert xad['d'] == -1.5 assert xad['e'] == [1.5, 2] assert xad['f'] == 'f' assert xad['g'] == ['gg', 'gg'] assert xad['h'] is True assert xad['i'] == {'i': ['i1', 'i2']} assert xad['i'] != {'a': ['a']} assert xad['j'] == {'j': 'j1'} with self.assertRaises(KeyError): xad['z']
def test_values(self): xam = lpx.XAttrMap() xam['a'] = lpx.XAttr('a', -1) xam['b'] = lpx.XAttr('b', lpx.FloatVector([1.5, 2])) xad = XAttrDict(xam) assert -1 in xad.values() assert [1.5, 2] in xad.values()
def test_items(self): xam = lpx.XAttrMap() xam['a'] = lpx.XAttr('a', -1) xam['b'] = lpx.XAttr('b', lpx.FloatVector([1.5, 2])) xad = XAttrDict(xam) assert ('a', -1) in xad.items() assert ('b', [1.5, 2.]) in xad.items() assert ('c', 0) not in xad.items()
def test_xattr(self): xa = lpx.XAttr('a', -1) assert xa.name == 'a' assert xa.type == 'INT' assert xa.i == -1 xa = lpx.XAttr('b', lpx.IntVector([1, 2, 3])) assert xa.name == 'b' assert xa.type == 'INTS' assert xa.ints == lpx.IntVector([1, 2, 3]) xa = lpx.XAttr( 'c', lpx.IntVector2D([lpx.IntVector([1, 2, 3]), lpx.IntVector([0, 0])])) assert xa.name == 'c' assert xa.type == 'INTS2D' assert xa.ints2d == lpx.IntVector2D( [lpx.IntVector([1, 2, 3]), lpx.IntVector([0, 0])]) xa = lpx.XAttr('d', -1.5) assert xa.name == 'd' assert xa.type == 'FLOAT' assert xa.f == -1.5 xa = lpx.XAttr('e', lpx.FloatVector([1, 2, 3])) assert xa.name == 'e' assert xa.type == 'FLOATS' assert xa.floats == lpx.FloatVector([1, 2, 3]) xa = lpx.XAttr('f', 'f') assert xa.name == 'f' assert xa.type == 'STRING' assert xa.s == 'f' xa = lpx.XAttr('e', lpx.StrVector(['a', 'b'])) assert xa.name == 'e' assert xa.type == 'STRINGS' assert xa.strings == lpx.StrVector(['a', 'b'])
def test_pop(self): xam = lpx.XAttrMap() xam['a'] = lpx.XAttr('a', -1) xam['b'] = lpx.XAttr('b', lpx.FloatVector([1.5, 2])) xad = XAttrDict(xam) assert len(xad) == 2 a = xad.pop('a') assert len(xad) == 1 assert a == -1 b = xad.pop('b') assert len(xad) == 0 assert b == [1.5, 2]
def __setitem__(self, key: str, value): value_error = "Unsupported value: {} for attribute: {}. XAttr values"\ " can only be of types: int, float, str, List[int],"\ " List[float], List[str], List[List[int]],"\ " Dict[str, str], Dict[Str, List[Str]]"\ .format(value, key) # TODO: improve this code if isinstance(value, list): if all([isinstance(e, int) for e in value]): value = lpx.IntVector(value) elif all([isinstance(e, float) for e in value]): value = lpx.FloatVector(value) elif all([isinstance(e, str) for e in value]): value = lpx.StrVector(value) elif all([isinstance(e, list) for e in value]): if all([[isinstance(ee, int) for ee in e] for e in value]): value = lpx.IntVector2D([lpx.IntVector(v) for v in value]) else: raise ValueError(value_error) elif all([isinstance(e, IntVector) for e in value]): value = lpx.IntVector2D([e.get_lpx_vector() for e in value]) else: raise ValueError(value_error) elif isinstance(value, dict): values = list(value.values()) if len(values) > 0 and isinstance(list(value.values())[0], str): value = MapStrStr.from_dict(value).get_lpx_map() else: value = MapStrVectorStr.from_dict(value).get_lpx_map() # raise ValueError("Unsupported dictionary for XAttr with" # " values" # " of type: {}, only 'Str' and 'List[Str]'" # " supported" # .format(type(value.volues()[0]))) elif isinstance(value, (MapStrVectorStr, MapStrStr)): value = value.get_lpx_map() elif isinstance(value, (StrVector, IntVector, IntVector2D, FloatVector)): value = value.get_lpx_vector() elif value is not None and \ not isinstance(value, (float, int, str, StrVector, IntVector, IntVector2D, FloatVector)): raise ValueError(value_error) if value is not None: xattr = lpx.XAttr(key, value) else: xattr = lpx.XAttr(key) self._xattr_map[key] = xattr
def test_len(self): xam = lpx.XAttrMap() xam['a'] = lpx.XAttr('a', -1) xam['b'] = lpx.XAttr('b', lpx.FloatVector([1.5, 2])) xad = XAttrDict(xam) assert len(xad) == 2 assert len(xam) == 2 xam['c'] = lpx.XAttr('c', 'c') assert len(xad) == 3 del xad['c'] assert len(xad) == 2 xad.clear() assert len(xad) == 0
def test_get_copy(self): xam = lpx.XAttrMap() xam['a'] = lpx.XAttr('a', -1) xam['b'] = lpx.XAttr('b', lpx.IntVector([1, 2])) xam['c'] = lpx.XAttr( 'c', lpx.IntVector2D([lpx.IntVector([1, 2]), lpx.IntVector([0, 0])])) xam['d'] = lpx.XAttr('d', -1.5) xam['e'] = lpx.XAttr('e', lpx.FloatVector([1.5, 2])) xam['f'] = lpx.XAttr('f', 'f') xam['g'] = lpx.XAttr('g', lpx.StrVector(['gg', 'gg'])) xam['h'] = lpx.XAttr('h', False) xad = XAttrDict(xam) a_c = xad._get_copy('a') assert a_c == -1 xad['a'] = 1 assert a_c == -1 assert xad['a'] == 1 ints_c = xad._get_copy('b') assert ints_c == [1, 2] xad['b'][0] = -1 assert xad['b'] == [-1, 2] assert ints_c == [1, 2] ints2d_c = xad._get_copy('c') assert ints2d_c == [[1, 2], [0, 0]] xad['c'][0] = [-1, 2] assert xad['c'] == [[-1, 2], [0, 0]] assert ints2d_c == [[1, 2], [0, 0]] assert xad._get_copy('d') == -1.5 floats_c = xad._get_copy('e') assert floats_c == [1.5, 2] xad['e'] = [-1.5, 2.] assert xad['e'] == [-1.5, 2] assert floats_c == [1.5, 2] s_c = xad._get_copy('f') assert s_c == 'f' xad['f'] = 'ff' assert xad['f'] == 'ff' assert s_c == 'f' strings_c = xad._get_copy('g') assert strings_c == ['gg', 'gg'] xad['g'] = ['gg'] assert xad['g'] == ['gg'] assert strings_c == ['gg', 'gg'] b_c = xad._get_copy('h') assert b_c is False xad['h'] = True assert xad['h'] is True assert b_c is False with self.assertRaises(KeyError): xad._get_copy('z')
def __setitem__(self, key, value): if isinstance(key, slice): value = lpx.FloatVector(value) return self.get_lpx_vector().__setitem__(key, value)
def test_float_vector(self): iv = lpx.FloatVector([1, 1.5, 3]) ivx = FloatVector(iv) assert ivx == iv assert ivx == [1, 1.5, 3] # Append ivx.append(4) assert len(iv) == 4 assert len(ivx) == 4 # Contains assert 1.5 in iv assert 1.5 in ivx assert 1.51 not in iv assert 1.51 not in ivx with self.assertRaises(TypeError): assert 'a' not in ivx # Delete del ivx[3] assert ivx == [1, 1.5, 3] assert iv == lpx.FloatVector([1, 1.5, 3]) # Equal assert ivx == [1, 1.5, 3] assert ivx == lpx.FloatVector([1, 1.5, 3]) assert iv == lpx.FloatVector([1, 1.5, 3]) # Extend ivx.extend([4, 5]) assert len(iv) == 5 assert len(ivx) == 5 ivx += [6., 6.5] assert len(iv) == 7 assert len(ivx) == 7 del ivx[5] del ivx[5] # Get item assert ivx[3] == 4 assert ivx[-1] == 5 with self.assertRaises(IndexError): ivx[6] # Iter c = [1, 1.5, 3, 4, 5] for i, e in enumerate(ivx): assert e == c[i] for i, e in enumerate(iv): assert e == c[i] # Length assert len(ivx) == len(iv) assert len(ivx) == 5 # Not equal assert iv != lpx.FloatVector([1, 1.5, 3]) assert ivx != [1, 1.5, 3, 4] # Repr assert repr(iv) == "FloatVector[1, 1.5, 3, 4, 5]" assert repr(ivx) == "FloatVector[1, 1.5, 3, 4, 5]" # Str assert str(iv) == "FloatVector[1, 1.5, 3, 4, 5]" assert str(ivx) == "FloatVector[1, 1.5, 3, 4, 5]" # Set ivx[0] = -1 assert ivx == [-1, 1.5, 3, 4, 5] assert iv == lpx.FloatVector([-1, 1.5, 3, 4, 5]) with self.assertRaises(IndexError): ivx[6] = -1 # to list assert ivx.to_list() == [-1, 1.5, 3, 4, 5]