Browse Source

Open all files using with blocks

I truly am a modern Python man.
Clara Hobbs 8 years ago
parent
commit
9b9cc013ab
2 changed files with 18 additions and 20 deletions
  1. 10
    14
      kaylee.py
  2. 8
    6
      languageupdater.py

+ 10
- 14
kaylee.py View File

@@ -73,15 +73,13 @@ class Kaylee:
73 73
 
74 74
     def create_strings_file(self):
75 75
         # Open the strings file
76
-        strings = open(self.config.strings_file, "w")
77
-        # Add command words to the corpus
78
-        for voice_cmd in sorted(self.commands.keys()):
79
-            strings.write(voice_cmd.strip().replace('%d', '') + "\n")
80
-        # Add number words to the corpus
81
-        for word in self.number_parser.number_words:
82
-            strings.write(word + "\n")
83
-        # Close the strings file
84
-        strings.close()
76
+        with open(self.config.strings_file, 'w') as strings:
77
+            # Add command words to the corpus
78
+            for voice_cmd in sorted(self.commands.keys()):
79
+                strings.write(voice_cmd.strip().replace('%d', '') + "\n")
80
+            # Add number words to the corpus
81
+            for word in self.number_parser.number_words:
82
+                strings.write(word + "\n")
85 83
 
86 84
     def log_history(self, text):
87 85
         if self.options['history']:
@@ -91,11 +89,9 @@ class Kaylee:
91 89
                 self.history.pop(0)
92 90
 
93 91
             # Open and truncate the history file
94
-            hfile = open(self.config.history_file, "w")
95
-            for line in self.history:
96
-                hfile.write(line + "\n")
97
-            # Close the file
98
-            hfile.close()
92
+            with open(self.config.history_file, 'w') as hfile:
93
+                for line in self.history:
94
+                    hfile.write(line + '\n')
99 95
 
100 96
     def run_command(self, cmd):
101 97
         """Print the command, then run it"""

+ 8
- 6
languageupdater.py View File

@@ -47,17 +47,19 @@ class LanguageUpdater:
47 47
         host = 'http://www.speech.cs.cmu.edu'
48 48
         url = host + '/cgi-bin/tools/lmtool/run'
49 49
 
50
-        # Prepare request
51
-        files = {'corpus': open(self.config.strings_file, 'rb')}
52
-        values = {'formtype': 'simple'}
50
+        # Submit the corpus to the lmtool
51
+        response_text = ""
52
+        with open(self.config.strings_file, 'rb') as corpus:
53
+            files = {'corpus': corpus}
54
+            values = {'formtype': 'simple'}
53 55
 
54
-        # Send corpus to the server
55
-        r = requests.post(url, files=files, data=values)
56
+            r = requests.post(url, files=files, data=values)
57
+            response_text = r.text
56 58
 
57 59
         # Parse response to get URLs of the files we need
58 60
         path_re = r'.*<title>Index of (.*?)</title>.*'
59 61
         number_re = r'.*TAR([0-9]*?)\.tgz.*'
60
-        for line in r.text.split('\n'):
62
+        for line in response_text.split('\n'):
61 63
             # If we found the directory, keep it and don't break
62 64
             if re.search(path_re, line):
63 65
                 path = host + re.sub(path_re, r'\1', line)

Loading…
Cancel
Save