Ejemplo n.º 1
0
    def __add__(self, argument):
        r'''Adds typed tuple to `argument`.

        Returns new typed tuple.
        '''
        if isinstance(argument, type(self)):
            items = argument._collection
            return new(self, items=self._collection[:] + items)
        elif isinstance(argument, type(self._collection)):
            items = argument[:]
            return new(self, items=self._collection[:] + items)
        raise NotImplementedError
Ejemplo n.º 2
0
    def __add__(self, argument):
        """
        Adds typed tuple to ``argument``.

        Returns new typed tuple.
        """
        if isinstance(argument, type(self)):
            items = argument._collection
            return new(self, items=self._collection[:] + items)
        elif isinstance(argument, type(self._collection)):
            items = argument[:]
            return new(self, items=self._collection[:] + items)
        raise NotImplementedError
Ejemplo n.º 3
0
    def __mul__(self, argument):
        r'''Multiplies typed tuple by `argument`.

        Returns new typed tuple.
        '''
        items = self._collection * argument
        return new(self, items=items)
Ejemplo n.º 4
0
 def _get_lilypond_format_bundle(self, component=None):
     bundle = LilyPondFormatBundle()
     markup = self._get_markup()
     markup = new(markup, direction=Up)
     markup_format_pieces = markup._get_format_pieces()
     bundle.right.markup.extend(markup_format_pieces)
     return bundle
Ejemplo n.º 5
0
    def __mul__(self, argument):
        """
        Multiplies typed tuple by ``argument``.

        Returns new typed tuple.
        """
        items = self._collection * argument
        return new(self, items=items)
Ejemplo n.º 6
0
 def _get_lilypond_format_bundle(self, component=None):
     from abjad.tools import systemtools
     lilypond_format_bundle = systemtools.LilyPondFormatBundle()
     markup = self._to_markup()
     markup = new(markup, direction=Up)
     markup_format_pieces = markup._get_format_pieces()
     lilypond_format_bundle.right.markup.extend(markup_format_pieces)
     return lilypond_format_bundle
Ejemplo n.º 7
0
 def _lilypond_format_bundle(self):
     from abjad.tools import systemtools
     lilypond_format_bundle = systemtools.LilyPondFormatBundle()
     markup = self.markup
     markup = new(markup, direction=Up)
     markup_format_pieces = markup._get_format_pieces()
     lilypond_format_bundle.right.markup.extend(markup_format_pieces)
     return lilypond_format_bundle
Ejemplo n.º 8
0
 def _get_lilypond_format_bundle(self, component=None):
     bundle = LilyPondFormatBundle()
     if self.tweaks:
         tweaks = self.tweaks._list_format_contributions()
         bundle.right.markup.extend(tweaks)
     markup = self.markup
     markup = new(markup, direction=Up)
     markup_format_pieces = markup._get_format_pieces()
     bundle.right.markup.extend(markup_format_pieces)
     return bundle
Ejemplo n.º 9
0
    def __copy__(self, *arguments):
        r"""
        Copies rehearsal mark.

        >>> import copy

        ..  container:: example

            Preserves tweaks:

            >>> mark = abjad.RehearsalMark(number=1)
            >>> abjad.tweak(mark).color = 'red'
            >>> staff = abjad.Staff("c'4 d' e' f'")
            >>> abjad.attach(mark, staff[0])
            >>> abjad.show(staff) # doctest: +SKIP

            ..  docs::

                >>> abjad.f(staff)
                \new Staff
                {
                    \tweak color #red
                    \mark #1
                    c'4
                    d'4
                    e'4
                    f'4
                }

            >>> mark = copy.copy(mark)
            >>> staff = abjad.Staff("c'4 d' e' f'")
            >>> abjad.attach(mark, staff[0])
            >>> abjad.show(staff) # doctest: +SKIP

            ..  docs::

                >>> abjad.f(staff)
                \new Staff
                {
                    \tweak color #red
                    \mark #1
                    c'4
                    d'4
                    e'4
                    f'4
                }

        """
        mark_ = new(self)
        mark_._lilypond_tweak_manager = copy.copy(self.tweaks)
        return mark_
