Pattern

digraph InheritanceGraph { graph [bgcolor=transparent, color=lightsteelblue2, fontname=Arial, fontsize=10, outputorder=edgesfirst, overlap=prism, penwidth=2, rankdir=LR, splines=spline, style="dashed, rounded", truecolor=true]; node [colorscheme=pastel19, fontname=Arial, fontsize=10, height=0, penwidth=2, shape=box, style="filled, rounded", width=0]; edge [color=lightslategrey, penwidth=1]; subgraph "cluster_abjad.system" { graph [label="abjad.system"]; node [color=1]; "abjad.system.AbjadObject.AbjadObject" [URL="../api/abjad/utilities/../system/AbjadObject.html#abjad.system.AbjadObject.AbjadObject", label="Abjad\nObject", target=_top]; "abjad.system.AbjadValueObject.AbjadValueObject" [URL="../api/abjad/utilities/../system/AbjadValueObject.html#abjad.system.AbjadValueObject.AbjadValueObject", label="Abjad\nValue\nObject", target=_top]; "abjad.system.AbjadObject.AbjadObject" -> "abjad.system.AbjadValueObject.AbjadValueObject"; } subgraph "cluster_abjad.utilities" { graph [label="abjad.utilities"]; node [color=2]; "abjad.utilities.Pattern.Pattern" [URL="../api/abjad/utilities/Pattern.html#abjad.utilities.Pattern.Pattern", color=black, fontcolor=white, label=Pattern, target=_top]; } subgraph cluster_builtins { graph [label=builtins]; node [color=3]; "builtins.object" [URL="https://docs.python.org/3.6/library/functions.html#object", label=object, target=_top]; } "abjad.system.AbjadValueObject.AbjadValueObject" -> "abjad.utilities.Pattern.Pattern"; "builtins.object" -> "abjad.system.AbjadObject.AbjadObject"; }

class abjad.utilities.Pattern.Pattern(indices=None, inverted=None, operator=None, patterns=None, payload=None, period=None, template=None)

Pattern.

Matches three indices out of every eight:

>>> pattern = abjad.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

Matches three indices out of every sixteen:

>>> pattern = abjad.Pattern(
...     indices=[0, 1, 7],
...     period=16,
...     )
>>> 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
9
10
11
12
13
14
15

Works with improper indices:

>>> pattern = abjad.Pattern(
...     indices=[16, 17, 23],
...     period=16,
...     )
>>> 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
9
10
11
12
13
14
15

Sieve from opening of Xenakis’s Psappha:

>>> sieve_1a = abjad.index([0, 1, 7], 8)
>>> sieve_1b = abjad.index([1, 3], 5)
>>> sieve_1 = sieve_1a & sieve_1b
>>> sieve_2a = abjad.index([0, 1, 2], 8)
>>> sieve_2b = abjad.index([0], 5)
>>> sieve_2 = sieve_2a & sieve_2b
>>> sieve_3 = abjad.index([3], 8)
>>> sieve_4 = abjad.index([4], 8)
>>> sieve_5a = abjad.index([5, 6], 8)
>>> sieve_5b = abjad.index([2, 3, 4], 5)
>>> sieve_5 = sieve_5a & sieve_5b
>>> sieve_6a = abjad.index([1], 8)
>>> sieve_6b = abjad.index([2], 5)
>>> sieve_6 = sieve_6a & sieve_6b
>>> sieve_7a = abjad.index([6], 8)
>>> sieve_7b = abjad.index([1], 5)
>>> sieve_7 = sieve_7a & sieve_7b
>>> sieve = sieve_1 | sieve_2 | sieve_3 | sieve_4 | sieve_5 | sieve_6 | sieve_7
>>> sieve.get_boolean_vector(total_length=40)
[1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1,
1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0]

Attributes Summary

