Expression

class datastructuretools.Expression(argument_count=None, argument_values=None, callbacks=None, evaluation_template=None, force_return=None, has_parentheses=None, is_composite=None, is_initializer=None, is_postfix=None, keywords=None, map_operand=None, markup_maker_callback=None, module_names=None, name=None, next_name=None, precedence=None, proxy_class=None, qualified_method_name=None, string_template=None, subclass_hook=None, subexpressions=None)

Expression.

>>> import abjad

Makes identity expression:

>>> expression = abjad.Expression()
>>> expression() is None
True
>>> expression(99)
99
>>> expression([99, 99.25])
[99, 99.25]

Makes integer initialization expression:

>>> expression = abjad.Expression(evaluation_template='int({})')
>>> expression()
0
>>> expression(99)
99
>>> expression(99.25)
99

Makes binary integer initialization expression:

>>> expression = abjad.Expression(evaluation_template='int({}, base=2)')
>>> expression('1')
1
>>> expression('11')
3
>>> expression('111')
7

Makes three-integer addition expression:

>>> expression = abjad.Expression(
...     argument_count=3,
...     evaluation_template='{} + {} + {}',
...     )
>>> expression(1, 2, 3)
6

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.AbjadValueObject.AbjadValueObject" [color=1, group=0, label=AbjadValueObject, shape=box]; "abjad.tools.abctools.AbjadObject.AbjadObject" -> "abjad.tools.abctools.AbjadValueObject.AbjadValueObject"; "abjad.tools.abctools.AbjadObject.AbstractBase" -> "abjad.tools.abctools.AbjadObject.AbjadObject"; } subgraph cluster_datastructuretools { graph [label=datastructuretools]; "abjad.tools.datastructuretools.Expression.Expression" [color=black, fontcolor=white, group=2, label=<<B>Expression</B>>, shape=box, style="filled, rounded"]; } subgraph cluster_builtins { graph [label=builtins]; "builtins.object" [color=2, group=1, label=object, shape=box]; } "abjad.tools.abctools.AbjadValueObject.AbjadValueObject" -> "abjad.tools.datastructuretools.Expression.Expression"; "builtins.object" -> "abjad.tools.abctools.AbjadObject.AbstractBase"; }

Bases

Attribute summary

append_callback(callback) Appends callback to expression.
argument_count Gets argument count.
argument_values Gets argument values.
callbacks Gets callbacks.
establish_equivalence(name) Makes new expression with name.
evaluation_template Gets evaluation template.
force_return Is true when expression should return primary input argument.
get_markup([direction, name]) Gets markup directly.
get_string([name]) Gets string.
has_parentheses Is true when expression has parentheses.
is_composite Is true when expression is composite.
is_initializer Is true when expression is initializer.
is_postfix Is true when expression is postfix.
iterate(**keywords) Makes iterate expression.
keywords Gets keywords.
label(**keywords) Makes label expression.
make_callback([argument_count, …]) Makes callback.
map_operand Gets expression to map.
markup_maker_callback Gets markup-maker callback.
module_names Gets module names.
name Gets name.
next_name Gets next name.
pitch_class_segment(**keywords) Makes pitch-class segment expression.
precedence Gets precedence.
proxy_class Gets proxy class.
qualified_method_name Gets qualified method name of expression.
sequence(**keywords) Makes sequence expression.
string_template Gets string template.
subclass_hook Gets subclass hook.
subexpressions Gets subexpressions.
wrap_in_list() Makes expression to wrap argument in list.
__add__(i) Gets proxy method or adds expressions.
__call__(*arguments, **keywords) Calls expression on arguments with keywords.
__copy__(*arguments) Copies Abjad value object.
__eq__(argument) Is true when expression storage format equals argument storage format.
__format__([format_specification]) Formats expression.
__getattr__(name) Gets attribute name.
__getitem__(argument) Gets proxy method.
__hash__() Hashes expression.
__iadd__(i) Gets proxy method.
__ne__(argument) Is true when Abjad object does not equal argument.
__radd__(i) Gets proxy method.
__repr__() Gets interpreter representation.
__setitem__(i, argument) Gets proxy method.
__str__() Gets string representation of expression.