Ejemplo n.º 10
0
 def __illustrate__(
     self,
     default_paper_size: str = None,
     global_staff_size: int = None,
     includes: typing.List[str] = None,
 ) -> LilyPondFile:
     r'''Illustrates score template.
     '''
     score = self()
     for voice in iterate(score).components(Voice):
         voice.append(Skip(1))
     self.attach_defaults(score)
     lilypond_file = score.__illustrate__()
     lilypond_file = new(
         lilypond_file,
         default_paper_size=default_paper_size,
         global_staff_size=global_staff_size,
         includes=includes,
     )
     return lilypond_file
Ejemplo n.º 11
0
 def _get_lilypond_format(self, context=None):
     result = []
     markup = self.markup
     if markup.direction is not None:
         markup = new(
             markup,
             direction=None,
             )
     if isinstance(context, str):
         pass
     elif context is not None:
         context = context.lilypond_type
     else:
         context = self._lilypond_type
     pieces = markup._get_format_pieces()
     result.append(rf'\set {context!s}.instrumentName =')
     result.extend(pieces)
     result.append(rf'\set {context!s}.shortInstrumentName =')
     result.extend(pieces)
     return result
Ejemplo n.º 12
0
    def __invert__(self):
        r'''Inverts pattern.

        ..  container:: example

            **Example.**

            ::

                >>> pattern = patterntools.select_first(3)
                >>> print(format(pattern))
                patterntools.Pattern(
                    indices=(0, 1, 2),
                    )

            ::

                >>> pattern = ~pattern
                >>> print(format(pattern))
                patterntools.Pattern(
                    indices=(0, 1, 2),
                    inverted=True,
                    )

            ::

                >>> pattern = ~pattern
                >>> print(format(pattern))
                patterntools.Pattern(
                    indices=(0, 1, 2),
                    inverted=False,
                    )
                    
            Negation defined equal to inversion.

        Returns new pattern.
        '''
        inverted = not self.inverted
        pattern = new(self, inverted=inverted)
        return pattern
Ejemplo n.º 13
0
    def __invert__(self):
        r'''Inverts pattern.

        ..  container:: example

            **Example.**

            ::

                >>> pattern = patterntools.select_first(3)
                >>> print(format(pattern))
                patterntools.Pattern(
                    indices=(0, 1, 2),
                    )

            ::

                >>> pattern = ~pattern
                >>> print(format(pattern))
                patterntools.Pattern(
                    indices=(0, 1, 2),
                    inverted=True,
                    )

            ::

                >>> pattern = ~pattern
                >>> print(format(pattern))
                patterntools.Pattern(
                    indices=(0, 1, 2),
                    inverted=False,
                    )
                    
            Negation defined equal to inversion.

        Returns new pattern.
        '''
        inverted = not self.inverted
        pattern = new(self, inverted=inverted)
        return pattern
