Ejemplo n.º 1
0
 def __new__(class_, *arguments):
     """
     Makes new duration.
     """
     if len(arguments) == 1:
         argument = arguments[0]
         if type(argument) is class_:
             return argument
         if isinstance(argument, mathtools.NonreducedFraction):
             return Fraction.__new__(class_, *argument.pair)
         try:
             return Fraction.__new__(class_, *argument)
         except (AttributeError, TypeError):
             pass
         try:
             return Fraction.__new__(class_, argument)
         except (AttributeError, TypeError):
             pass
         if (
             isinstance(argument, tuple)
             and len(argument) == 2
             and mathtools.is_integer_equivalent(argument[0])
             and mathtools.is_integer_equivalent(argument[1])
             and not argument[1] == 0
         ):
             return Fraction.__new__(
                 class_, int(argument[0]), int(argument[1])
             )
         try:
             return Fraction.__new__(class_, argument.duration)
         except AttributeError:
             pass
         if isinstance(argument, str) and "/" not in argument:
             result = Duration._initialize_from_lilypond_duration_string(
                 argument
             )
             return Fraction.__new__(class_, result)
         if (
             isinstance(argument, tuple)
             and len(argument) == 1
             and mathtools.is_integer_equivalent(argument[0])
         ):
             return Fraction.__new__(class_, int(argument[0]))
     else:
         try:
             return Fraction.__new__(class_, *arguments)
         except TypeError:
             pass
         if mathtools.all_are_integer_equivalent_numbers(arguments):
             return Fraction.__new__(class_, *[int(x) for x in arguments])
     raise ValueError(f"can not construct duration: {arguments!r}.")
Ejemplo n.º 2
0
 def __new__(class_, *arguments):
     if len(arguments) == 1:
         argument = arguments[0]
         if type(argument) is class_:
             return argument
         if isinstance(argument, mathtools.NonreducedFraction):
             return Fraction.__new__(class_, *argument.pair)
         try:
             return Fraction.__new__(class_, *argument)
         except (AttributeError, TypeError):
             pass
         try:
             return Fraction.__new__(class_, argument)
         except (AttributeError, TypeError):
             pass
         if (isinstance(argument, tuple) and
             len(argument) == 2 and
             mathtools.is_integer_equivalent(argument[0]) and
             mathtools.is_integer_equivalent(argument[1]) and
             not argument[1] == 0):
             return Fraction.__new__(
                 class_,
                 int(argument[0]),
                 int(argument[1]),
                 )
         try:
             return Fraction.__new__(class_, argument.duration)
         except AttributeError:
             pass
         if isinstance(argument, str) and '/' not in argument:
             result = Duration._initialize_from_lilypond_duration_string(
                 argument)
             return Fraction.__new__(class_, result)
         if (isinstance(argument, tuple) and
             len(argument) == 1 and
             mathtools.is_integer_equivalent(argument[0])):
             return Fraction.__new__(class_, int(argument[0]))
     else:
         try:
             return Fraction.__new__(class_, *arguments)
         except TypeError:
             pass
         if mathtools.all_are_integer_equivalent_numbers(arguments):
             return Fraction.__new__(
                 class_,
                 *[int(x) for x in arguments]
                 )
     message = 'can not construct duration: {!r}.'
     message = message.format(arguments)
     raise ValueError(message)
Ejemplo n.º 3
0
 def _get_nth_component_in_time_order_from(self, n):
     import abjad
     assert mathtools.is_integer_equivalent(n)
     def next(component):
         if component is not None:
             for parent in abjad.inspect(component).parentage(
                 include_self=True):
                 next_sibling = parent._get_sibling(1)
                 if next_sibling is not None:
                     return next_sibling
     def previous(component):
         if component is not None:
             for parent in abjad.inspect(component).parentage(
                 include_self=True):
                 next_sibling = parent._get_sibling(-1)
                 if next_sibling is not None:
                     return next_sibling
     result = self
     if 0 < n:
         for i in range(n):
             result = next(result)
     elif n < 0:
         for i in range(abs(n)):
             result = previous(result)
     return result
Ejemplo n.º 4
0
 def _format_repeat_tremolo_command(self):
     tremolo = inspect(self).indicator(Tremolo)
     reattack_duration = self._get_tremolo_reattack_duration()
     repeat_count = self.written_duration / reattack_duration / 2
     if not mathtools.is_integer_equivalent(repeat_count):
         message = f'can not tremolo duration {self.written_duration}'
         message += f' with {tremolo.beam_count} beams.'
         raise Exception(message)
     repeat_count = int(repeat_count)
     command = r'\repeat tremolo {}'.format(repeat_count)
     return command