Read-only properties

Expression.argument_count

Gets argument count.

Defaults to none.

Set to nonnegative integer or none.

Returns nonnegative integer or none.

Expression.argument_values

Gets argument values.

Defaults to none.

Set to dictionary or none.

Returns dictionary or none.

Expression.callbacks

Gets callbacks.

Defaults to none:

>>> expression = abjad.Expression()
>>> expression.callbacks is None
True

Set to callbacks or none.

Expression.evaluation_template

Gets evaluation template.

Defaults to none.

Set to string.

Returns string.

Expression.force_return

Is true when expression should return primary input argument. Otherwise false.

Defaults to none.

Set to true, false or none.

Returns true, false or none.

Expression.has_parentheses

Is true when expression has parentheses. Otherwise false.

Defaults to none.

Set to true, false or none.

Returns true, false or none.

Expression.is_composite

Is true when expression is composite. Otherwise false.

Defaults to none.

Set to true, false or none.

Returns true, false or none.

Expression.is_initializer

Is true when expression is initializer. Otherwise false.

Defaults to none.

Set to true, false or none.

Returns true, false or none.

Expression.is_postfix

Is true when expression is postfix. Otherwise false.

Defaults to none.

Set to true, false or none.

Returns true, false or none.

Expression.keywords

Gets keywords.

Defaults to none.

Set to dictionary or none.

Returns dictionary or none.

Expression.map_operand

Gets expression to map.

Defaults to none.

Set to expression or none.

Returns expression or none.

Expression.markup_maker_callback

Gets markup-maker callback.

Defaults to none.

Set to string or none.

Returns string or none.

Expression.module_names

Gets module names.

Defaults to none.

Set to strings or none.

Returns strings or none.

Expression.name

Gets name.

Preserves name after initializer callback:

>>> expression = abjad.Expression(name='J')
>>> expression.name
'J'
>>> expression = expression.sequence()
>>> expression.name
'J'

Defaults to none.

Set to string or none.

Returns string or none.

Expression.next_name

Gets next name.

Defaults to none.

Set to string or none.

Returns string or none.

Expression.precedence

Gets precedence.

Defaults to none.

Set to integer or none.

Returns integer or none.

Expression.proxy_class

Gets proxy class.

Defaults to none.

Set to class or none.

Returns class or none.

Expression.qualified_method_name

Gets qualified method name of expression.

Returns string or none.

Expression.string_template

Gets string template.

Defaults to none.

Set to string or none.

Returns string or none.

Expression.subclass_hook

Gets subclass hook.

Only to be set by expression subclasses.

Set to name of custom evaluation method.

Defaults to none.

Set to string or none.

Returns string or none.

Expression.subexpressions

Gets subexpressions.

Defaults to none.

Set to expressions or none.

Returns list of expressions or none.

Methods

Expression.append_callback(callback)

Appends callback to expression.

>>> expression = abjad.Expression()
>>> expression.callbacks is None
True
>>> callback = abjad.Expression(evaluation_template='int({})')
>>> expression = expression.append_callback(callback)
>>> for callback in expression.callbacks:
...     callback
... 
Expression(evaluation_template='int({})')
>>> callback = abjad.Expression(evaluation_template='{}**2')
>>> expression = expression.append_callback(callback)
>>> for expression in expression.callbacks:
...     expression
... 
Expression(evaluation_template='int({})')
Expression(evaluation_template='{}**2')

Returns new expression.

Expression.establish_equivalence(name)

Makes new expression with name.

>>> expression = abjad.Expression(name='J')
>>> expression = expression.pitch_class_segment()
>>> expression = expression.rotate(n=1)
>>> expression = expression.rotate(n=2)
>>> expression = expression.establish_equivalence(name='Q')
>>> expression([-2, -1.5, 6, 7, -1.5, 7])
PitchClassSegment([7, 10.5, 7, 10, 10.5, 6])
>>> expression.get_string()
'Q = r2(r1(J))'
>>> markup = expression.get_markup()
>>> show(markup) 

