示例#1
0
    def __add__(self, argument):
        r'''Concatenate containers self and argument. The operation c = a + b
        returns a new RhythmTreeContainer c with the content of both a and b,
        and a preprolated_duration equal to the sum of the durations
        of a and b. The operation is non-commutative: the content of the
        first operand will be placed before the content of the second operand:

        >>> a = abjad.rhythmtreetools.RhythmTreeParser()('(1 (1 1 1))')[0]
        >>> b = abjad.rhythmtreetools.RhythmTreeParser()('(2 (3 4))')[0]

        >>> c = a + b

        >>> c.preprolated_duration
        Duration(3, 1)

        >>> abjad.f(c)
        abjad.rhythmtreetools.RhythmTreeContainer(
            children=(
                abjad.rhythmtreetools.RhythmTreeLeaf(
                    preprolated_duration=abjad.Duration(1, 1),
                    is_pitched=True,
                    ),
                abjad.rhythmtreetools.RhythmTreeLeaf(
                    preprolated_duration=abjad.Duration(1, 1),
                    is_pitched=True,
                    ),
                abjad.rhythmtreetools.RhythmTreeLeaf(
                    preprolated_duration=abjad.Duration(1, 1),
                    is_pitched=True,
                    ),
                abjad.rhythmtreetools.RhythmTreeLeaf(
                    preprolated_duration=abjad.Duration(3, 1),
                    is_pitched=True,
                    ),
                abjad.rhythmtreetools.RhythmTreeLeaf(
                    preprolated_duration=abjad.Duration(4, 1),
                    is_pitched=True,
                    ),
                ),
            preprolated_duration=abjad.Duration(3, 1),
            )

        Returns new RhythmTreeContainer.
        '''
        from abjad.tools.rhythmtreetools.RhythmTreeParser \
            import RhythmTreeParser
        if isinstance(argument, str):
            argument = RhythmTreeParser()(argument)
            assert 1 == len(argument) and isinstance(argument[0], type(self))
            argument = argument[0]
        container = type(self)(preprolated_duration=self.preprolated_duration +
                               argument.preprolated_duration)
        container.extend(self[:])
        container.extend(argument[:])
        return container
    def __setitem__(self, i, expr):
        r'''Set `expr` in self at nonnegative integer index `i`,
        or set `expr` in self at slice i.
        Replace contents of `self[i]` with `expr`.
        Attach parentage to contents of `expr`,
        and detach parentage of any replaced nodes.

        ::

            >>> a = rhythmtreetools.RhythmTreeContainer()
            >>> b = rhythmtreetools.RhythmTreeLeaf()
            >>> c = rhythmtreetools.RhythmTreeLeaf()

        ::

            >>> a.append(b)
            >>> b.parent is a
            True

        ::

            >>> a.children == (b,)
            True

        ::

            >>> a[0] = c

        ::

            >>> c.parent is a
            True

        ::

            >>> b.parent is None
            True

        ::

            >>> a.children == (c,)
            True

        Return `None`.
        '''
        from abjad.tools.rhythmtreetools.RhythmTreeParser \
            import RhythmTreeParser

        proper_parentage = self.proper_parentage

        if isinstance(i, int):
            if isinstance(expr, str):
                expr = RhythmTreeParser()(expr)[0]
                assert len(expr) == 1
                expr = expr[0]
            else:
                assert isinstance(expr, self._node_class)
            old = self[i]
            assert expr not in proper_parentage
            old._set_parent(None)
            expr._set_parent(self)
            self._children.insert(i, expr)
        else:
            if isinstance(expr, str):
                expr = RhythmTreeParser()(expr)
            elif isinstance(expr, list) and len(expr) == 1 and \
                isinstance(expr[0], str):
                expr = RhythmTreeParser()(expr[0])
            else:
                assert all(isinstance(x, self._node_class) for x in expr)
            if i.start == i.stop and i.start is not None \
                and i.stop is not None and i.start <= -len(self):
                start, stop = 0, 0
            else:
                start, stop, stride = i.indices(len(self))
            old = self[start:stop]
            for node in expr:
                assert node not in proper_parentage
            for node in old:
                node._set_parent(None)
            for node in expr:
                node._set_parent(self)
            self._children.__setitem__(slice(start, start), expr)
        self._mark_entire_tree_for_later_update()