__and__ Logical AND of two patterns.
__invert__ Inverts pattern.
__len__ Gets length of pattern.
__or__ Logical OR of two patterns.
__xor__ Logical XOR of two patterns.
from_vector Makes pattern from boolean vector.
get_boolean_vector Gets boolean vector of pattern applied to input sequence with total_length.
get_matching_items Gets maching items from sequence.
index Makes pattern that matches indices.
index_all Makes pattern that matches all indices.
index_first Makes pattern that matches the first n indices.
index_last Makes pattern that matches the last n indices.
indices Gets indices of pattern.
inverted Is true when pattern is inverted.
matches_index Is true when pattern matches index taken under total_length.
operator Gets operator of pattern.
patterns Gets paterns of pattern.
payload Gets payload of pattern.
period Gets period of pattern.
reverse Reverses pattern.
rotate Rotates pattern by index n.
template Get pattern template.
weight Gets weight of pattern.

Special methods

__and__(pattern)

Logical AND of two patterns.

Flat grouping of two patterns:

>>> pattern_1 = abjad.index_first(3)
>>> pattern_2 = abjad.index_last(3)
>>> pattern = pattern_1 & pattern_2
>>> abjad.f(pattern)
abjad.Pattern(
    operator='and',
    patterns=(
        abjad.index_first(3),
        abjad.index_last(3),
        ),
    )

Flat grouping of three patterns:

>>> pattern_1 = abjad.index_first(3)
>>> pattern_2 = abjad.index_last(3)
>>> pattern_3 = abjad.index([0], 2)
>>> pattern = pattern_1 & pattern_2 & pattern_3
>>> abjad.f(pattern)
abjad.Pattern(
    operator='and',
    patterns=(
        abjad.index_first(3),
        abjad.index_last(3),
        abjad.index([0], period=2),
        ),
    )
>>> pattern.get_boolean_vector(total_length=16)
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Nested grouping of three patterns:

>>> pattern_1 = abjad.index_first(3)
>>> pattern_2 = abjad.index_last(3)
>>> pattern_3 = abjad.index([0], 2)
>>> pattern = pattern_1 & pattern_2 | pattern_3
>>> abjad.f(pattern)
abjad.Pattern(
    operator='or',
    patterns=(
        abjad.Pattern(
            operator='and',
            patterns=(
                abjad.index_first(3),
                abjad.index_last(3),
                ),
            ),
        abjad.index([0], period=2),
        ),
    )
>>> pattern.get_boolean_vector(total_length=16)
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0]

In-place AND is allowed:

>>> pattern = abjad.index_first(3)
>>> pattern &= abjad.index_last(3)
>>> abjad.f(pattern)
abjad.Pattern(
    operator='and',
    patterns=(
        abjad.index_first(3),
        abjad.index_last(3),
        ),
    )

Returns new pattern.

(AbjadValueObject).__copy__(*arguments)

Copies Abjad value object.

Returns new Abjad value object.

(AbjadValueObject).__eq__(argument)

Is true when all initialization values of Abjad value object equal the initialization values of argument.

Returns true or false.

(AbjadObject).__format__(format_specification='')

Formats Abjad object.

Set format_specification to '' or 'storage'. Interprets '' equal to 'storage'.

Returns string.

(AbjadValueObject).__hash__()

Hashes Abjad value object.

Returns integer.

__invert__()

Inverts pattern.

>>> pattern = abjad.index_first(3)
>>> abjad.f(pattern)
abjad.index_first(3)
>>> pattern = ~pattern
>>> abjad.f(pattern)
abjad.Pattern(
    indices=[0, 1, 2],
    inverted=True,
    )
>>> pattern = ~pattern
>>> abjad.f(pattern)
abjad.Pattern(
    indices=[0, 1, 2],
    inverted=False,
    )

Negation defined equal to inversion.

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

>>> pattern_1 = abjad.index_first(3)
>>> pattern_2 = abjad.index_last(3)
>>> pattern = pattern_1 | pattern_2
>>> abjad.f(pattern)
abjad.Pattern(
    operator='or',
    patterns=(
        abjad.index_first(3),
        abjad.index_last(3),
        ),
    )
>>> pattern.get_boolean_vector(total_length=16)
[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1]

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

