def __init__(self, source, index): self._source = simplify(source) self._start, self._stop = get_slice_indices(len(source), index) self._len = self._stop - self._start if self._len <= 0: self.replacement = Constant() self._len = 0 elif self._start <= 0 and self._stop >= len(source): self.replacement = source else: source.replacement_available.connect(self._replace_source) self._replace_source()
def replace(self, index, replacement): """Replace the given element (or slice) with a value (This is the equivalent of :token:`__setitem__`, but it returns a new Expression instead of modifying the old.) :param index: Index, or slice, to be replaced :param replacement: The new value :type replacement: Expression, tuple, or a simple number Any element of an expression can be replaced:: >>> Constant(1, 2, 3).replace(0, -2) <-2.0, 2.0, 3.0> This can be used to change the size of the expression:: >>> Constant(1, 2, 3).replace(0, (-2, -3)) <-2.0, -3.0, 2.0, 3.0> Slices can be replaced as well:: >>> Constant(1, 2, 3).replace(slice(0, -1), (-2, -3)) <-2.0, -3.0, 3.0> >>> Constant(1, 2, 3).replace(slice(1, 1), (-8, -9)) <1.0, -8.0, -9.0, 2.0, 3.0> >>> Constant(1, 2, 3).replace(slice(1, None), ()) <1.0> >>> Constant(1, 2, 3).replace(slice(None, None), ()) <> When replacing a slice by a plain number, the number is repeated so that the size does not change:: >>> Constant(1, 2, 3).replace(slice(0, -1), -1) <-1.0, -1.0, 3.0> Of course this does not happen when using a tuple or expression:: >>> Constant(1, 2, 3).replace(slice(0, -1), (-1,)) <-1.0, 3.0> >>> Constant(1, 2, 3).replace(slice(0, -1), Constant(-1)) <-1.0, 3.0> """ start, stop = get_slice_indices(len(self), index) replacement = coerce(replacement, size=stop - start, strict=False) return simplify(Concat(self[:start], replacement, self[stop:]))
def __getitem__(self, index): start, end = get_slice_indices(len(self), index) new_children = [] for child in self._children: child_len = len(child) if end <= 0: break elif start >= child_len: pass else: if start <= 0 and end >= child_len: new_children.append(child) else: new_children.append(child[start:end]) start -= child_len if start < 0: start = 0 end -= child_len return Concat(*new_children)
def __getitem__(self, index): start, end = get_slice_indices(len(self), index) index = slice(self._start + start, self._start + end) return _ComponentProperty( vector_property=self._vector_property, index=index)
def __init__(self, vector_property, index): self._vector_property = vector_property self._start, self._end = get_slice_indices(len(vector_property), index) self.name = vector_property._get_component_name(self._start, self._end)
def __getitem__(self, index): start, end = get_slice_indices(len(self), index) return Constant(*self._value[slice(start, end)])