SequenceExpression

class expressiontools.SequenceExpression(callbacks=None)

Sequence expression.

Example 1. Makes expression to initialize sequence:

>>> expression = sequence()
>>> expression()
Sequence(())
>>> expression([1, 2, [3, [4]], 5])
Sequence((1, 2, [3, [4]], 5))

Example 2. Makes expression to flatten sequence:

>>> expression = sequence()
>>> expression = expression.flatten()
>>> expression([1, 2, [3, [4]], 5])
Sequence((1, 2, 3, 4, 5))

Example 3. Makes expression ot reverse sequence:

>>> expression = sequence()
>>> expression = expression.reverse()
>>> expression([1, 2, [3, [4]], 5])
Sequence((5, [3, [4]], 2, 1))

Example 4. Makes expression to flatten and reverse sequence:

>>> expression = sequence()
>>> expression = expression.flatten()
>>> expression = expression.reverse()
>>> expression([1, 2, [3, [4]], 5])
Sequence((5, 4, 3, 2, 1))

Example 5. Makes expression to reverse and flatten sequence:

>>> expression = sequence()
>>> expression = expression.reverse()
>>> expression = expression.flatten()
>>> expression([1, 2, [3, [4]], 5])
Sequence((5, 3, 4, 2, 1))

Example 6. Makes expression to get item from sequence:

>>> expression = sequence()
>>> expression = expression[-1]
>>> expression([1, 2, [3, [4]], 5])
5

Example 7. Makes expression to get item from sequence and wrap result in new sequence:

>>> expression = sequence()
>>> expression = expression[-1]
>>> expression = expression.sequence()
>>> expression([1, 2, [3, [4]], 5])
Sequence((5,))

Example 8. Makes expression to get slice from sequence:

>>> expression = sequence()
>>> expression = expression[:-1]
>>> expression([1, 2, [3, [4]], 5])
Sequence((1, 2, [3, [4]]))

Example 9. Makes expression to get slice from sequence and flatten slice:

>>> expression = sequence()
>>> expression = expression[:-1]
>>> expression = expression.flatten()
>>> expression([1, 2, [3, [4]], 5])
Sequence((1, 2, 3, 4))

Example 10. Makes expression to add [4, 5] to sequence:

>>> expression = sequence()
>>> expression = expression.__add__([4, 5])
>>> expression([1, 2, 3])
Sequence((1, 2, 3, 4, 5))

Example 11. Makes expression to partition sequence into thirds and get middle third:

>>> expression = sequence()
>>> ratio = mathtools.Ratio((1, 1, 1))
>>> expression = expression.partition_by_ratio_of_lengths(ratio)
>>> expression = expression[1]
>>> expression(range(1, 10+1))
Sequence((4, 5, 6, 7))

Example 12. Makes expression to partition sequence into parts with lengths equal to three:

>>> expression = sequence()
>>> expression = expression.partition_by_counts([3], cyclic=True)
>>> expression(range(1, 10+1))
Sequence((Sequence((1, 2, 3)), Sequence((4, 5, 6)), Sequence((7, 8, 9))))

Example 13. Makes expression to partition sequence and sum parts:

>>> expression = sequence()
>>> expression = expression.partition_by_counts([3], cyclic=True)
>>> expression = expression.map().sum()
>>> expression(range(1, 10+1))
Sequence((6, 15, 24))

Example 14. Makes expression to sum sequence:

>>> expression = sequence()
>>> expression = expression.sum()
>>> expression(range(10))
45

Example 15. Makes expression to sum sequence and wrap result in new sequence:

>>> expression = sequence()
>>> expression = expression.sum()
>>> expression = expression.sequence()
>>> expression(range(1, 10+1))
Sequence((55,))

Example 16. Makes expression to split sequence by weights:

>>> expression = sequence()
>>> expression = expression.split([10], cyclic=True)
>>> expression = expression.sequence()
>>> expression(range(1, 10+1))
Sequence((Sequence((1, 2, 3, 4)), Sequence((5, 5)), Sequence((1, 7, 2)), Sequence((6, 4)), Sequence((5, 5))))

Example 17. Makes expression to rotate sequence:

>>> expression = sequence()
>>> expression = expression.rotate(-1)
>>> expression(range(1, 10+1))
Sequence((2, 3, 4, 5, 6, 7, 8, 9, 10, 1))

Note

Aadd usage examples to this docstring. Do not add usage examples to property and method docstrings. Properties and methods will all be derived automatically from the Sequence class at some point in future.

Initializer returns expression.

Expression returns object.

Lineage

