summary refs log tree commit diff
path: root/languageupdater.py
diff options
context:
space:
mode:
authorClayton G. Hobbs <clay@lakeserv.net>2016-02-04 12:58:18 -0500
committerClayton G. Hobbs <clay@lakeserv.net>2016-02-04 12:58:18 -0500
commit9b9cc013ab324b98fcb3ec883c97d57df232a808 (patch)
treee46da9be20dd422b70910ecbb50fcb9a84db9f19 /languageupdater.py
parentd36b65890a925c3de87dfc103b4e2279d9b33eb1 (diff)
Open all files using with blocks
I truly am a modern Python man.
Diffstat (limited to 'languageupdater.py')
-rw-r--r--languageupdater.py14
1 files changed, 8 insertions, 6 deletions
diff --git a/languageupdater.py b/languageupdater.py
index 035bee4..98397c7 100644
--- a/languageupdater.py
+++ b/languageupdater.py
@@ -47,17 +47,19 @@ class LanguageUpdater:
         host = 'http://www.speech.cs.cmu.edu'
         url = host + '/cgi-bin/tools/lmtool/run'
 
-        # Prepare request
-        files = {'corpus': open(self.config.strings_file, 'rb')}
-        values = {'formtype': 'simple'}
+        # Submit the corpus to the lmtool
+        response_text = ""
+        with open(self.config.strings_file, 'rb') as corpus:
+            files = {'corpus': corpus}
+            values = {'formtype': 'simple'}
 
-        # Send corpus to the server
-        r = requests.post(url, files=files, data=values)
+            r = requests.post(url, files=files, data=values)
+            response_text = r.text
 
         # Parse response to get URLs of the files we need
         path_re = r'.*<title>Index of (.*?)</title>.*'
         number_re = r'.*TAR([0-9]*?)\.tgz.*'
-        for line in r.text.split('\n'):
+        for line in response_text.split('\n'):
             # If we found the directory, keep it and don't break
             if re.search(path_re, line):
                 path = host + re.sub(path_re, r'\1', line)