From c5f2e5d07a66846b8251d771eb4a7178c6f03bfb Mon Sep 17 00:00:00 2001 From: Jonas Hahnfeld Date: Sat, 21 Sep 2019 13:45:47 +0200 Subject: [PATCH 4/4] musicxml2ly: Fix integer calculations for Python 3 Python 3 changed the semantics of / to result in a floating point value, use // to get a (truncated) integer. --- python/musicexp.py | 6 +++--- python/musicxml.py | 6 +++--- python/rational.py | 13 ++++++++++--- scripts/musicxml2ly.py | 20 ++++++++++---------- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/python/musicexp.py b/python/musicexp.py index 01faa1b75d..5805827243 100644 --- a/python/musicexp.py +++ b/python/musicexp.py @@ -396,7 +396,7 @@ class Pitch: while c.step < 0: c.step += 7 c.octave -= 1 - c.octave += c.step / 7 + c.octave += c.step // 7 c.step = c.step % 7 def lisp_expression (self): @@ -461,9 +461,9 @@ class Pitch: pitch_diff = (this_pitch_steps - previous_pitch_steps) previous_pitch = self if pitch_diff > 3: - return "'" * ((pitch_diff + 3) / 7) + return "'" * ((pitch_diff + 3) // 7) elif pitch_diff < -3: - return "," * ((-pitch_diff + 3) / 7) + return "," * ((-pitch_diff + 3) // 7) else: return "" diff --git a/python/musicxml.py b/python/musicxml.py index 0d58983715..fdf6e24c0e 100644 --- a/python/musicxml.py +++ b/python/musicxml.py @@ -957,7 +957,7 @@ class Syllabic(Music_xml_node): class Lyric(Music_xml_node): - def number(self): + def get_number(self): """ Return the number attribute(if it exists) of the lyric element. @@ -965,7 +965,7 @@ class Lyric(Music_xml_node): @return: The value of the number attribute """ if hasattr(self, 'number'): - return self.number + return int(self.number) else: return -1 @@ -1239,7 +1239,7 @@ class Musicxml_voice: self.has_lyrics = len(lyrics) > 0 for l in lyrics: - nr = l.number + nr = l.get_number() if(nr > 0) and not(nr in self._lyrics): self._lyrics.append(nr) diff --git a/python/rational.py b/python/rational.py index 4a6eaa5823..c43f0a5576 100644 --- a/python/rational.py +++ b/python/rational.py @@ -3,6 +3,7 @@ import math as _math +from functools import total_ordering def _gcf(a, b): """Returns the greatest common factor of a and b.""" @@ -12,6 +13,7 @@ def _gcf(a, b): a, b = b, a % b return a +@total_ordering class Rational(object): """ This class provides an exact representation of rational numbers. @@ -161,11 +163,16 @@ class Rational(object): return other - other // self * self def __divmod__(self, other): return self // other, self % other - def __cmp__(self, other): + def __eq__(self, other): if other == 0: - return cmp(self._n, 0) + return self._n == 0 else: - return cmp(self - other, 0) + return (self - other)._n == 0 + def __lt__(self, other): + if other == 0: + return self._n < 0 + else: + return (self - other) < 0 def __pow__(self, other): if isinstance(other, int): if other < 0: diff --git a/scripts/musicxml2ly.py b/scripts/musicxml2ly.py index e5584f6851..4be0f617c5 100755 --- a/scripts/musicxml2ly.py +++ b/scripts/musicxml2ly.py @@ -73,7 +73,7 @@ additional_definitions = { } def round_to_two_digits(val): - return round(val * 100) / 100 + return round(val * 100) // 100 def extract_paper_information(score_partwise): defaults = score_partwise.get_maybe_exist_named_child('defaults') @@ -856,7 +856,7 @@ def musicxml_transpose_to_lily(attributes): (3, 0), (3, 1), (4, 0), (5, -1), (5, 0), (6, -1), (6, 0)][chromatic_shift_normalized]; - shift.octave += (chromatic_shift - chromatic_shift_normalized) / 12 + shift.octave += (chromatic_shift - chromatic_shift_normalized) // 12 diatonic = transpose.get_maybe_exist_named_child('diatonic') if diatonic: @@ -1256,7 +1256,7 @@ def hexcolorval_to_nr(hex_val): v = int(hex_val, 16) if v == 255: v = 256 - return v / 256. + return v // 256. except ValueError: return 0. @@ -1684,8 +1684,8 @@ def musicxml_get_string_tunings(lines): string_tunings = [musicexp.Pitch()] * lines for i in range(0, lines): p = musicexp.Pitch() - p.step = musicxml2ly_conversion.musicxml_step_to_lily(((("E","A","D","G","B")*(lines/5+1))[0:lines])[i]) - p.octave = (([-2+int(x%5>1)+2*(x/5) for x in range(0,lines)][0:lines])[i]) + p.step = musicxml2ly_conversion.musicxml_step_to_lily(((("E","A","D","G","B")*(lines//5+1))[0:lines])[i]) + p.octave = (([-2+int(x%5>1)+2*(x//5) for x in range(0,lines)][0:lines])[i]) p.alteration = 0 p._force_absolute_pitch = True string_tunings[i] = p @@ -1933,7 +1933,7 @@ class LilyPondVoiceBuilder: r = musicexp.MultiMeasureRest() lenfrac = self.measure_length r.duration = musicxml2ly_conversion.rational_to_lily_duration(lenfrac) - r.duration.factor *= self.pending_multibar / lenfrac + r.duration.factor *= self.pending_multibar // lenfrac self.elements.append(r) self.begin_moment = self.end_moment self.end_moment = self.begin_moment + self.pending_multibar @@ -2130,7 +2130,7 @@ def get_all_lyric_parts_in_voice(voice): lyrics = elem.get_typed_children(musicxml.Lyric) if lyrics: for lyric in lyrics: - index = lyric.number + index = lyric.get_number() if not index in all_lyric_parts: all_lyric_parts.append(index) return all_lyric_parts @@ -2157,17 +2157,17 @@ def extract_lyrics(voice, lyric_key, lyrics_dict): def has_lyric_belonging_to_lyric_part(note, lyric_part_id): lyric_elements = get_lyric_elements(note) - lyric_numbers = [lyric.number for lyric in lyric_elements] + lyric_numbers = [lyric.get_number() for lyric in lyric_elements] return any([lyric_number == lyric_part_id for lyric_number in lyric_numbers]) for idx, elem in enumerate(voice._elements): lyrics = get_lyric_elements(elem) - lyric_keys = [lyric.number for lyric in lyrics] + lyric_keys = [lyric.get_number() for lyric in lyrics] note_has_lyric_belonging_to_lyric_part = lyric_key in lyric_keys # Current note has lyric with 'number' matching 'lyric_key'. if note_has_lyric_belonging_to_lyric_part: for lyric in lyrics: - if lyric.number == lyric_key: + if lyric.get_number() == lyric_key: text = musicxml_lyrics_to_text(lyric, None) result.append(text) # Note has any lyric. -- 2.23.0