Ejemplo n.º 14
0
 def _get_lilypond_format_bundle(self, component=None):
     bundle = self._get_basic_lilypond_format_bundle(component)
     markup = inspect(component).get_piecewise(self, Markup, None)
     line_segment = inspect(component).get_piecewise(
         self, LineSegment, None)
     if self._should_format_last_leaf_markup(component):
         last_leaf_markup = inspect(self[-1]).get_piecewise(self, Markup)
     else:
         last_leaf_markup = None
     indicators = (markup, line_segment, last_leaf_markup)
     has_indicators = any(_ is not None for _ in indicators)
     if not has_indicators:
         if component is self[0]:
             if self._wrappers:
                 string = r'- \tweak Y-extent ##f'
                 bundle.right.spanner_starts.append(string)
                 line_segment = self._make_invisible_line_segment()
                 tweaks = line_segment._get_lilypond_grob_overrides(
                     tweaks=True)
                 bundle.right.spanner_starts.extend(tweaks)
             bundle.right.spanner_starts.append(self._start_command())
         if component is self[-1]:
             bundle.right.spanner_stops.append(self._stop_command())
         return bundle
     if has_indicators and not component is self[0]:
         bundle.right.spanner_stops.append(self._stop_command())
     if not component is self[-1]:
         if self._wrappers:
             string = r'- \tweak Y-extent ##f'
             bundle.right.spanner_starts.append(string)
         if line_segment is None:
             line_segment = self._make_invisible_line_segment()
         if markup is not None:
             if line_segment.left_hspace is not None:
                 left_hspace = line_segment.left_hspace
                 left_hspace = Markup.hspace(left_hspace)
                 markup = Markup.concat([markup, left_hspace])
             override = LilyPondGrobOverride(
                 grob_name='TextSpanner',
                 once=True,
                 property_path=(
                     'bound-details',
                     'left',
                     'text',
                 ),
                 value=markup,
             )
             string = override.tweak_string
             bundle.right.spanner_starts.append(string)
         tweaks = line_segment._get_lilypond_grob_overrides(tweaks=True)
         bundle.right.spanner_starts.extend(tweaks)
     if last_leaf_markup is not None:
         right_hspace = line_segment.right_padding or 0
         # optical correction to draw last markup left:
         right_hspace -= 0.5
         right_hspace = Markup.hspace(right_hspace)
         last_leaf_markup = Markup.concat(
             [right_hspace, last_leaf_markup], )
         last_leaf_markup = new(last_leaf_markup, direction=None)
         override = LilyPondGrobOverride(
             grob_name='TextSpanner',
             once=True,
             property_path=(
                 'bound-details',
                 'right',
                 'text',
             ),
             value=last_leaf_markup,
         )
         string = override.tweak_string
         bundle.right.spanner_starts.append(string)
     # all tweaks must appear immediately before start command:
     if not component is self[-1]:
         bundle.right.spanner_starts.append(self._start_command())
     return bundle
Ejemplo n.º 15
0
    def advance(self, weight: int) -> 'Talea':
        r'''Advances talea by `weight`.

        ..  container:: example

            >>> talea = abjad.rhythmmakertools.Talea(
            ...     counts=[2, 1, 3, 2, 4, 1, 1],
            ...     denominator=16,
            ...     preamble=[1, 1, 1, 1],
            ...     )

            >>> abjad.f(talea.advance(0))
            abjad.rhythmmakertools.Talea(
                counts=[2, 1, 3, 2, 4, 1, 1],
                denominator=16,
                preamble=[1, 1, 1, 1],
                )

            >>> abjad.f(talea.advance(1))
            abjad.rhythmmakertools.Talea(
                counts=[2, 1, 3, 2, 4, 1, 1],
                denominator=16,
                preamble=[1, 1, 1],
                )

            >>> abjad.f(talea.advance(2))
            abjad.rhythmmakertools.Talea(
                counts=[2, 1, 3, 2, 4, 1, 1],
                denominator=16,
                preamble=[1, 1],
                )

            >>> abjad.f(talea.advance(3))
            abjad.rhythmmakertools.Talea(
                counts=[2, 1, 3, 2, 4, 1, 1],
                denominator=16,
                preamble=[1],
                )

            >>> abjad.f(talea.advance(4))
            abjad.rhythmmakertools.Talea(
                counts=[2, 1, 3, 2, 4, 1, 1],
                denominator=16,
                )

            >>> abjad.f(talea.advance(5))
            abjad.rhythmmakertools.Talea(
                counts=[2, 1, 3, 2, 4, 1, 1],
                denominator=16,
                preamble=[1, 1, 3, 2, 4, 1, 1],
                )

            >>> abjad.f(talea.advance(6))
            abjad.rhythmmakertools.Talea(
                counts=[2, 1, 3, 2, 4, 1, 1],
                denominator=16,
                preamble=[1, 3, 2, 4, 1, 1],
                )

            >>> abjad.f(talea.advance(7))
            abjad.rhythmmakertools.Talea(
                counts=[2, 1, 3, 2, 4, 1, 1],
                denominator=16,
                preamble=[3, 2, 4, 1, 1],
                )

            >>> abjad.f(talea.advance(8))
            abjad.rhythmmakertools.Talea(
                counts=[2, 1, 3, 2, 4, 1, 1],
                denominator=16,
                preamble=[2, 2, 4, 1, 1],
                )

        ..  container:: example

            REGRESSION. Works when talea advances by period of talea:

            >>> talea = abjad.rhythmmakertools.Talea(
            ...     counts=[1, 2, 3, 4],
            ...     denominator=16,
            ...     )

            >>> abjad.f(talea.advance(10))
            abjad.rhythmmakertools.Talea(
                counts=[1, 2, 3, 4],
                denominator=16,
                )

            >>> abjad.f(talea.advance(20))
            abjad.rhythmmakertools.Talea(
                counts=[1, 2, 3, 4],
                denominator=16,
                )

        '''
        assert isinstance(weight, int), repr(weight)
        if weight < 0:
            raise Exception(f'weight {weight} must be nonnegative.')
        if weight == 0:
            return new(self)
        preamble = Sequence(self.preamble or ())
        counts = Sequence(self.counts or ())
        preamble_: typing.Optional[Sequence]
        if weight < preamble.weight():
            consumed, remaining = preamble.split([weight], overhang=True)
            preamble_ = remaining
        elif weight == preamble.weight():
            preamble_ = None
        else:
            assert preamble.weight() < weight
            weight -= preamble.weight()
            preamble = counts[:]
            while True:
                if weight <= preamble.weight():
                    break
                preamble += counts
            if preamble.weight() == weight:
                consumed, remaining = preamble[:], None
            else:
                consumed, remaining = preamble.split([weight], overhang=True)
            preamble_ = remaining
        return new(
            self,
            counts=counts,
            denominator=self.denominator,
            preamble=preamble_,
        )