>>> pattern = ~pattern
>>> abjad.f(pattern)
abjad.Pattern(
    inverted=True,
    operator='or',
    patterns=(
        abjad.index_first(3),
        abjad.index_last(3),
        ),
    )
>>> 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 pattern.

__len__()

Gets length of pattern.

Gets length of cyclic pattern:

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

Length of cyclic pattern defined equal to period of the pattern.

Gets length of acyclic pattern:

>>> pattern = abjad.Pattern(
...     indices=[0, 2, 3],
...     )
>>> len(pattern)
4

Length of acyclic pattern defined equal to greatest index in pattern, plus 1.

Gets length of pattern with negative indices:

>>> pattern = abjad.Pattern(
...     indices=[-3],
...     )
>>> len(pattern)
3

Length of pattern with negative indices defined equal to absolute value of least index.

Returns nonnegative integer.

__or__(pattern)

Logical OR of two patterns.

Flat grouping of two patterns:

>>> pattern_1 = abjad.index_first(3)
>>> pattern_2 = abjad.index_last(3)
>>> pattern = pattern_1 | pattern_2
>>> abjad.f(pattern)
abjad.Pattern(
    operator='or',
    patterns=(
        abjad.index_first(3),
        abjad.index_last(3),
        ),
    )

Flat grouping:

>>> pattern_1 = abjad.index_first(3)
>>> pattern_2 = abjad.index_last(3)
>>> pattern_3 = abjad.index([0], 2)
>>> pattern = pattern_1 | pattern_2 | pattern_3
>>> abjad.f(pattern)
abjad.Pattern(
    operator='or',
    patterns=(
        abjad.index_first(3),
        abjad.index_last(3),
        abjad.index([0], period=2),
        ),
    )
>>> pattern.get_boolean_vector(total_length=16)
[1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1]

Nested grouping:

>>> pattern_1 = abjad.index_first(3)
>>> pattern_2 = abjad.index_last(3)
>>> pattern_3 = abjad.index([0], 2)
>>> pattern = pattern_1 | pattern_2 & pattern_3
>>> abjad.f(pattern)
abjad.Pattern(
    operator='or',
    patterns=(
        abjad.index_first(3),
        abjad.Pattern(
            operator='and',
            patterns=(
                abjad.index_last(3),
                abjad.index([0], period=2),
                ),
            ),
        ),
    )
>>> pattern.get_boolean_vector(total_length=16)
[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]

In-place OR is allowed:

>>> pattern = abjad.index_first(3)
>>> pattern |= abjad.index_last(3)
>>> abjad.f(pattern)
abjad.Pattern(
    operator='or',
    patterns=(
        abjad.index_first(3),
        abjad.index_last(3),
        ),
    )

Returns new pattern.

(AbjadObject).__repr__()

Gets interpreter representation of Abjad object.

Returns string.

__xor__(pattern)

Logical XOR of two patterns.

>>> pattern_1 = abjad.index_first(3)
>>> pattern_2 = abjad.index_last(3)
>>> pattern = pattern_1 ^ pattern_2
>>> abjad.f(pattern)
abjad.Pattern(
    operator='xor',
    patterns=(
        abjad.index_first(3),
        abjad.index_last(3),
        ),
    )

Flat grouping:

>>> pattern_1 = abjad.index_first(3)
>>> pattern_2 = abjad.index_last(3)
>>> pattern_3 = abjad.index([0], 2)
>>> pattern = pattern_1 ^ pattern_2 ^ pattern_3
>>> abjad.f(pattern)
abjad.Pattern(
    operator='xor',
    patterns=(
        abjad.index_first(3),
        abjad.index_last(3),
        abjad.index([0], period=2),
        ),
    )
>>> pattern.get_boolean_vector(total_length=16)
[0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1]

Nested grouping:

>>> pattern_1 = abjad.index_first(3)
>>> pattern_2 = abjad.index_last(3)
>>> pattern_3 = abjad.index([0], 2)
>>> pattern = pattern_1 ^ pattern_2 & pattern_3
>>> abjad.f(pattern)
abjad.Pattern(
    operator='xor',
    patterns=(
        abjad.index_first(3),
        abjad.Pattern(
            operator='and',
            patterns=(
                abjad.index_last(3),
                abjad.index([0], period=2),
                ),
            ),
        ),
    )
