Exemplo n.º 1
0
    def __format__(self, format_specification=''):
        r'''Formats scheme.

        Set `format_specification` to `''`', `'lilypond'` or ``'storage'``.
        Interprets `''` equal to `'lilypond'`.

        ..  container:: example

            **Example 1.** Scheme LilyPond format:

            ::

                >>> scheme = schemetools.Scheme('foo')
                >>> format(scheme)
                '#foo'

        ..  container:: example

            **Example 2.** Scheme storage format:

            ::

                >>> print(format(scheme, 'storage'))
                schemetools.Scheme(
                    'foo'
                    )

        Returns string.
        '''
        from abjad.tools import systemtools
        if format_specification in ('', 'lilypond'):
            return self._lilypond_format
        elif format_specification == 'storage':
            return systemtools.StorageFormatAgent(self).get_storage_format()
        return str(self)
Exemplo n.º 2
0
def test_systemtools_StorageFormatAgent_get_import_statements_02():
    subject = selectortools.Selector().by_leaf()
    agent = systemtools.StorageFormatAgent(subject)
    assert agent.get_import_statements() == (
        'from abjad.tools import scoretools',
        'from abjad.tools import selectortools',
    )
Exemplo n.º 3
0
def test_systemtools_StorageFormatAgent_get_import_statements_04():
    subject = rhythmmakertools.IncisedRhythmMaker(
        incise_specifier=rhythmmakertools.InciseSpecifier(
            prefix_talea=(1, ),
            prefix_counts=(0, ),
            suffix_talea=(1, ),
            suffix_counts=(1, ),
            talea_denominator=16,
            body_ratio=mathtools.Ratio((1, )),
            outer_divisions_only=True,
        ),
        beam_specifier=rhythmmakertools.BeamSpecifier(
            beam_each_division=False,
            beam_divisions_together=False,
        ),
        duration_spelling_specifier=rhythmmakertools.DurationSpellingSpecifier(
            decrease_durations_monotonically=True,
            forbidden_written_duration=durationtools.Duration(1, 2),
        ),
        tuplet_spelling_specifier=rhythmmakertools.TupletSpellingSpecifier(
            avoid_dots=True,
            is_diminution=True,
            simplify_redundant_tuplets=True,
        ),
    )
    agent = systemtools.StorageFormatAgent(subject)
    assert agent.get_import_statements() == (
        'from abjad.tools import durationtools',
        'from abjad.tools import mathtools',
        'from abjad.tools import rhythmmakertools',
    )
Exemplo n.º 4
0
    def __repr__(self):
        r'''Gets interpreter representation of Abjad object.

        Returns string.
        '''
        from abjad.tools import systemtools
        return systemtools.StorageFormatAgent(self).get_repr_format()
Exemplo n.º 5
0
    def __format__(self, format_specification=''):
        r'''Formats LilyPond file.

        ..  container:: example

            **Example 1.** Gets format:

                >>> lilypond_file = lilypondfiletools.make_basic_lilypond_file()

            ::

                >>> print(format(lilypond_file)) # doctest: +SKIP
                % 2016-01-31 20:29
                <BLANKLINE>
                \version "2.19.35"
                \language "english"
                <BLANKLINE>
                \header {}
                <BLANKLINE>
                \layout {}
                <BLANKLINE>
                \paper {}

        Returns string.
        '''
        from abjad.tools import systemtools
        if format_specification in ('', 'lilypond'):
            return self._lilypond_format
        elif format_specification == 'storage':
            return systemtools.StorageFormatAgent(self).get_storage_format()
        return str(self)
Exemplo n.º 6
0
    def __format__(self, format_specification=''):
        r'''Formats stem tremolo.

        ..  container:: example

            **Example 1.** Sixteenth-note tremolo:

            ::

                >>> stem_tremolo = indicatortools.StemTremolo(16)
                >>> print(format(stem_tremolo))
                :16

        ..  container:: example

            **Example 2.** Thirty-second-note tremolo:

            ::

                >>> stem_tremolo = indicatortools.StemTremolo(32)
                >>> print(format(stem_tremolo))
                :32

        Returns string.
        '''
        from abjad.tools import systemtools
        if format_specification in ('', 'lilypond'):
            return self._lilypond_format
        elif format_specification == 'storage':
            return systemtools.StorageFormatAgent(self).get_storage_format()
        return str(self)
Exemplo n.º 7
0
    def __format__(self, format_specification=''):
        r'''Formats time signature.

        ..  container:: example

            **Example 1.** First time signature:

            ::

                >>> print(format(TimeSignature((3, 8))))
                indicatortools.TimeSignature((3, 8))

        ..  container:: example

            **Example 2.** Second time signature:

            ::

                >>> print(format(TimeSignature((4, 4))))
                indicatortools.TimeSignature((4, 4))

        Returns string.
        '''
        from abjad.tools import systemtools
        if format_specification in ('', 'storage'):
            return systemtools.StorageFormatAgent(self).get_storage_format()
        elif format_specification == 'lilypond':
            return self._lilypond_format
        return str(self)