Ejemplo n.º 16
0
 def _repr_specification(self):
     specification = self._storage_format_specification
     return new(
         specification,
         is_indented=False,
     )
Ejemplo n.º 17
0
    def rotate(self, n=0):
        r'''Rotates compound pattern.

        ..  container:: example

            **Example 1.** Matches every index that is (equal to 0 % 2) AND
            (not one of the last three indices):

            ::

                >>> pattern = patterntools.CompoundPattern(
                ...     [
                ...         patterntools.Pattern(
                ...             indices=[0],
                ...             period=2,
                ...             ),
                ...         patterntools.Pattern(
                ...             indices=[-3, -2, -1],
                ...             inverted=True,
                ...             ),
                ...         ],
                ...     operator='and',
                ...     )

            ::

                >>> print(format(pattern))
                patterntools.CompoundPattern(
                    (
                        patterntools.Pattern(
                            indices=(0,),
                            period=2,
                            ),
                        patterntools.Pattern(
                            indices=(-3, -2, -1),
                            inverted=True,
                            ),
                        ),
                    operator='and',
                    )

            Rotates pattern two elements to the right:

            ::

                >>> pattern = pattern.rotate(n=2)
                >>> print(format(pattern))
                patterntools.CompoundPattern(
                    (
                        patterntools.Pattern(
                            indices=(2,),
                            period=2,
                            ),
                        patterntools.Pattern(
                            indices=(-1, 0, 1),
                            inverted=True,
                            ),
                        ),
                    operator='and',
                    )
            
            New pattern matches every index that is (equal to 2 % 2) AND (not
            the first, second or last index in the pattern).

        Returns new compound pattern.
        '''
        patterns = [_.rotate(n=n) for _ in self]
        return new(self, items=patterns)
