From 7f155f16c480839411e2587a57ee76f61b0e0662 Mon Sep 17 00:00:00 2001 From: Guillaume Bouchard Date: Mon, 24 May 2010 14:42:46 +0200 Subject: [PATCH] Utils: latex-preprocessing twice faster, half the code --- utils/latex-preprocessing.py | 34 ++++++++++------------------------ 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/utils/latex-preprocessing.py b/utils/latex-preprocessing.py index 5968211c..71237b88 100755 --- a/utils/latex-preprocessing.py +++ b/utils/latex-preprocessing.py @@ -1,21 +1,8 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -import re import glob -import codecs - -def replace_words(text, word_dic): - """ - take a text and replace words - that match a key in a dictionary with the associated value, - return the changed text - """ - rc = re.compile('|'.join(map(re.escape, word_dic))) - def translate(match): - return word_dic[match.group(0)] - return rc.sub(translate, text) - + # the dictionary has target_word:replacement_word pairs word_dic = { #oe inclusion @@ -51,13 +38,12 @@ word_dic = { # Process song files songfiles = glob.glob('songs/*/*.sg') -for file in songfiles: - songfile = codecs.open(file, "r", "utf-8") - data = songfile.read().encode("utf-8") - data = replace_words(data, word_dic) - songfile.close() - songfile = open(file, "w") - songfile.write(data) - songfile.close() - - +for filename in songfiles: + with open(filename, 'r+') as songfile: + data = songfile.read() + for search, replace in word_dic.items(): + data = data.replace(search, replace) + songfile.seek(0) + songfile.write(data) + songfile.truncate() +