def test___setitem___with_validator(): def validator(fixed_length_list, key=None, value=None): new_fixed_length_list = list(fixed_length_list) if key is not None and value is not None: new_fixed_length_list.__setitem__(key, value) if len(new_fixed_length_list) != len(set(new_fixed_length_list)): raise ValueError('This list cannot have repeated values.') fll = FixedLengthList([1, 2, 3], validator=validator) with pytest.raises(ValueError): _ = FixedLengthList([1, 1, 1], validator=validator) fll[2] = 4 assert fll[2] == 4 fll[1:] = [0, -1] assert fll[0] == 1 assert fll[1] == 0 assert fll[2] == -1 with pytest.raises(TypeError): fll[1:1] = range(10) with pytest.raises(TypeError): fll[1:] = [1] with pytest.raises(TypeError): fll[:] = [1, 2] with pytest.raises(TypeError): fll[:1] = [1, 2] with pytest.raises(ValueError): fll[1] = 1 with pytest.raises(ValueError): fll[1:] = [0, 0]
def accelerations(self, accelerations): if len(self.joint_values) != len(accelerations): raise ValueError( 'Must have {} accelerations, but {} given.'.format( len(self.joint_values), len(accelerations))) self._accelerations = FixedLengthList(accelerations)
def test_fixed_length_list_deepcopy(): fll = FixedLengthList([[1, 2, 3] for _ in range(3)]) fll_copy = copy.deepcopy(fll) assert fll_copy[0][0] == 1 fll_copy[0][0] = 0 assert fll_copy[0][0] == 0 assert fll[0][0] == 1
def test___setitem__(): fll = FixedLengthList([1, 2, 3]) fll[2] = 4 assert fll[2] == 4 fll[:1] = [5] assert fll[0] == 5 with pytest.raises(TypeError): fll[1:1] = range(10)
def data(self, data): self._joint_values = FixedLengthList( data.get('joint_values') or data.get('values') or []) self._joint_types = FixedLengthList( data.get('joint_types') or data.get('types') or []) self._joint_names = FixedLengthList(data.get('joint_names') or []) self._velocities = FixedLengthList(data.get('velocities') or []) self._accelerations = FixedLengthList(data.get('accelerations') or []) self._effort = FixedLengthList(data.get('effort') or []) self.time_from_start = Duration.from_data( data.get('time_from_start') or {})
def effort(self, effort): if len(self.joint_values) != len(effort): raise ValueError('Must have {} efforts, but {} given.'.format( len(self.joint_values), len(effort))) self._effort = FixedLengthList(effort)
def velocities(self, velocities): if len(self.joint_values) != len(velocities): raise ValueError('Must have {} velocities, but {} given.'.format( len(self.joint_values), len(velocities))) self._velocities = FixedLengthList(velocities)
def test_length_altering_ops(): fll = FixedLengthList([1, 2, 3]) with pytest.raises(TypeError): fll.append(4) with pytest.raises(TypeError): fll.extend([4]) with pytest.raises(TypeError): _ = fll.pop() with pytest.raises(TypeError): fll.clear() with pytest.raises(TypeError): fll.insert(1, 7) with pytest.raises(TypeError): fll.remove(1) with pytest.raises(TypeError): fll.remove(7)
def test_fixed_length_list(): fll = FixedLengthList([1, 2, 3]) assert len(fll) == 3