From 62e13b5b5d434857e9987a8cf7c5c3b370f87e8c Mon Sep 17 00:00:00 2001 From: John Mandereau Date: Sun, 27 Apr 2008 11:21:33 +0200 Subject: [PATCH] Update deprecated Python constructs Also fix a small amount of style nits. A lot more could be done to improve coding style of these scripts, but at least they won't break with future Python releases because of obsolete constructs. --- scripts/abc2ly.py | 56 +++++++++++++++++++++++----------------------- scripts/etf2ly.py | 17 +++++++------- scripts/midi2ly.py | 17 +++++++------- 3 files changed, 44 insertions(+), 46 deletions(-) diff --git a/scripts/abc2ly.py b/scripts/abc2ly.py index 553c400ea4..05edf98ae5 100644 --- a/scripts/abc2ly.py +++ b/scripts/abc2ly.py @@ -58,6 +58,7 @@ #TODO: # +# * coding style # * lilylib # * GNU style messages: warning:FILE:LINE: # * l10n @@ -72,7 +73,6 @@ import __main__ import getopt import sys import re -import string import os program_name = sys.argv[0] @@ -117,7 +117,7 @@ def error (msg): sys.stderr.write (msg) if global_options.strict: sys.exit (1) - + def alphabet (i): return chr (i + ord('A')) @@ -211,7 +211,7 @@ def dump_slyrics (outf): ks.sort () for k in ks: if re.match('[1-9]', k): - m = alphabet(string.atoi(k)) + m = alphabet (int (k)) else: m = k for i in range (len(slyrics[voice_idx_dict[k]])): @@ -226,7 +226,7 @@ def dump_voices (outf): ks.sort () for k in ks: if re.match ('[1-9]', k): - m = alphabet(string.atoi(k)) + m = alphabet (int (k)) else: m = k outf.write ("\nvoice%s = {" % m) @@ -245,19 +245,19 @@ def try_parse_q(a): global midi_specs #assume that Q takes the form "Q:1/4=120" #There are other possibilities, but they are deprecated - if string.count(a, '/') == 1: - array=string.split(a,'/') + if a.count ('/') == 1: + array = a.split('/') numerator=array[0] if int(numerator) != 1: sys.stderr.write("abc2ly: Warning, unable to translate a Q specification with a numerator of %s: %s\n" % (numerator, a)) - array2=string.split(array[1],'=') + array2 = array[1].split ('=') denominator=array2[0] perminute=array2[1] - duration=str(string.atoi(denominator)/string.atoi(numerator)) - midi_specs=string.join([" \n\t\t\context {\n\t\t \Score tempoWholesPerMinute = #(ly:make-moment ", perminute, " ", duration, ")\n\t\t }\n"]) + duration = str (int (denominator) / int (numerator)) + midi_specs = ' '.join([" \n\t\t\context {\n\t\t \Score tempoWholesPerMinute = #(ly:make-moment ", perminute, " ", duration, ")\n\t\t }\n"]) else: sys.stderr.write("abc2ly: Warning, unable to parse Q specification: %s\n" % a) - + def dump_score (outf): outf.write (r""" @@ -269,7 +269,7 @@ def dump_score (outf): ks.sort () for k in ks: if re.match('[1-9]', k): - m = alphabet (string.atoi(k)) + m = alphabet (int (k)) else: m = k if k == 'default' and len (voice_idx_dict) > 1: @@ -284,7 +284,7 @@ def dump_score (outf): for lyrics in slyrics [voice_idx_dict[k]]: outf.write ("\n\t\\addlyrics { \n") if re.match('[1-9]',k): - m = alphabet (string.atoi(k)) + m = alphabet (int (k)) else: m = k @@ -301,14 +301,14 @@ def set_default_length (s): global length_specified m = re.search ('1/([0-9]+)', s) if m: - __main__.default_len = string.atoi ( m.group (1)) + __main__.default_len = int ( m.group (1)) length_specified = 1 def set_default_len_from_time_sig (s): m = re.search ('([0-9]+)/([0-9]+)', s) if m: - n = string.atoi (m.group (1)) - d = string.atoi (m.group (2)) + n = int (m.group (1)) + d = int (m.group (2)) if (n * 1.0 )/(d * 1.0) < 0.75: __main__.default_len = 16 else: @@ -407,7 +407,7 @@ key_lookup = { # abc to lilypond key mode names def lily_key (k): orig = "" + k # UGR - k = string.lower (k) + k = k.lower () key = k[0] #UGH k = k[1:] @@ -465,7 +465,7 @@ key_shift = { # semitone shifts for key mode names 'dorian' : -2 } def compute_key (k): - k = string.lower (k) + k = k.lower () intkey = (ord (k[0]) - ord('a') + 5) % 7 intkeyacc =0 k = k[1:] @@ -497,7 +497,7 @@ def compute_key (k): accseq = map (lambda x: (3*x + 3 ) % 7, range (1, key_count + 1)) else: error ("Huh?") - raise "Huh" + raise Exception ("Huh") key_table = [0] * 7 for a in accseq: @@ -521,7 +521,7 @@ def try_parse_tuplet_begin (str, state): dig = str[1] str = str[2:] prev_tuplet_state = state.parsing_tuplet - state.parsing_tuplet = string.atoi (dig[0]) + state.parsing_tuplet = int (dig[0]) if prev_tuplet_state: voices_append ("}") voices_append ("\\times %s {" % tup_lookup[dig]) @@ -540,7 +540,7 @@ def header_append (key, a): header [key] = s + a def wordwrap(a, v): - linelen = len (v) - string.rfind(v, '\n') + linelen = len (v) - v.rfind ('\n') if linelen + len (a) > 80: v = v + '\n' return v + a + ' ' @@ -763,7 +763,7 @@ def parse_num (str): n = None if durstr: - n =string.atoi (durstr) + n = int (durstr) return (str,n) @@ -900,7 +900,7 @@ def try_parse_articulation (str, state): # s7m2 input doesnt care about spaces if re.match('[ \t]*\(', str): - str = string.lstrip (str) + str = str.lstrip () slur_begin =0 while str[:1] =='(' and str[1] not in DIGITS: @@ -928,9 +928,9 @@ def get_bar_acc(note, octave, state): return(UNDEF) def clear_bar_acc(state): - for k in state.in_acc.keys(): + for k in state.in_acc: del state.in_acc[k] - + # if we are parsing a beam, close it off def close_beam_state(state): @@ -961,7 +961,7 @@ def try_parse_note (str, parser_state): octave = parser_state.base_octave if str[0] in "ABCDEFG": - str = string.lower (str[0]) + str[1:] + str = str[0].lower () + str[1:] octave = octave - 1 @@ -989,7 +989,7 @@ def try_parse_note (str, parser_state): (str, num,den,current_dots) = parse_duration (str, parser_state) if re.match('[ \t]*\)', str): - str = string.lstrip (str) + str = str.lstrip () slur_end =0 while str[:1] ==')': @@ -1261,11 +1261,11 @@ def try_parse_comment (str): if 'nobarlines' in str: nobarlines = 1 elif str[0:3] == '%LY': - p = string.find(str, 'voices') + p = str.find ('voices') if (p > -1): voices_append(str[p+7:]) voices_append("\n") - p = string.find(str, 'slyrics') + p = str.find ('slyrics') if (p > -1): slyrics_append(str[p+8:]) diff --git a/scripts/etf2ly.py b/scripts/etf2ly.py index 4e2fcc7a11..5377727d80 100644 --- a/scripts/etf2ly.py +++ b/scripts/etf2ly.py @@ -30,7 +30,6 @@ import __main__ import getopt import sys import re -import string import os program_name = sys.argv[0] @@ -268,7 +267,7 @@ class Slur: if not cs or not ce: raise IndexError - cs.note_suffix = '-(' + cs.note_suffix + cs.note_suffix = '-(' + cs.note_suffix ce.note_suffix = ce.note_suffix + '-)' except IndexError: @@ -572,7 +571,7 @@ class Staff: if g.bracket == 'start': strs.append ('"0."') - str = string.join (map (lambda x: '(volta %s)' % x, strs)) + str = ' '.join (['(volta %s)' % x for x in strs]) e = e + ' \\set Score.repeatCommands = #\'(%s) ' % str @@ -811,7 +810,7 @@ Return: (value, rest-of-STR) str = str[1:] - return (string.atol (hex, 16), str) + return (long (hex, 16), str) elif str[0] == '"': str = str[1:] s = '' @@ -826,7 +825,7 @@ Return: (value, rest-of-STR) dec = dec + str[0] str = str[1:] - return (string.atoi (dec), str) + return (int (dec), str) else: sys.stderr.write ("cannot convert `%s'\n" % str) return (None, str) @@ -845,10 +844,10 @@ def parse_etf_file (fn, tag_dict): f = open (fn) gulp = re.sub ('[\n\r]+', '\n', f.read ()) - ls = string.split (gulp, '\n^') + ls = gulp.split ('\n^') etf_file_dict = {} - for k in tag_dict.keys (): + for k in tag_dict: etf_file_dict[k] = {} last_tag = None @@ -860,7 +859,7 @@ def parse_etf_file (fn, tag_dict): if m and tag_dict.has_key (m.group (1)): tag = m.group (1) - indices = tuple (map (string.atoi, string.split (m.group (2), ','))) + indices = tuple ([int (s) for s in m.group (2).split (',')]) content = l[m.end (2)+1:] @@ -1141,7 +1140,7 @@ class Etf_file: if staffs: str += '\\version "2.3.25"\n' - str = str + '<<\n %s\n>> } ' % string.join (staffs) + str = str + '<<\n %s\n>> } ' % ' '.join (staffs) return str diff --git a/scripts/midi2ly.py b/scripts/midi2ly.py index 6fb037092c..4b5f2507aa 100644 --- a/scripts/midi2ly.py +++ b/scripts/midi2ly.py @@ -1,6 +1,6 @@ #!@TARGET_PYTHON@ # -# msdi2ly.py -- LilyPond midi import script +# midi2ly.py -- LilyPond midi import script # # source file of the GNU LilyPond music typesetter # @@ -22,7 +22,6 @@ TODO: ''' import os -import string import sys """ @@ -87,7 +86,7 @@ def warning (s): def error (s): progress (_ ("error: ") + s) - raise _ ("Exiting... ") + raise Exception (_ ("Exiting... ")) def system (cmd, ignore_error = 0): return ly.system (cmd, ignore_error=ignore_error) @@ -482,12 +481,12 @@ def events_on_channel (channel): # all include ALL_NOTES_OFF elif e[1][0] >= midi.ALL_SOUND_OFF \ and e[1][0] <= midi.POLY_MODE_ON: - for i in pitches.keys (): + for i in pitches: end_note (pitches, notes, t, i) elif e[1][0] == midi.META_EVENT: if e[1][1] == midi.END_OF_TRACK: - for i in pitches.keys (): + for i in pitches: end_note (pitches, notes, t, i) break @@ -671,10 +670,10 @@ def dump_channel (thread, skip): for ch in chs: t = ch[0] - i = string.rfind (lines[-1], '\n') + 1 + i = lines[-1].rfind ('\n') + 1 if len (lines[-1][i:]) > LINE_BELL: lines.append ('') - + if t - last_t > 0: lines[-1] = lines[-1] + dump_skip (skip, t-last_t) elif t - last_t < 0: @@ -697,7 +696,7 @@ def dump_channel (thread, skip): last_t, bar_count) lines[-1] = lines[-1] + s - return string.join (lines, '\n ') + '\n' + return '\n '.join (lines) + '\n' def track_name (i): return 'track%c' % (i + ord ('A')) @@ -917,7 +916,7 @@ def do_options (): warranty () sys.exit (0) if 1: - (alterations, minor) = map (int, string.split (options.key + ':0', ':'))[0:2] + (alterations, minor) = map (int, (options.key + ':0').split (':'))[0:2] sharps = 0 flats = 0 if alterations >= 0: -- 2.39.2