Returns new expression.

Expression.get_markup(direction=None, name=None)

Gets markup directly.

Avoids markup expressions.

Returns markup or none.

Expression.get_string(name=None)

Gets string.

Gets string for sequence expression:

Without name:

>>> expression = abjad.sequence()
>>> expression = expression.reverse()
>>> expression = expression.rotate(n=2)
>>> expression([1, 2, 3, 4, 5, 6])
Sequence([2, 1, 6, 5, 4, 3])
>>> abjad.Expression.get_string(expression, name='J')
'r2(R(J))'

With name:

>>> expression = abjad.Expression(name='J')
>>> expression = expression.sequence()
>>> expression = expression.reverse()
>>> expression = expression.rotate(n=2)
>>> expression([1, 2, 3, 4, 5, 6])
Sequence([2, 1, 6, 5, 4, 3])
>>> abjad.Expression.get_string(expression)
'r2(R(J))'

Overrides name:

>>> abjad.Expression.get_string(expression, name='K')
'r2(R(K))'

Gets string for segment expression:

Without name:

>>> expression = abjad.Expression()
>>> expression = expression.pitch_class_segment()
>>> expression = expression.invert()
>>> expression = expression.rotate(n=2)
>>> expression([-2, -1.5, 6, 7, -1.5, 7])
PitchClassSegment([1.5, 5, 2, 1.5, 6, 5])
>>> abjad.Expression.get_string(expression, name='J')
'r2(I(J))'

With name:

>>> expression = abjad.Expression(name='J')
>>> expression = expression.pitch_class_segment()
>>> expression = expression.invert()
>>> expression = expression.rotate(n=2)
>>> expression([-2, -1.5, 6, 7, -1.5, 7])
PitchClassSegment([1.5, 5, 2, 1.5, 6, 5])
>>> abjad.Expression.get_string(expression)
'r2(I(J))'

Overrides name:

>>> abjad.Expression.get_string(expression, name='K')
'r2(I(K))'

Returns string or none.

Expression.iterate(**keywords)

Makes iterate expression.

Makes expression to iterate leaves:

>>> staff = abjad.Staff()
>>> staff.append(abjad.Measure((2, 8), "<c' bf'>8 <g' a'>8"))
>>> staff.append(abjad.Measure((2, 8), "af'8 r8"))
>>> staff.append(abjad.Measure((2, 8), "r8 gf'8"))
>>> show(staff) 
>>> expression = abjad.Expression()
>>> expression = expression.iterate()
>>> expression = expression.by_leaf()
>>> for leaf in expression(staff):
...     leaf
...
Chord("<c' bf'>8")
Chord("<g' a'>8")
Note("af'8")
Rest('r8')
Rest('r8')
Note("gf'8")

Returns expression.

Expression.label(**keywords)

Makes label expression.

Makes expression to label logical tie durations:

>>> staff = abjad.Staff(r"c'4. d'8 ~ d'4. e'16 [ ef'16 ]")
>>> expression = abjad.Expression()
>>> expression = expression.label()
>>> expression = expression.with_durations()
>>> expression(staff)
>>> show(staff) 

Returns expression.

Expression.pitch_class_segment(**keywords)

Makes pitch-class segment expression.

Makes expression to transpose pitch-class segment:

>>> items = [-2, -1.5, 6, 7, -1.5, 7]
>>> J = abjad.PitchClassSegment(items=items)
>>> J
PitchClassSegment([10, 10.5, 6, 7, 10.5, 7])
>>> show(J) 
>>> expression = abjad.Expression(name='J')
>>> expression = expression.pitch_class_segment()
>>> expression = expression.transpose(n=13)
>>> expression([-2, -1.5, 6, 7, -1.5, 7])
PitchClassSegment([11, 11.5, 7, 8, 11.5, 8])
>>> segment = expression([-2, -1.5, 6, 7, -1.5, 7])
>>> markup = expression.get_markup()
>>> show(segment, figure_name=markup) 

