Beispiel #1
0
 def __getitem__(self, key: str):
     """ Parse and return the attribute corresponding to the given key """
     _xattr = self._xattr_map[key]
     assert _xattr.name == key
     if _xattr.type == 'UNDEFINED':
         return None
     elif _xattr.type == 'BOOL':
         return _xattr.b
     elif _xattr.type == 'INT':
         return _xattr.i
     elif _xattr.type == 'INTS':
         return IntVector(_xattr.ints)
     elif _xattr.type == 'INTS2D':
         return IntVector2D(_xattr.ints2d)
     elif _xattr.type == 'FLOAT':
         return _xattr.f
     elif _xattr.type == 'FLOATS':
         return FloatVector(_xattr.floats)
     elif _xattr.type == 'STRING':
         return _xattr.s
     elif _xattr.type == 'STRINGS':
         return StrVector(_xattr.strings)
     elif _xattr.type == 'MAP_STR_STR':
         return MapStrStr(_xattr.map_str_str)
     elif _xattr.type == 'MAP_STR_VSTR':
         return MapStrVectorStr(_xattr.map_str_vstr)
     else:
         raise NotImplementedError(
             "Unsupported attribute: {} of type: {}".format(
                 _xattr, _xattr.type))
Beispiel #2
0
 def _get_copy(self, key: str):
     """ Parse and return a copy of the attribute corresponding
         to the given key """
     _xattr = self._xattr_map[key]
     assert _xattr.name == key
     if _xattr.type == 'UNDEFINED':
         return None
     elif _xattr.type in ['INT', 'FLOAT', 'BOOL']:
         return self.__getitem__(key)
     elif _xattr.type == 'INTS':
         return [i for i in _xattr.ints]
     elif _xattr.type == 'INTS2D':
         return [[ii for ii in i] for i in _xattr.ints2d]
     elif _xattr.type == 'FLOATS':
         return [f for f in _xattr.floats]
     elif _xattr.type == 'STRING':
         return str(_xattr.s)
     elif _xattr.type == 'STRINGS':
         return [s for s in _xattr.strings]
     elif _xattr.type == 'MAP_STR_STR':
         return MapStrStr(_xattr.map_str_str).to_dict()
     elif _xattr.type == 'MAP_STR_VSTR':
         return MapStrVectorStr(_xattr.map_str_vstr).to_dict()
     else:
         raise NotImplementedError(
             "Unsupported attribute: {} of type: {}".format(
                 _xattr, _xattr.type))
Beispiel #3
0
    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
Beispiel #4
0
    def test_map_str_vector_str(self):

        # Constructor

        _m = lpx.MapStrVectorStr()
        _m['a'] = lpx.StrVector(['a', 'b'])
        m = MapStrVectorStr(_m)
        assert len(m) == 1

        # Contains
        assert 'a' in m
        assert 'b' not in m
        with self.assertRaises(TypeError):
            assert 1 not in m

        _m['b'] = lpx.StrVector(['b', 'b'])

        # Delete
        assert len(m) == 2
        del m['b']
        assert len(m) == 1
        assert m['a'] == ['a', 'b']
        _m['c'] = lpx.StrVector(['c', 'c'])

        # Equal
        assert m == {'a': ['a', 'b'], 'c': ['c', 'c']}
        assert m == {'a': lpx.StrVector(['a', 'b']),
                     'c': lpx.StrVector(['c', 'c'])}

        # Get item
        assert m['a'] == ['a', 'b']
        assert m['a'] == lpx.StrVector(['a', 'b'])
        assert m.get('b') is None
        with self.assertRaises(KeyError):
            m['b']

        # Get lpx map
        assert m.get_lpx_map() == _m

        # Items
        assert ('a', ['a', 'b']) in m.items()
        assert ('c', ('c', 'c')) in m.items()

        # Keys
        assert set(m.keys()) == set(['a', 'c'])

        # Length
        assert len(m) == 2
        assert len(_m) == 2

        # Not equal
        assert m != {'a': ['a', 'b']}

        # Set
        m['d'] = ['d']
        assert len(m) == 3
        m['a'] = lpx.StrVector(['a'])
        assert len(m) == 3
        assert m['a'] == ['a']
        m['c'][0] = 'cc'

        # to dict
        d = m.to_dict()
        assert len(d) == 3
        assert d['a'] == ['a']
        assert d['c'] == ['cc', 'c']
        d['c'][1] = 'cc'
        assert d['c'] == ['cc', 'cc']
        assert m['c'] == ['cc', 'c']
        assert d['d'] == ['d']

        # Update
        m.update({'a': ['update'], 'e': ['e']})
        assert len(m) == 4
        assert m['a'][0] == 'update'
        assert m['e'] == ['e']
Beispiel #5
0
 def test_map_str_vector_str_from_dict(self):
     m = MapStrVectorStr.from_dict({'a': ['a', 'b'], 'b': ['b']})
     assert len(m) == 2
     assert m['a'] == ['a', 'b']
     assert m['b'] == ['b']