示例#3
0
    def __setitem__(self, i, expr):
        r'''Set `expr` in self at nonnegative integer index `i`,
        or set `expr` in self at slice i.
        Replace contents of `self[i]` with `expr`.
        Attach parentage to contents of `expr`,
        and detach parentage of any replaced nodes.

        ::

            >>> a = rhythmtreetools.RhythmTreeContainer()
            >>> b = rhythmtreetools.RhythmTreeLeaf()
            >>> c = rhythmtreetools.RhythmTreeLeaf()

        ::

            >>> a.append(b)
            >>> b.parent is a
            True

        ::

            >>> a.children == (b,)
            True

        ::

            >>> a[0] = c

        ::

            >>> c.parent is a
            True

        ::

            >>> b.parent is None
            True

        ::

            >>> a.children == (c,)
            True

        Return `None`.
        '''
        from abjad.tools.rhythmtreetools.RhythmTreeParser \
            import RhythmTreeParser

        proper_parentage = self.proper_parentage

        if isinstance(i, int):
            if isinstance(expr, str):
                expr = RhythmTreeParser()(expr)[0]
                assert len(expr) == 1
                expr = expr[0]
            else:
                assert isinstance(expr, self._node_class)
            old = self[i]
            assert expr not in proper_parentage
            old._set_parent(None)
            expr._set_parent(self)
            self._children.insert(i, expr)
        else:
            if isinstance(expr, str):
                expr = RhythmTreeParser()(expr)
            elif isinstance(expr, list) and len(expr) == 1 and \
                isinstance(expr[0], str):
                expr = RhythmTreeParser()(expr[0])
            else:
                assert all(isinstance(x, self._node_class) for x in expr)
            if i.start == i.stop and i.start is not None \
                and i.stop is not None and i.start <= -len(self):
                start, stop = 0, 0
            else:
                start, stop, stride = i.indices(len(self))
            old = self[start:stop]
            for node in expr:
                assert node not in proper_parentage
            for node in old:
                node._set_parent(None)
            for node in expr:
                node._set_parent(self)
            self._children.__setitem__(slice(start, start), expr)
        self._mark_entire_tree_for_later_update()
示例#4
0
    def __add__(self, expr):
        r'''Concatenate containers self and expr. The operation c = a + b
        returns a new RhythmTreeContainer c with the content of both a and b,
        and a preprolated_duration equal to the sum of the durations
        of a and b. The operation is non-commutative: the content of the
        first operand will be placed before the content of the second operand:

        ::

            >>> a = rhythmtreetools.RhythmTreeParser()('(1 (1 1 1))')[0]
            >>> b = rhythmtreetools.RhythmTreeParser()('(2 (3 4))')[0]

        ::

            >>> c = a + b

        ::

            >>> c.preprolated_duration
            Duration(3, 1)

        ::

            >>> print(format(c))
            rhythmtreetools.RhythmTreeContainer(
                children=(
                    rhythmtreetools.RhythmTreeLeaf(
                        preprolated_duration=durationtools.Duration(1, 1),
                        is_pitched=True,
                        ),
                    rhythmtreetools.RhythmTreeLeaf(
                        preprolated_duration=durationtools.Duration(1, 1),
                        is_pitched=True,
                        ),
                    rhythmtreetools.RhythmTreeLeaf(
                        preprolated_duration=durationtools.Duration(1, 1),
                        is_pitched=True,
                        ),
                    rhythmtreetools.RhythmTreeLeaf(
                        preprolated_duration=durationtools.Duration(3, 1),
                        is_pitched=True,
                        ),
                    rhythmtreetools.RhythmTreeLeaf(
                        preprolated_duration=durationtools.Duration(4, 1),
                        is_pitched=True,
                        ),
                    ),
                preprolated_duration=durationtools.Duration(3, 1),
                )

        Returns new RhythmTreeContainer.
        '''
        from abjad.tools.rhythmtreetools.RhythmTreeParser \
            import RhythmTreeParser
        if isinstance(expr, str):
            expr = RhythmTreeParser()(expr)
            assert 1 == len(expr) and isinstance(expr[0], type(self))
            expr = expr[0]
        container = type(self)(
            preprolated_duration=self.preprolated_duration +
            expr.preprolated_duration)
        container.extend(self[:])
        container.extend(expr[:])
        return container