Returns expression.

Expression.sequence(**keywords)

Makes sequence expression.

Makes expression to initialize, flatten and reverse sequence:

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

Returns expression.

Expression.wrap_in_list()

Makes expression to wrap argument in list.

>>> expression = abjad.Expression()
>>> expression = expression.wrap_in_list()
>>> expression(abjad.Markup('Allegro assai'))
[Markup(contents=['Allegro assai'])]
>>> f(expression)
abjad.Expression(
    callbacks=[
        abjad.Expression(
            evaluation_template='[{}]',
            ),
        ],
    )

Returns expression.

Class & static methods

static Expression.make_callback(argument_count=None, evaluation_template=None, force_return=None, has_parentheses=None, is_composite=None, is_initializer=None, is_postfix=None, keywords=None, map_operand=None, module_names=None, next_name=None, precedence=None, qualified_method_name=None, string_template=None)

Makes callback.

Returns expression.

Special methods

Expression.__add__(i)

Gets proxy method or adds expressions.

Adds expressions:

>>> expression_1 = abjad.Expression(
...     argument_count=3,
...     evaluation_template='{} + {} + {}',
...     )
>>> expression_2 = abjad.Expression(
...     argument_count=2,
...     evaluation_template='{} + {}',
...     )
>>> expression = expression_1 + expression_2
>>> f(expression)
abjad.Expression(
    argument_count=2,
    evaluation_template='{}.__add__({})',
    is_composite=True,
    markup_maker_callback='_make_add_expression_markup',
    qualified_method_name='abjad.Expression.__add__',
    string_template='{} + {}',
    subexpressions=(
        abjad.Expression(
            argument_count=3,
            evaluation_template='{} + {} + {}',
            ),
        abjad.Expression(
            argument_count=2,
            evaluation_template='{} + {}',
            ),
        ),
    )
>>> expression(1, 2, 3, 4, 5)
15
Expression.__call__(*arguments, **keywords)

Calls expression on arguments with keywords.

Calls identity expression:

>>> expression = abjad.Expression()
>>> expression() is None
True

Returns ouput of last callback.

(AbjadValueObject).__copy__(*arguments)

Copies Abjad value object.

Returns new Abjad value object.

Expression.__eq__(argument)

Is true when expression storage format equals argument storage format. Otherwise false.

>>> expression_1 = abjad.Expression().sequence()
>>> expression_2 = abjad.Expression().sequence()
>>> expression_1 == expression_2
True
>>> expression_1 = abjad.Expression().sequence()
>>> expression_2 = abjad.new(expression_1)
>>> expression_1 == expression_2
True
>>> expression_1 = abjad.Expression().sequence()
>>> expression_2 = abjad.Expression().sequence().reverse()
>>> expression_1 == expression_2
False
>>> expression_1 = abjad.Expression().sequence()
>>> expression_1 == 'text'
False

Returns true or false.

Expression.__format__(format_specification='')

Formats expression.

Formats identity expression:

>>> expression = abjad.Expression()
>>> f(expression)
abjad.Expression()

Returns string.

Expression.__getattr__(name)

Gets attribute name.

Returns proxy method when proxy class is set.

Returns normally when proxy class is not set.

Expression.__getitem__(argument)

Gets proxy method.

Expression.__hash__()

Hashes expression.

Returns integer.

Expression.__iadd__(i)

Gets proxy method.

(AbjadObject).__ne__(argument)

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

Returns true or false.

Expression.__radd__(i)

Gets proxy method.

Expression.__repr__()

Gets interpreter representation.

Gets interpreter representation of identity expression:

>>> expression = abjad.Expression()
>>> expression
Expression()

Returns string.

Expression.__setitem__(i, argument)

Gets proxy method.

Expression.__str__()

Gets string representation of expression.

Gets string representation of identity expression:

>>> expression = abjad.Expression()
>>> str(expression)
'Expression()'

Returns string.