Ejemplo n.º 18
0
    def __invert__(self):
        r'''Inverts pattern.

        ..  container:: example

            **Example 1.** Matches every index that is (one of the first three
            indices) or (one of the last three indices):

            ::

                >>> pattern_1 = patterntools.select_first(3)
                >>> pattern_2 = patterntools.select_last(3)
                >>> pattern = pattern_1 | pattern_2

            ::

                >>> print(format(pattern))
                patterntools.CompoundPattern(
                    (
                        patterntools.Pattern(
                            indices=(0, 1, 2),
                            ),
                        patterntools.Pattern(
                            indices=(-3, -2, -1),
                            ),
                        ),
                    operator='or',
                    )

            ::

                >>> pattern.get_boolean_vector(total_length=16)
                [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1]

        ..  container:: example

            **Example 2.** Matches every index that is NOT (one of the first
            three indices) or (one of the last three indices):

            ::

                >>> pattern = ~pattern
                >>> print(format(pattern))
                patterntools.CompoundPattern(
                    (
                        patterntools.Pattern(
                            indices=(0, 1, 2),
                            ),
                        patterntools.Pattern(
                            indices=(-3, -2, -1),
                            ),
                        ),
                    inverted=True,
                    operator='or',
                    )

            ::

                >>> pattern.get_boolean_vector(total_length=16)
                [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0]

        Returns new compound pattern.
        '''
        inverted = not self.inverted
        pattern = new(self, inverted=inverted)
        return pattern
Ejemplo n.º 19
0
    def reverse(self):
        r'''Reverses pattern.

        ..  container:: example

            **Example 1.** Matches three indices out of every eight:

            ::

                >>> pattern = patterntools.Pattern(
                ...     indices=[0, 1, 7],
                ...     period=8,
                ...     )

            ::

                >>> total_length = 16
                >>> for index in range(16):
                ...     match = pattern.matches_index(index, total_length)
                ...     match = match or ''
                ...     print(index, match)
                0 True
                1 True
                2 
                3 
                4 
                5 
                6 
                7 True
                8 True
                9 True
                10 
                11 
                12 
                13 
                14 
                15 True

        ..  container:: example

            **Example 2.** Reverses pattern:

            ::

                >>> pattern = patterntools.Pattern(
                ...     indices=[0, 1, 7],
                ...     period=8,
                ...     )

            ::

                >>> pattern = pattern.reverse()
                >>> print(format(pattern))
                patterntools.Pattern(
                    indices=(-1, -2, -8),
                    period=8,
                    )

            ::

                >>> total_length = 16
                >>> for index in range(16):
                ...     match = pattern.matches_index(index, total_length)
                ...     match = match or ''
                ...     print(index, match)
                0 True
                1 
                2 
                3 
                4 
                5 
                6 True
                7 True
                8 True
                9 
                10 
                11 
                12 
                13 
                14 True
                15 True

        Returns new pattern.
        '''
        indices = [-index - 1 for index in self.indices]
        return new(self, indices=indices)
Ejemplo n.º 20
0
    def reverse(self):
        r'''Reverses compound pattern.

        ..  container:: example

            **Example 1.** Matches every index that is (equal to 0 % 2) AND
            (not one of the last three indices):

            ::

                >>> pattern = patterntools.CompoundPattern(
                ...     [
                ...         patterntools.Pattern(
                ...             indices=[0],
                ...             period=2,
                ...             ),
                ...         patterntools.Pattern(
                ...             indices=[-3, -2, -1],
                ...             inverted=True,
                ...             ),
                ...         ],
                ...     operator='and',
                ...     )

            ::

                >>> print(format(pattern))
                patterntools.CompoundPattern(
                    (
                        patterntools.Pattern(
                            indices=(0,),
                            period=2,
                            ),
                        patterntools.Pattern(
                            indices=(-3, -2, -1),
                            inverted=True,
                            ),
                        ),
                    operator='and',
                    )

            Reverses pattern:

            ::

                >>> pattern = pattern.reverse()
                >>> print(format(pattern))
                patterntools.CompoundPattern(
                    (
                        patterntools.Pattern(
                            indices=(-1,),
                            period=2,
                            ),
                        patterntools.Pattern(
                            indices=(2, 1, 0),
                            inverted=True,
                            ),
                        ),
                    operator='and',
                    )

            New pattern matches every index that is (equal to -1 % 2) AND
            (not one of the first three indices).

        Returns new compound pattern.
        '''
        patterns = [_.reverse() for _ in self]
        return new(self, items=patterns)