Exemplo n.º 8
0
 def _get_format_specification(self):
     agent = systemtools.StorageFormatAgent(self)
     names = agent.signature_names
     return systemtools.FormatSpecification(
         client=self,
         repr_is_indented=True,
         template_names=names,
     )
Exemplo n.º 9
0
def test_systemtools_StorageFormatAgent_get_import_statements_03():
    subject = [
        indicatortools.TimeSignature((3, 4)),
        indicatortools.TimeSignature((4, 4)),
    ]
    agent = systemtools.StorageFormatAgent(subject)
    assert agent.get_import_statements() == (
        'from abjad.tools import indicatortools', )
Exemplo n.º 10
0
    def __repr__(self):
        """
        Gets interpreter representation of Supriya object.

        Returns string.
        """
        from abjad.tools import systemtools
        return systemtools.StorageFormatAgent(self).get_repr_format()
Exemplo n.º 11
0
    def __hash__(self):
        r'''Hashes LilyPond grob override.

        Returns integer.
        '''
        from abjad.tools import systemtools
        hash_values = systemtools.StorageFormatAgent(self).get_hash_values()
        return hash(hash_values)
Exemplo n.º 12
0
def new(expr, **kwargs):
    r'''Makes new `expr` with optional new `kwargs`.

    Returns new object with the same type as `expr`.
    '''
    from abjad.tools import systemtools
    if expr is None:
        return expr
    agent = systemtools.StorageFormatAgent(expr)
    template_dict = agent.get_template_dict()
    recursive_arguments = {}
    for key, value in kwargs.items():
        if '__' in key:
            key, divider, subkey = key.partition('__')
            if key not in recursive_arguments:
                recursive_arguments[key] = []
            pair = (subkey, value)
            recursive_arguments[key].append(pair)
            continue
        if key in template_dict or agent.signature_accepts_kwargs:
            template_dict[key] = value
        elif isinstance(getattr(expr, key, None), types.MethodType):
            method = getattr(expr, key)
            result = method(value)
            if isinstance(result, type(expr)):
                expr = result
                template_dict.update(
                    systemtools.StorageFormatAgent(expr).get_template_dict())
        else:
            message = '{} has no key {!r}'.format(type(expr), key)
            raise KeyError(message)
    for key, pairs in recursive_arguments.items():
        recursed_object = getattr(expr, key)
        if recursed_object is None:
            continue
        recursive_template_dict = dict(pairs)
        recursed_object = new(recursed_object, **recursive_template_dict)
        if key in template_dict:
            template_dict[key] = recursed_object
    positional_values = []
    for name in agent.signature_positional_names:
        if name in template_dict:
            positional_values.append(template_dict.pop(name))
    result = type(expr)(*positional_values, **template_dict)
    return result
Exemplo n.º 13
0
    def __format__(self, format_specification=''):
        r'''Formats time relation.

        Returns string.
        '''
        from abjad.tools import systemtools
        if format_specification in ('', 'storage'):
            return systemtools.StorageFormatAgent(self).get_storage_format()
        return str(self)
Exemplo n.º 14
0
 def __hash__(self, expr):
     from abjad.tools import systemtools
     hash_values = systemtools.StorageFormatAgent(self).get_hash_values()
     try:
         result = hash(hash_values)
     except TypeError:
         message = 'unhashable type: {}'.format(self)
         raise TypeError(message)
     return result
Exemplo n.º 15
0
    def __hash__(self):
        r'''Hashes pitch range.

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

        Returns integer.
        '''
        from abjad.tools import systemtools
        hash_values = systemtools.StorageFormatAgent(self).get_hash_values()
        return hash(hash_values)
Exemplo n.º 16
0
 def _get_format_specification(self):
     from abjad.tools import systemtools
     agent = systemtools.StorageFormatAgent(self)
     names = agent.signature_keyword_names
     names.extend(sorted(self._kwargs))
     return systemtools.FormatSpecification(
         client=self,
         repr_is_indented=True,
         storage_format_kwargs_names=names,
     )
Exemplo n.º 17
0
 def _keyword_argument_names(self):
     from abjad.tools import systemtools
     agent = systemtools.StorageFormatAgent(self)
     keyword_argument_names = list(agent.signature_keyword_names)
     if 'client' in keyword_argument_names:
         keyword_argument_names.remove('client')
     if 'tweak_pairs' in keyword_argument_names:
         keyword_argument_names.remove('tweak_pairs')
     keyword_argument_names = tuple(keyword_argument_names)
     return keyword_argument_names