>>> pattern.get_boolean_vector(total_length=16)
[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]

In-place XOR is allowed:

>>> pattern = abjad.index_first(3)
>>> pattern ^= abjad.index_last(3)
>>> abjad.f(pattern)
abjad.Pattern(
    operator='xor',
    patterns=(
        abjad.index_first(3),
        abjad.index_last(3),
        ),
    )

Returns new pattern.


Methods

get_boolean_vector(total_length=None)

Gets boolean vector of pattern applied to input sequence with total_length.

Gets boolean vector of acyclic pattern:

>>> pattern = abjad.Pattern(
...     indices=[4, 5, 6, 7],
...     )
>>> pattern.get_boolean_vector(4)
[0, 0, 0, 0]
>>> pattern.get_boolean_vector(8)
[0, 0, 0, 0, 1, 1, 1, 1]
>>> pattern.get_boolean_vector(16)
[0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]

Sets total length to length of pattern when total_length is none:

>>> pattern.get_boolean_vector()
[0, 0, 0, 0, 1, 1, 1, 1]

Gets vector of cyclic pattern:

>>> pattern = abjad.Pattern(
...     indices=[4, 5, 6, 7],
...     period=20,
...     )
>>> pattern.get_boolean_vector(4)
[0, 0, 0, 0]
>>> pattern.get_boolean_vector(8)
[0, 0, 0, 0, 1, 1, 1, 1]
>>> pattern.get_boolean_vector(16)
[0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]

Sets total length to length of pattern when total_length is none:

>>> pattern.get_boolean_vector()
[0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Gets vector of inverted pattern:

>>> pattern = abjad.Pattern(
...     indices=[4, 5, 6, 7],
...     period=20,
...     )
>>> pattern.get_boolean_vector(4)
[0, 0, 0, 0]
>>> pattern.get_boolean_vector(8)
[0, 0, 0, 0, 1, 1, 1, 1]
>>> pattern.get_boolean_vector(16)
[0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]

Sets total length to length of pattern when total_length is none:

>>> pattern.get_boolean_vector()
[0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Two-part pattern with logical OR:

>>> pattern = abjad.Pattern(
...     operator='or',
...     patterns=[
...         abjad.Pattern(
...             indices=[0, 1, 2],
...             ),
...         abjad.Pattern(
...             indices=[-3, -2, -1],
...             ),
...         ],
...     )
>>> pattern.get_boolean_vector(4)
[1, 1, 1, 1]
>>> pattern.get_boolean_vector(8)
[1, 1, 1, 0, 0, 1, 1, 1]
>>> pattern.get_boolean_vector(16)
[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1]

Matches every index that is (one of the first three indices) OR (one of the last three indices).

Two-part pattern with mixed periodic and inverted parts:

>>> pattern = abjad.Pattern(
...     operator='and',
...     patterns=[
...         abjad.Pattern(
...             indices=[0],
...             period=2,
...             ),
...         abjad.Pattern(
...             indices=[-3, -2, -1],
...             inverted=True,
...             ),
...         ],
...     )
>>> pattern.get_boolean_vector(4)
[1, 0, 0, 0]
>>> pattern.get_boolean_vector(8)
[1, 0, 1, 0, 1, 0, 0, 0]
>>> pattern.get_boolean_vector(16)
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0]

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

Cyclic pattern that indexes every fourth and fifth item:

>>> pattern_1 = abjad.Pattern([0], period=4)
>>> pattern_2 = abjad.Pattern([0], period=5)
>>> pattern = pattern_1 | pattern_2
>>> abjad.f(pattern)
abjad.Pattern(
    operator='or',
    patterns=(
        abjad.Pattern(
            indices=[0],
            period=4,
            ),
        abjad.Pattern(
            indices=[0],
            period=5,
            ),
        ),
    period=20,
    )
>>> pattern.get_boolean_vector(4)
[1, 0, 0, 0]
>>> pattern.get_boolean_vector(8)
[1, 0, 0, 0, 1, 1, 0, 0]
>>> pattern.get_boolean_vector(16)
[1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1]

Sets total length to period of pattern when total_length is none:

>>> pattern.period
20
>>> pattern.get_boolean_vector()
[1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0]
>>> pattern.period == len(pattern.get_boolean_vector())
True

Returns list of ones and zeroes.

get_matching_items(sequence)

Gets maching items from sequence.

>>> pattern = abjad.Pattern(
...     indices=[4, 5, 6, 7],
...     )
>>> pattern.get_matching_items('abcdefghijklmnopqrstuvwxyz')
Sequence(['e', 'f', 'g', 'h'])
>>> pattern = abjad.Pattern(
...     indices=[8, 9],
...     period=10,
...     )
>>> pattern.get_matching_items('abcdefghijklmnopqrstuvwxyz')
Sequence(['i', 'j', 's', 't'])
>>> pattern = abjad.index_first(1) | abjad.index_last(2)
>>> pattern.get_matching_items('abcdefghijklmnopqrstuvwxyz')
Sequence(['a', 'y', 'z'])

Returns new sequence.

matches_index(index, total_length, rotation=None)

Is true when pattern matches index taken under total_length.

Matches three indices out of every eight:

>>> pattern = abjad.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

Matches three indices out of every eight, offset 1 to the left:

>>> pattern = abjad.Pattern(
...     indices=[0, 1, 7],
...     period=8,
...     )
>>> total_length = 16
>>> for index in range(16):
...     match = pattern.matches_index(
...         index,
...         total_length,
...         rotation=1,
...         )
...     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

Matches three indices out of every eight, offset 2 to the left:

>>> pattern = abjad.Pattern(
...     indices=[0, 1, 7],
...     period=8,
...     )
>>> total_length = 16
>>> for index in range(16):
...     match = pattern.matches_index(
...         index,
...         total_length,
...         rotation=2,
...         )
...     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

Matches three indices out of every sixteen:

>>> pattern = abjad.Pattern(
...     indices=[0, 1, 7],
...     period=16,
...     )
>>> 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
9
10
11
12
13
14
15

Matches three indices out of every sixteen, offset 1 to the left:

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

Matches three indices out of every sixteen, offset 2 to the left:

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

Empty pattern:

>>> pattern = abjad.Pattern()

Total length 16:

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

Total length 8:

>>> total_length = 8
>>> for index in range(total_length):
...     match = pattern.matches_index(index, total_length)
...     match = match or ''
...     print(index, match)
0
1
2
3
4
5
6
7

Total length 4:

>>> total_length = 4
>>> for index in range(total_length):
...     match = pattern.matches_index(index, total_length)
...     match = match or ''
...     print(index, match)
0
1
2
3

Matches nothing.

Simple pattern:

Logical OR:

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

Logical AND:

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

Logical XOR:

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

Matches every index that is (one of the first three indices).

Ignores operator.

Two-part pattern with logical OR:

>>> pattern = abjad.Pattern(
...     operator='or',
...     patterns=[
...         abjad.Pattern(
...             indices=[0, 1, 2],
...             ),
...         abjad.Pattern(
...             indices=[-3, -2, -1],
...             ),
...         ],
...     )

Total length 16:

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

Total length 8:

>>> total_length = 8
>>> for index in range(total_length):
...     match = pattern.matches_index(index, total_length)
...     match = match or ''
...     print(index, match)
0 True
1 True
2 True
3
4
5 True
6 True
7 True

Total length 4:

>>> total_length = 4
>>> for index in range(total_length):
...     match = pattern.matches_index(index, total_length)
...     match = match or ''
...     print(index, match)
0 True
1 True
2 True
3 True

Matches every index that is (one of the first three indices) OR (one of the last three indices).

Two-part pattern with logical AND:

>>> pattern = abjad.Pattern(
...     operator='and',
...     patterns=[
...         abjad.Pattern(
...             indices=[0, 1, 2],
...             ),
...         abjad.Pattern(
...             indices=[-3, -2, -1],
...             ),
...         ],
...     )

Total length 16:

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

Total length 8:

>>> total_length = 8
>>> for index in range(total_length):
...     match = pattern.matches_index(index, total_length)
...     match = match or ''
...     print(index, match)
0
1
2
3
4
5
6
7

Total length 4:

>>> total_length = 4
>>> for index in range(total_length):
...     match = pattern.matches_index(index, total_length)
...     match = match or ''
...     print(index, match)
0
1 True
2 True
3

Matches every index that is (one of the first three indices) AND (one of the last three indices).

Two-part pattern with logical XOR:

>>> pattern = abjad.Pattern(
...     operator='xor',
...     patterns=[
...         abjad.Pattern(
...             indices=[0, 1, 2],
...             ),
...         abjad.Pattern(
...             indices=[-3, -2, -1],
...             ),
...         ],
...     )

Total length 16:

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

Total length 8:

>>> total_length = 8
>>> for index in range(total_length):
...     match = pattern.matches_index(index, total_length)
...     match = match or ''
...     print(index, match)
0 True
1 True
2 True
3
4
5 True
6 True
7 True

Total length 4:

>>> total_length = 4
>>> for index in range(total_length):
...     match = pattern.matches_index(index, total_length)
...     match = match or ''
...     print(index, match)
0 True
1
2
3 True

Matches every index that is (one of the first three indices) XOR (one of the last three indices).

Two-part pattern with mixed periodic and inverted parts:

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

Total length 16:

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

Total length 8:

>>> total_length = 8
>>> for index in range(total_length):
...     match = pattern.matches_index(index, total_length)
...     match = match or ''
...     print(index, match)
0 True
1
2 True
3
4 True
5
6
7

Total length 4:

>>> total_length = 4
>>> for index in range(total_length):
...     match = pattern.matches_index(index, total_length)
...     match = match or ''
...     print(index, match)
0 True
1
2
3

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

Complex pattern with compound and simple parts:

>>> pattern = abjad.Pattern(
...     operator='and',
...     patterns=[
...         abjad.Pattern(
...             indices=[0],
...             period=2,
...             ),
...         abjad.Pattern(
...             indices=[-3, -2, -1],
...             inverted=True,
...             ),
...         ],
...     )
>>> pattern = abjad.Pattern(
...     operator='or',
...     patterns=[
...         pattern,
...         abjad.Pattern(
...             indices=[0, 1, 2],
...             ),
...         ],
...     )
>>> abjad.f(pattern)
abjad.Pattern(
    operator='or',
    patterns=(
        abjad.Pattern(
            operator='and',
            patterns=(
                abjad.Pattern(
                    indices=[0],
                    period=2,
                    ),
                abjad.Pattern(
                    indices=[-3, -2, -1],
                    inverted=True,
                    ),
                ),
            ),
        abjad.Pattern(
            indices=[0, 1, 2],
            ),
        ),
    )

Total length 16:

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

Total length 8:

>>> total_length = 8
>>> for index in range(total_length):
...     match = pattern.matches_index(index, total_length)
...     match = match or ''
...     print(index, match)
0 True
1 True
2 True
3
4 True
5
6
7

Total length 4:

>>> total_length = 4
>>> for index in range(total_length):
...     match = pattern.matches_index(index, total_length)
...     match = match or ''
...     print(index, match)
0 True
1 True
2 True
3

Matches every index that is ((equal to 0 % 2) AND (not one of the last three indices)) OR is (one of the first three indices).

Returns true or false.

reverse()

Reverses pattern.

Matches three indices out of every eight:

>>> pattern = abjad.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

Reverses pattern:

>>> pattern = abjad.Pattern(
...     indices=[0, 1, 7],
...     period=8,
...     )
>>> pattern = pattern.reverse()
>>> abjad.f(pattern)
abjad.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

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

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

Reverses pattern:

>>> pattern = pattern.reverse()
>>> abjad.f(pattern)
abjad.Pattern(
    operator='and',
    patterns=(
        abjad.Pattern(
            indices=[-1],
            period=2,
            ),
        abjad.Pattern(
            indices=[2, 1, 0],
            inverted=True,
            ),
        ),
    )

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

Returns new pattern.

rotate(n=0)

Rotates pattern by index n.

Matches three indices out of every eight:

>>> pattern = abjad.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 = abjad.Pattern(
...     indices=[0, 1, 7],
...     period=8,
...     )
>>> pattern = pattern.rotate(n=2)
>>> abjad.f(pattern)
abjad.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

Matches three indices out of every eight with negative indices:

>>> pattern = abjad.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 = abjad.Pattern(
...     indices=[-3, -2, -1],
...     period=8,
...     )
>>> pattern = pattern.rotate(n=2)
>>> abjad.f(pattern)
abjad.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

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

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

Rotates pattern two elements to the right:

>>> pattern = pattern.rotate(n=2)
>>> abjad.f(pattern)
abjad.Pattern(
    operator='and',
    patterns=(
        abjad.Pattern(
            indices=[2],
            period=2,
            ),
        abjad.Pattern(
            indices=[-1, 0, 1],
            inverted=True,
            ),
        ),
    )

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

Returns new pattern.


Class & static methods

classmethod from_vector(vector)

Makes pattern from boolean vector.

Matches three indices out of every five:

>>> pattern = [1, 0, 0, 1, 1]
>>> pattern = abjad.Pattern.from_vector(pattern)
>>> abjad.f(pattern)
abjad.Pattern(
    indices=[0, 3, 4],
    period=5,
    )
>>> total_length = 10
>>> for index in range(10):
...     match = pattern.matches_index(index, total_length)
...     match = match or ''
...     print(index, match)
0 True
1
2
3 True
4 True
5 True
6
7
8 True
9 True

Matches three indices out of every six:

>>> pattern = [1, 0, 0, 1, 1, 0]
>>> pattern = abjad.Pattern.from_vector(pattern)
>>> abjad.f(pattern)
abjad.Pattern(
    indices=[0, 3, 4],
    period=6,
    )
>>> total_length = 12
>>> for index in range(12):
...     match = pattern.matches_index(index, total_length)
...     match = match or ''
...     print(index, match)
0 True
1
2
3 True
4 True
5
6 True
7
8
9 True
10 True
11

Returns pattern.

static index(indices, period=None, inverted=None)

Makes pattern that matches indices.

Indexes item 2:

>>> pattern = abjad.index([2])
>>> abjad.f(pattern)
abjad.index([2])

Indexes items 2, 3 and 5:

>>> pattern = abjad.index([2, 3, 5])
>>> abjad.f(pattern)
abjad.index([2, 3, 5])

Returns pattern.

static index_all(inverted=None)

Makes pattern that matches all indices.

Indexes all divisions for tie creation:

>>> pattern = abjad.index_all()
>>> abjad.f(pattern)
abjad.index_all()

Returns pattern.

static index_first(n, inverted=None)

Makes pattern that matches the first n indices.

Indexes first division for tie creation:

>>> pattern = abjad.index_first(1)
>>> abjad.f(pattern)
abjad.index_first(1)

Indexes first two items:

>>> pattern = abjad.index_first(2)
>>> abjad.f(pattern)
abjad.index_first(2)

Indexes nothing:

>>> pattern = abjad.index_first(0)
>>> abjad.f(pattern)
abjad.index_first(0)

Returns pattern.

static index_last(n, inverted=None)

Makes pattern that matches the last n indices.

Indexes last two items:

>>> pattern = abjad.index_last(2)
>>> abjad.f(pattern)
abjad.index_last(2)

Indexes nothing:

>>> pattern = abjad.index_last(0)
>>> abjad.f(pattern)
abjad.index_last(0)

Returns pattern.


Read-only properties

indices

Gets indices of pattern.

Matches three indices out of every eight:

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

Matches three indices out of every sixteen:

>>> pattern = abjad.Pattern(
...     indices=[0, 1, 7],
...     period=16,
...     )
>>> pattern.indices
[0, 1, 7]

Defaults to none.

Set to integers or none.

Returns integers or none.

inverted

Is true when pattern is inverted.

Matches three indices out of every eight:

>>> pattern = abjad.Pattern(
...     indices=[0, 1, 7],
...     period=8,
...     )
>>> pattern.inverted is None
True
>>> 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

Pattern that rejects three indices from every eight; equivalently, pattern matches 8-3=5 indices out of every eight:

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

Matches every index that is (one of the first three indices) OR (one of the last three indices):

>>> pattern_1 = abjad.index_first(3)
>>> pattern_2 = abjad.index_last(3)
>>> pattern = pattern_1 | pattern_2
>>> pattern.inverted is None
True
>>> pattern.get_boolean_vector(total_length=16)
[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1]

Matches every index that is NOT (one of the first three indices) OR (one of the last three indices):

>>> pattern = abjad.new(pattern, inverted=True)
>>> pattern.inverted
True
>>> pattern.get_boolean_vector(total_length=16)
[0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0]

Defaults to none.

Set to true, false or none.

Returns true, false or none.

operator

Gets operator of pattern.

Set to string.

Returns string.

patterns

Gets paterns of pattern.

Set to patterns or none.

Returns tuple of patterns or none.

payload

Gets payload of pattern.

Pattern with string payload assigned to three of every eight indices:

>>> pattern = abjad.Pattern(
...     indices=[0, 1, 7],
...     payload='Allegro non troppo',
...     period=8,
...     )
>>> total_length = 10
>>> for index in range(10):
...     match = pattern.matches_index(index, total_length)
...     if match:
...         payload = pattern.payload
...     else:
...         payload = ''
...     print(index, repr(payload))
...
0 'Allegro non troppo'
1 'Allegro non troppo'
2 ''
3 ''
4 ''
5 ''
6 ''
7 'Allegro non troppo'
8 'Allegro non troppo'
9 'Allegro non troppo'

Defaults to none.

Set to any object.

Returns arbitrary object.

period

Gets period of pattern.

Pattern with a period of eight:

>>> pattern = abjad.Pattern(
...     indices=[0, 1, 7],
...     period=8,
...     )
>>> pattern.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

Same pattern with a period of sixteen:

>>> pattern = abjad.Pattern(
...     indices=[0, 1, 7],
...     period=16,
...     )
>>> pattern.period
16
>>> 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
9
10
11
12
13
14
15

Gets period of pattern that indexs every fourth and fifth element:

>>> pattern_1 = abjad.Pattern([0], period=4)
>>> pattern_2 = abjad.Pattern([0], period=5)
>>> pattern = pattern_1 | pattern_2
>>> abjad.f(pattern)
abjad.Pattern(
    operator='or',
    patterns=(
        abjad.Pattern(
            indices=[0],
            period=4,
            ),
        abjad.Pattern(
            indices=[0],
            period=5,
            ),
        ),
    period=20,
    )
>>> pattern.period
20

Returns none when pattern contains acyclic parts:

>>> pattern_1 = abjad.Pattern([0], period=4)
>>> pattern_2 = abjad.Pattern([0])
>>> pattern = pattern_1 | pattern_2
>>> abjad.f(pattern)
abjad.Pattern(
    operator='or',
    patterns=(
        abjad.Pattern(
            indices=[0],
            period=4,
            ),
        abjad.Pattern(
            indices=[0],
            ),
        ),
    )
>>> pattern.period is None
True

Defaults to none.

Set to positive integer or none.

Returns positive integer or none.

template

Get pattern template.

Set to string or none.

Returns string or none.

weight

Gets weight of pattern.

Gets weight of cyclic pattern:

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

Gets weight of acyclic pattern:

>>> pattern = abjad.Pattern(
...     indices=[0, 2, 3],
...     )
>>> pattern.weight
3

Weight defined equal to number of indices in pattern.

Returns nonnegative integer.