From 52301794f7219bc583040449a0319e245d3b71b2 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Tue, 4 Sep 2007 14:44:14 +0000 Subject: [PATCH] cleanup keys/has_key usage --- oxdbarchive/model.py | 2 +- oxdbarchive/subtitles.py | 12 ++++++------ oxdbarchive/timeline.py | 2 +- oxdbarchive/tools/shift_srt.py | 7 +++++-- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/oxdbarchive/model.py b/oxdbarchive/model.py index d6c9536..3753366 100644 --- a/oxdbarchive/model.py +++ b/oxdbarchive/model.py @@ -181,7 +181,7 @@ class Archive(SQLObject): for f in files: meta = oxdb_import.oxdb_file_stats(f) f = f.replace(self.basePath, '') - if oxdb_files.has_key(f) and oxdb_files[f]['size'] == meta['size']: + if f in oxdb_files and oxdb_files[f]['size'] == meta['size']: stats['skipped'] += 1 md5sum_on_disk.append(oxdb_files[f]['md5sum']) else: diff --git a/oxdbarchive/subtitles.py b/oxdbarchive/subtitles.py index 30d6101..320e997 100644 --- a/oxdbarchive/subtitles.py +++ b/oxdbarchive/subtitles.py @@ -15,7 +15,7 @@ img_extension = 'jpg' def srt2txt(srt, encoding = "utf-8"): subtitles = srt2dict(srt, encoding) txt = '' - for k in sorted([int(k) for k in subtitles.keys()]): + for k in sorted([int(k) for k in subtitles]): txt += "%s\n\n" % subtitles["%s" % k]['text'] return txt.strip() @@ -49,7 +49,7 @@ def dict2srt(subtitles, encoding = "utf-8"): into an srt file ''' srt = '' - for k in sorted([int(k) for k in subtitles.keys()]): + for k in sorted([int(k) for k in subtitles]): k = "%s" % k srt += "%s\r\n%s --> %s\r\n%s\r\n\r\n" % ( k, @@ -91,7 +91,7 @@ def shift_subtitles(offset, offset_num, subtitles): shifts a subtitle by offset, where offsest is a tuple (time, position) ''' sdict = {} - for k in sorted([int(k) for k in subtitles.keys()]): + for k in sorted([int(k) for k in subtitles]): ko = "%s" % (k + offset_num) sdict[ko] = subtitles["%s" % k] sdict[ko]['start'] = shift_time(offset, sdict[ko]['start']) @@ -105,7 +105,7 @@ def merge_subtitles(subtitles): ''' subs = {} offset = 0 - for k in sorted(subtitles.keys()): + for k in sorted(subtitles): sdict = srt2dict(subtitles[k]['txt']) if offset: sdict = shift_subtitles(offset, len(subs), sdict) @@ -121,7 +121,7 @@ def split_subtitle(subtitles, offset): offset_time = time.strftime("%H:%M:%S", offset) one = {} two = {} - for k in sorted([int(k) for k in subtitles.keys()]): + for k in sorted([int(k) for k in subtitles]): if subtitles['stop'] < offset_time: one[k] = subtitle[k] else: @@ -238,7 +238,7 @@ def extract_poster_still(movie_file, png_file, inpoint): def extract_subtitles(movie_file, srt, img_folder, width=128, offset = 0, redo = False): subtitles = srt2dict(srt) - for k in sorted([int(k) for k in subtitles.keys()]): + for k in sorted([int(k) for k in subtitles]): timestamp = subtitles["%s" % k]['start'] extract_frame(movie_file, timestamp, img_folder, width, offset, redo) diff --git a/oxdbarchive/timeline.py b/oxdbarchive/timeline.py index 9e99788..a35d60e 100644 --- a/oxdbarchive/timeline.py +++ b/oxdbarchive/timeline.py @@ -91,7 +91,7 @@ def loadTimelineImageMap(movie): length = int(movie.length / 1000) imageMap ='' - for key in sorted([int(k) for k in s.keys()]): + for key in sorted([int(k) for k in s]): sub = s["%s" % key] start = int(round(time_str2msec(sub['start']) / 1000)) stop = int(round(time_str2msec(sub['stop']) / 1000)) diff --git a/oxdbarchive/tools/shift_srt.py b/oxdbarchive/tools/shift_srt.py index 170b81f..f6328ad 100755 --- a/oxdbarchive/tools/shift_srt.py +++ b/oxdbarchive/tools/shift_srt.py @@ -32,7 +32,7 @@ def dict2srt(subtitles, encoding = "latin-1"): into an srt file ''' srt = '' - for k in sorted([int(k) for k in subtitles.keys()]): + for k in sorted([int(k) for k in subtitles]): k = "%s" % k srt += "%s\r\n%s --> %s\r\n%s\r\n\r\n" % ( k, @@ -72,7 +72,7 @@ def shift_subtitles(offset, offset_num, subtitles): shifts a subtitle by offset ''' sdict = {} - for k in sorted([int(k) for k in subtitles.keys()]): + for k in sorted([int(k) for k in subtitles]): ko = "%s" % (k + offset_num) sdict[ko] = subtitles["%s" % k] sdict[ko]['start'] = shift_time(offset, sdict[ko]['start']) @@ -80,6 +80,9 @@ def shift_subtitles(offset, offset_num, subtitles): return sdict if __name__ == '__main__': + if len(sys.argv) != 3: + print """\nusage: %s movie.srt offset(in milliseconds)\n""" % sys.argv[0] + sys.exit(1) srt = open(sys.argv[1]).read() srtd = srt2dict(srt) offset = int(sys.argv[2])