Exemplo n.º 18
0
    def compare_objects(object_one, object_two):
        r'''Compares `object_one` to `object_two`.

        Returns true or false.
        '''
        from abjad.tools import systemtools
        agent_one = systemtools.StorageFormatAgent(object_one)
        if agent_one.format_specification.coerce_for_equality:
            try:
                object_two = type(object_one)(object_two)
            except (
                TypeError,
                ValueError,
                ):
                return False
        elif not isinstance(object_two, type(object_one)):
            return False
        agent_two = systemtools.StorageFormatAgent(object_two)
        return agent_one.get_template_dict() == agent_two.get_template_dict()
Exemplo n.º 19
0
 def _get_format_specification(self):
     agent = systemtools.StorageFormatAgent(self)
     names = list(agent.signature_keyword_names)
     if 'items' in names:
         names.remove('items')
     return systemtools.FormatSpecification(
         self,
         repr_is_indented=False,
         storage_format_args_values=[self._collection],
         storage_format_kwargs_names=names,
     )
Exemplo n.º 20
0
 def _get_format_specification(self):
     agent = systemtools.StorageFormatAgent(self)
     names = agent.signature_keyword_names
     for name in ('attachments',):
         if not getattr(self, name, None) and name in names:
             names.remove(name)
     return systemtools.FormatSpecification(
         client=self,
         repr_is_indented=False,
         storage_format_kwargs_names=names,
         )
Exemplo n.º 21
0
 def _get_format_specification(self):
     from abjad.tools import systemtools
     agent = systemtools.StorageFormatAgent(self)
     names = agent.signature_keyword_names
     names.extend(self.patterns)
     names.sort()
     return systemtools.FormatSpecification(
         client=self,
         storage_format_kwargs_names=names,
         template_names=names,
     )
Exemplo n.º 22
0
    def __repr__(self):
        r'''Gets interpreter representation of nonreduced fraction.

        ::

            >>> mathtools.NonreducedFraction(3, 6)
            NonreducedFraction(3, 6)

        Returns string.
        '''
        return systemtools.StorageFormatAgent(self).get_repr_format()
Exemplo n.º 23
0
    def __format__(self, format_specification=''):
        r'''Formats registration component.

        Set `format_specification` to `''`, `'lilypond'` or `'storage'`.

        Returns string.
        '''
        from abjad.tools import systemtools
        if format_specification in ('', 'storage'):
            return systemtools.StorageFormatAgent(self).get_storage_format()
        return str(self)
Exemplo n.º 24
0
    def __format__(self, format_specification=''):
        r'''Formats woodwind fingering.

        Set `format_specification` to `''` or `'storage'`.

        Returns string.
        '''
        from abjad.tools import systemtools
        if format_specification in ('', 'storage'):
            return systemtools.StorageFormatAgent(self).get_storage_format()
        return str(self)
Exemplo n.º 25
0
 def _get_format_specification(self):
     agent = systemtools.StorageFormatAgent(self)
     names = list(agent.signature_keyword_names)
     for name in ('children', 'name', 'options'):
         if not getattr(self, name, None) and name in names:
             names.remove(name)
     return systemtools.FormatSpecification(
         client=self,
         repr_is_indented=True,
         storage_format_kwargs_names=names,
     )
Exemplo n.º 26
0
 def _format_specification(self):
     from abjad.tools import systemtools
     agent = systemtools.StorageFormatAgent(self)
     names = agent.signature_names
     for name in ('client', 'index'):
         if name in names:
             names.remove(name)
     return systemtools.FormatSpecification(
         client=self,
         storage_format_kwargs_names=names,
     )
Exemplo n.º 27
0
    def __format__(self, format_specification=''):
        r'''Formats block.

        Returns string.
        '''
        from abjad.tools import systemtools
        if format_specification in ('', 'lilypond'):
            return self._lilypond_format
        elif format_specification == 'storage':
            return systemtools.StorageFormatAgent(self).get_storage_format()
        return str(self)
Exemplo n.º 28
0
 def _get_format_specification(self):
     from abjad.tools import systemtools
     agent = systemtools.StorageFormatAgent(self)
     names = list(agent.signature_keyword_names)
     for name in names[:]:
         if not getattr(self, name):
             names.remove(name)
     return systemtools.FormatSpecification(
         client=self,
         storage_format_kwargs_names=names,
         )
Exemplo n.º 29
0
    def __repr__(self):
        r'''Gets interpreter representation of scale degree.

        ::

            >>> scale_degree
            ScaleDegree('sharp', 4)

        Returns string.
        '''
        return systemtools.StorageFormatAgent(self).get_repr_format()
Exemplo n.º 30
0
 def _get_format_specification(self):
     agent = systemtools.StorageFormatAgent(self)
     names = agent.signature_names
     template_names = names[:]
     if 'q_event_proxies' in names and not self.q_event_proxies:
         names.remove('q_event_proxies')
     return systemtools.FormatSpecification(
         client=self,
         repr_is_indented=True,
         storage_format_kwargs_names=names,
         template_names=template_names,
         )