Ejemplo n.º 21
0
    def rotate(self, n=0):
        r'''Rotates pattern.

        ..  container:: example

            **Example 1.** Matches three indices out of every eight:

            ::

                >>> pattern = patterntools.Pattern(
                ...     indices=[0, 1, 7],
                ...     period=8,
                ...     )

            ::

                >>> total_length = 16
                >>> for index in range(16):
                ...     match = pattern.matches_index(index, total_length)
                ...     match = match or ''
                ...     print(index, match)
                0 True
                1 True
                2 
                3 
                4 
                5 
                6 
                7 True
                8 True
                9 True
                10 
                11 
                12 
                13 
                14 
                15 True

            Rotates pattern two elements to the right:

            ::

                >>> pattern = patterntools.Pattern(
                ...     indices=[0, 1, 7],
                ...     period=8,
                ...     )

            ::

                >>> pattern = pattern.rotate(n=2)
                >>> print(format(pattern))
                patterntools.Pattern(
                    indices=(2, 3, 9),
                    period=8,
                    )

            ::

                >>> total_length = 16
                >>> for index in range(16):
                ...     match = pattern.matches_index(index, total_length)
                ...     match = match or ''
                ...     print(index, match)
                0 
                1 True
                2 True
                3 True
                4 
                5 
                6 
                7 
                8 
                9 True
                10 True
                11 True
                12 
                13 
                14 
                15 

        ..  container:: example

            **Example 2.** Matches three indices out of every eight with
            negative indices:

            ::

                >>> pattern = patterntools.Pattern(
                ...     indices=[-3, -2, -1],
                ...     period=8,
                ...     )

            ::

                >>> total_length = 16
                >>> for index in range(16):
                ...     match = pattern.matches_index(index, total_length)
                ...     match = match or ''
                ...     print(index, match)
                0 
                1 
                2 
                3 
                4 
                5 True
                6 True
                7 True
                8 
                9 
                10 
                11 
                12 
                13 True
                14 True
                15 True

            Rotates pattern two elements to the right:

            ::

                >>> pattern = patterntools.Pattern(
                ...     indices=[-3, -2, -1],
                ...     period=8,
                ...     )

            ::

                >>> pattern = pattern.rotate(n=2)
                >>> print(format(pattern))
                patterntools.Pattern(
                    indices=(-1, 0, 1),
                    period=8,
                    )

            ::

                >>> total_length = 16
                >>> for index in range(16):
                ...     match = pattern.matches_index(index, total_length)
                ...     match = match or ''
                ...     print(index, match)
                0 True
                1 True
                2 
                3 
                4 
                5 
                6 
                7 True
                8 True
                9 True
                10 
                11 
                12 
                13 
                14 
                15 True

        Returns new pattern.
        '''
        indices = [index + n for index in self.indices]
        return new(self, indices=indices)
