From c4b9f6b302c3192bc0bd8b9b35978e8c958a1e83 Mon Sep 17 00:00:00 2001 From: schneefux Date: Thu, 11 Sep 2014 14:48:07 +0200 Subject: Use tempfile in client.g2p --- client/g2p.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'client') diff --git a/client/g2p.py b/client/g2p.py index d22445a..057757e 100644 --- a/client/g2p.py +++ b/client/g2p.py @@ -1,9 +1,9 @@ # -*- coding: utf-8-*- import os +import tempfile import subprocess import re -TEMP_FILENAME = "g2ptemp" PHONE_MATCH = re.compile(r' (.*) ') PHONETISAURUS_PATH = os.environ['JASPER_HOME'] + "/phonetisaurus" @@ -25,12 +25,12 @@ def translateWord(word): def translateWords(words): full_text = '\n'.join(words) - f = open(TEMP_FILENAME, "wb") - f.write(full_text) - f.flush() + with tempfile.NamedTemporaryFile(suffix='.g2p', delete=False) as f: + temp_filename = f.name + f.write(full_text) - output = translateFile(TEMP_FILENAME) - os.remove(TEMP_FILENAME) + output = translateFile(temp_filename) + os.remove(temp_filename) return output -- cgit v1.3.1 From 8e721cd3bcade4fb526d2f7a1d8b02f3fba6bdf5 Mon Sep 17 00:00:00 2001 From: schneefux Date: Thu, 11 Sep 2014 14:58:39 +0200 Subject: Use `with` statement in client.g2p --- client/g2p.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'client') diff --git a/client/g2p.py b/client/g2p.py index 057757e..3225c73 100644 --- a/client/g2p.py +++ b/client/g2p.py @@ -43,9 +43,8 @@ def translateFile(input_filename, output_filename=None): if output_filename: out = '\n'.join(out) - f = open(output_filename, "wb") - f.write(out) - f.close() + with open(output_filename, "wb") as f: + f.write(out) return None -- cgit v1.3.1