Ejemplo n.º 5
0
Archivo: Chord.py Proyecto: Abjad/abjad
 def _format_repeat_tremolo_command(self):
     tremolo = inspect(self).indicator(Tremolo)
     reattack_duration = self._get_tremolo_reattack_duration()
     repeat_count = self.written_duration / reattack_duration / 2
     if not mathtools.is_integer_equivalent(repeat_count):
         message = f"can not tremolo duration {self.written_duration}"
         message += f" with {tremolo.beam_count} beams."
         raise Exception(message)
     repeat_count = int(repeat_count)
     command = r"\repeat tremolo {}".format(repeat_count)
     return command
Ejemplo n.º 6
0
 def _format_repeat_tremolo_command(self):
     import abjad
     tremolo = abjad.inspect(self).indicator(abjad.Tremolo)
     reattack_duration = self._get_tremolo_reattack_duration()
     repeat_count = self.written_duration / reattack_duration / 2
     if not mathtools.is_integer_equivalent(repeat_count):
         message = 'can not tremolo duration {} with {} beams.'
         message = message.format(self.written_duration, tremolo.beam_count)
         raise Exception(message)
     repeat_count = int(repeat_count)
     command = r'\repeat tremolo {}'.format(repeat_count)
     return command
Ejemplo n.º 7
0
def all_are_integer_equivalent(argument):
    """
    Is true when ``argument`` is an iterable collection with
    integer-equivalent items.

    ..  container:: example

        >>> items = [1, '2', 3.0, abjad.Fraction(4, 1)]
        >>> abjad.mathtools.all_are_integer_equivalent(items)
        True

        >>> abjad.mathtools.all_are_integer_equivalent([1, '2', 3.5, 4])
        False

    Returns true or false.
    """
    from abjad import mathtools
    try:
        return all(mathtools.is_integer_equivalent(_) for _ in argument)
    except TypeError:
        return False
Ejemplo n.º 8
0
def is_integer_equivalent_n_tuple(argument, n):
    """
    Is true when ``argument`` is a tuple of ``n`` integer-equivalent items.

    ..  container:: example

        >>> tuple_ = (2.0, '3', abjad.Fraction(4, 1))
        >>> abjad.mathtools.is_integer_equivalent_n_tuple(tuple_, 3)
        True

        >>> tuple_ = (2.5, '3', abjad.Fraction(4, 1))
        >>> abjad.mathtools.is_integer_equivalent_n_tuple(tuple_, 3)
        False

    Returns true or false.
    """
    from abjad import mathtools
    return (
        isinstance(argument, tuple) and
        len(argument) == n and
        all(mathtools.is_integer_equivalent(_) for _ in argument)
        )
Ejemplo n.º 9
0
def all_are_integer_equivalent(argument):
    """
    Is true when ``argument`` is an iterable collection with
    integer-equivalent items.

    ..  container:: example

        >>> items = [1, '2', 3.0, abjad.Fraction(4, 1)]
        >>> abjad.mathtools.all_are_integer_equivalent(items)
        True

        >>> abjad.mathtools.all_are_integer_equivalent([1, '2', 3.5, 4])
        False

    Returns true or false.
    """
    from abjad import mathtools

    try:
        return all(mathtools.is_integer_equivalent(_) for _ in argument)
    except TypeError:
        return False
Ejemplo n.º 10
0
def is_integer_equivalent_n_tuple(argument, n):
    """
    Is true when ``argument`` is a tuple of ``n`` integer-equivalent items.

    ..  container:: example

        >>> tuple_ = (2.0, '3', abjad.Fraction(4, 1))
        >>> abjad.mathtools.is_integer_equivalent_n_tuple(tuple_, 3)
        True

        >>> tuple_ = (2.5, '3', abjad.Fraction(4, 1))
        >>> abjad.mathtools.is_integer_equivalent_n_tuple(tuple_, 3)
        False

    Returns true or false.
    """
    from abjad import mathtools

    return (
        isinstance(argument, tuple)
        and len(argument) == n
        and all(mathtools.is_integer_equivalent(_) for _ in argument)
    )