Ejemplo n.º 22
0
    def rotate(self, n=0):
        r'''Rotates pattern.

        ..  container:: example

            **Example 1.** Matches three indices out of every eight:

            ::

                >>> pattern = patterntools.Pattern(
                ...     indices=[0, 1, 7],
                ...     period=8,
                ...     )

            ::

                >>> total_length = 16
                >>> for index in range(16):
                ...     match = pattern.matches_index(index, total_length)
                ...     match = match or ''
                ...     print(index, match)
                0 True
                1 True
                2 
                3 
                4 
                5 
                6 
                7 True
                8 True
                9 True
                10 
                11 
                12 
                13 
                14 
                15 True

            Rotates pattern two elements to the right:

            ::

                >>> pattern = patterntools.Pattern(
                ...     indices=[0, 1, 7],
                ...     period=8,
                ...     )

            ::

                >>> pattern = pattern.rotate(n=2)
                >>> print(format(pattern))
                patterntools.Pattern(
                    indices=(2, 3, 9),
                    period=8,
                    )

            ::

                >>> total_length = 16
                >>> for index in range(16):
                ...     match = pattern.matches_index(index, total_length)
                ...     match = match or ''
                ...     print(index, match)
                0 
                1 True
                2 True
                3 True
                4 
                5 
                6 
                7 
                8 
                9 True
                10 True
                11 True
                12 
                13 
                14 
                15 

        ..  container:: example

            **Example 2.** Matches three indices out of every eight with
            negative indices:

            ::

                >>> pattern = patterntools.Pattern(
                ...     indices=[-3, -2, -1],
                ...     period=8,
                ...     )

            ::

                >>> total_length = 16
                >>> for index in range(16):
                ...     match = pattern.matches_index(index, total_length)
                ...     match = match or ''
                ...     print(index, match)
                0 
                1 
                2 
                3 
                4 
                5 True
                6 True
                7 True
                8 
                9 
                10 
                11 
                12 
                13 True
                14 True
                15 True

            Rotates pattern two elements to the right:

            ::

                >>> pattern = patterntools.Pattern(
                ...     indices=[-3, -2, -1],
                ...     period=8,
                ...     )

            ::

                >>> pattern = pattern.rotate(n=2)
                >>> print(format(pattern))
                patterntools.Pattern(
                    indices=(-1, 0, 1),
                    period=8,
                    )

            ::

                >>> total_length = 16
                >>> for index in range(16):
                ...     match = pattern.matches_index(index, total_length)
                ...     match = match or ''
                ...     print(index, match)
                0 True
                1 True
                2 
                3 
                4 
                5 
                6 
                7 True
                8 True
                9 True
                10 
                11 
                12 
                13 
                14 
                15 True

        Returns new pattern.
        '''
        indices = [index + n for index in self.indices]
        return new(self, indices=indices)
Ejemplo n.º 23
0
    def __invert__(self):
        r'''Inverts pattern.

        ..  container:: example

            **Example 1.** Matches every index that is (one of the first three
            indices) or (one of the last three indices):

            ::

                >>> pattern_1 = patterntools.select_first(3)
                >>> pattern_2 = patterntools.select_last(3)
                >>> pattern = pattern_1 | pattern_2

            ::

                >>> print(format(pattern))
                patterntools.CompoundPattern(
                    (
                        patterntools.Pattern(
                            indices=(0, 1, 2),
                            ),
                        patterntools.Pattern(
                            indices=(-3, -2, -1),
                            ),
                        ),
                    operator='or',
                    )

            ::

                >>> pattern.get_boolean_vector(total_length=16)
                [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1]

        ..  container:: example

            **Example 2.** Matches every index that is NOT (one of the first
            three indices) or (one of the last three indices):

            ::

                >>> pattern = ~pattern
                >>> print(format(pattern))
                patterntools.CompoundPattern(
                    (
                        patterntools.Pattern(
                            indices=(0, 1, 2),
                            ),
                        patterntools.Pattern(
                            indices=(-3, -2, -1),
                            ),
                        ),
                    inverted=True,
                    operator='or',
                    )

            ::

                >>> pattern.get_boolean_vector(total_length=16)
                [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0]

        Returns new compound pattern.
        '''
        inverted = not self.inverted
        pattern = new(self, inverted=inverted)
        return pattern
Ejemplo n.º 24
0
    def reverse(self):
        r'''Reverses compound pattern.

        ..  container:: example

            **Example 1.** Matches every index that is (equal to 0 % 2) AND
            (not one of the last three indices):

            ::

                >>> pattern = patterntools.CompoundPattern(
                ...     [
                ...         patterntools.Pattern(
                ...             indices=[0],
                ...             period=2,
                ...             ),
                ...         patterntools.Pattern(
                ...             indices=[-3, -2, -1],
                ...             inverted=True,
                ...             ),
                ...         ],
                ...     operator='and',
                ...     )

            ::

                >>> print(format(pattern))
                patterntools.CompoundPattern(
                    (
                        patterntools.Pattern(
                            indices=(0,),
                            period=2,
                            ),
                        patterntools.Pattern(
                            indices=(-3, -2, -1),
                            inverted=True,
                            ),
                        ),
                    operator='and',
                    )

            Reverses pattern:

            ::

                >>> pattern = pattern.reverse()
                >>> print(format(pattern))
                patterntools.CompoundPattern(
                    (
                        patterntools.Pattern(
                            indices=(-1,),
                            period=2,
                            ),
                        patterntools.Pattern(
                            indices=(2, 1, 0),
                            inverted=True,
                            ),
                        ),
                    operator='and',
                    )

            New pattern matches every index that is (equal to -1 % 2) AND
            (not one of the first three indices).

        Returns new compound pattern.
        '''
        patterns = [_.reverse() for _ in self]
        return new(self, items=patterns)