digraph InheritanceGraph { graph [background=transparent, bgcolor=transparent, color=lightslategrey, fontname=Arial, outputorder=edgesfirst, overlap=prism, penwidth=2, rankdir=LR, root="__builtin__.object", splines=spline, style="dotted, rounded", truecolor=true]; node [colorscheme=pastel19, fontname=Arial, fontsize=12, penwidth=2, style="filled, rounded"]; edge [color=lightsteelblue2, penwidth=2]; subgraph cluster_abctools { graph [label=abctools]; "abjad.tools.abctools.AbjadObject.AbjadObject" [color=1, group=0, label=AbjadObject, shape=box]; "abjad.tools.abctools.AbjadObject.AbstractBase" [color=1, group=0, label=AbstractBase, shape=box]; "abjad.tools.abctools.AbjadObject.AbstractBase" -> "abjad.tools.abctools.AbjadObject.AbjadObject"; } subgraph cluster_expressiontools { graph [label=expressiontools]; "abjad.tools.expressiontools.Expression.Expression" [color=3, group=2, label=Expression, shape=box]; "abjad.tools.expressiontools.SequenceExpression.SequenceExpression" [color=black, fontcolor=white, group=2, label=<<B>SequenceExpression</B>>, shape=box, style="filled, rounded"]; "abjad.tools.expressiontools.Expression.Expression" -> "abjad.tools.expressiontools.SequenceExpression.SequenceExpression"; } subgraph cluster_builtins { graph [label=builtins]; "builtins.object" [color=2, group=1, label=object, shape=box]; } "abjad.tools.abctools.AbjadObject.AbjadObject" -> "abjad.tools.expressiontools.Expression.Expression"; "builtins.object" -> "abjad.tools.abctools.AbjadObject.AbstractBase"; }

Bases

Attribute summary

callbacks Gets callbacks.
flatten([classes, depth, indices]) Makes flatten callback.
map() Makes map callback.
partition_by_counts(counts[, cyclic, overhang]) Makes partition-by-counts callback.
partition_by_ratio_of_lengths(ratio) Makes partition-by-ratio-of-lengths callback.
reverse() Makes reverse callback.
rotate([index]) Makes rotate callback.
sequence() Makes sequence callback.
split(weights[, cyclic, overhang]) Makes split callback.
sum() Makes sum callback.
__add__(expr) Makes add callback.
__call__([items]) Calls sequence expression on items.
__eq__(expr) Is true when ID of expr equals ID of Abjad object.
__format__([format_specification]) Formats sequence expression.
__getitem__(i) Makes get-item callback.
__hash__() Hashes Abjad object.
__ne__(expr) Is true when Abjad object does not equal expr.
__radd__(expr) Makes right-add callback.
__repr__() Gets interpreter representation of Abjad object.

Read-only properties

(Expression).callbacks

Gets callbacks.

Returns tuple or none.

Methods

SequenceExpression.flatten(classes=None, depth=-1, indices=None)

Makes flatten callback.

Returns callback.

SequenceExpression.map()

Makes map callback.

Returns callback.

SequenceExpression.partition_by_counts(counts, cyclic=False, overhang=False)

Makes partition-by-counts callback.

Returns callback.

SequenceExpression.partition_by_ratio_of_lengths(ratio)

Makes partition-by-ratio-of-lengths callback.

Returns callback.

SequenceExpression.reverse()

Makes reverse callback.

Returns callback.

SequenceExpression.rotate(index=None)

Makes rotate callback.

Returns callback.

SequenceExpression.sequence()

Makes sequence callback.

Returns callback.

SequenceExpression.split(weights, cyclic=False, overhang=False)

Makes split callback.

Returns callback.

SequenceExpression.sum()

Makes sum callback.

Returns callback.

Special methods

SequenceExpression.__add__(expr)

Makes add callback.

Returns callback.

SequenceExpression.__call__(items=None)

Calls sequence expression on items.

Makes sequence from items.

Then applies callbacks to sequence.

Returns sequence.

(AbjadObject).__eq__(expr)

Is true when ID of expr equals ID of Abjad object. Otherwise false.

Returns true or false.

SequenceExpression.__format__(format_specification='')

Formats sequence expression.

Example 1. Gets storage format:

>>> expression = sequence()
>>> expression = expression.reverse()
>>> expression = expression.flatten()
>>> expression = expression[:-3]
>>> expression = expression[0]
>>> print(format(expression))
expressiontools.SequenceExpression(
    callbacks=(
        expressiontools.Callback(
            name='Sequence.reverse',
            ),
        expressiontools.Callback(
            name='Sequence.flatten',
            arguments=[
                ('classes', None),
                ('depth', -1),
                ('indices', None),
                ],
            ),
        expressiontools.Callback(
            name='Sequence.__getitem__',
            arguments=[
                (
                    'i',
                    slice(None, -3, None),
                    ),
                ],
            ),
        expressiontools.Callback(
            name='Sequence.__getitem__',
            arguments=[
                ('i', 0),
                ],
            ),
        ),
    )

Returns string.

SequenceExpression.__getitem__(i)

Makes get-item callback.

Returns callback.

(AbjadObject).__hash__()

Hashes Abjad object.

Required to be explicitly redefined on Python 3 if __eq__ changes.

Returns integer.

(AbjadObject).__ne__(expr)

Is true when Abjad object does not equal expr. Otherwise false.

Returns true or false.

SequenceExpression.__radd__(expr)

Makes right-add callback.

Returns callback.

(AbjadObject).__repr__()

Gets interpreter representation of Abjad object.

Returns string.