Ejemplo n.º 25
0
    def rotate(self, n=0):
        r'''Rotates compound pattern.

        ..  container:: example

            **Example 1.** Matches every index that is (equal to 0 % 2) AND
            (not one of the last three indices):

            ::

                >>> pattern = patterntools.CompoundPattern(
                ...     [
                ...         patterntools.Pattern(
                ...             indices=[0],
                ...             period=2,
                ...             ),
                ...         patterntools.Pattern(
                ...             indices=[-3, -2, -1],
                ...             inverted=True,
                ...             ),
                ...         ],
                ...     operator='and',
                ...     )

            ::

                >>> print(format(pattern))
                patterntools.CompoundPattern(
                    (
                        patterntools.Pattern(
                            indices=(0,),
                            period=2,
                            ),
                        patterntools.Pattern(
                            indices=(-3, -2, -1),
                            inverted=True,
                            ),
                        ),
                    operator='and',
                    )

            Rotates pattern two elements to the right:

            ::

                >>> pattern = pattern.rotate(n=2)
                >>> print(format(pattern))
                patterntools.CompoundPattern(
                    (
                        patterntools.Pattern(
                            indices=(2,),
                            period=2,
                            ),
                        patterntools.Pattern(
                            indices=(-1, 0, 1),
                            inverted=True,
                            ),
                        ),
                    operator='and',
                    )

            New pattern matches every index that is (equal to 2 % 2) AND (not
            the first, second or last index in the pattern).

        Returns new compound pattern.
        '''
        patterns = [_.rotate(n=n) for _ in self]
        return new(self, items=patterns)
Ejemplo n.º 26
0
    def reverse(self):
        r'''Reverses pattern.

        ..  container:: example

            **Example 1.** Matches three indices out of every eight:

            ::

                >>> pattern = patterntools.Pattern(
                ...     indices=[0, 1, 7],
                ...     period=8,
                ...     )

            ::

                >>> total_length = 16
                >>> for index in range(16):
                ...     match = pattern.matches_index(index, total_length)
                ...     match = match or ''
                ...     print(index, match)
                0 True
                1 True
                2 
                3 
                4 
                5 
                6 
                7 True
                8 True
                9 True
                10 
                11 
                12 
                13 
                14 
                15 True

        ..  container:: example

            **Example 2.** Reverses pattern:

            ::

                >>> pattern = patterntools.Pattern(
                ...     indices=[0, 1, 7],
                ...     period=8,
                ...     )

            ::

                >>> pattern = pattern.reverse()
                >>> print(format(pattern))
                patterntools.Pattern(
                    indices=(-1, -2, -8),
                    period=8,
                    )

            ::

                >>> total_length = 16
                >>> for index in range(16):
                ...     match = pattern.matches_index(index, total_length)
                ...     match = match or ''
                ...     print(index, match)
                0 True
                1 
                2 
                3 
                4 
                5 
                6 True
                7 True
                8 True
                9 
                10 
                11 
                12 
                13 
                14 True
                15 True

        Returns new pattern.
        '''
        indices = [-index - 1 for index in self.indices]
        return new(self, indices=indices)
Ejemplo n.º 27
0
 def __radd__(self, argument):
     r'''Right-adds `argument` to typed tuple.
     '''
     items = argument + self._collection
     return new(self, items=items)
Ejemplo n.º 28
0
 def __radd__(self, argument):
     """
     Right-adds ``argument`` to typed tuple.
     """
     items = argument + self._collection
     return new(self, items=items)
Ejemplo n.º 29
0
 def _repr_specification(self):
     specification = self._storage_format_specification
     return new(
         specification,
         is_indented=False,
         )