Sfoglia il codice sorgente

Open all files using with blocks

I truly am a modern Python man.
Clara Hobbs 8 anni fa
parent
commit
9b9cc013ab
2 ha cambiato i file con 18 aggiunte e 20 eliminazioni
  1. 10
    14
      kaylee.py
  2. 8
    6
      languageupdater.py

+ 10
- 14
kaylee.py Vedi File

73
 
73
 
74
     def create_strings_file(self):
74
     def create_strings_file(self):
75
         # Open the strings file
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
     def log_history(self, text):
84
     def log_history(self, text):
87
         if self.options['history']:
85
         if self.options['history']:
91
                 self.history.pop(0)
89
                 self.history.pop(0)
92
 
90
 
93
             # Open and truncate the history file
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
     def run_command(self, cmd):
96
     def run_command(self, cmd):
101
         """Print the command, then run it"""
97
         """Print the command, then run it"""

+ 8
- 6
languageupdater.py Vedi File

47
         host = 'http://www.speech.cs.cmu.edu'
47
         host = 'http://www.speech.cs.cmu.edu'
48
         url = host + '/cgi-bin/tools/lmtool/run'
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
         # Parse response to get URLs of the files we need
59
         # Parse response to get URLs of the files we need
58
         path_re = r'.*<title>Index of (.*?)</title>.*'
60
         path_re = r'.*<title>Index of (.*?)</title>.*'
59
         number_re = r'.*TAR([0-9]*?)\.tgz.*'
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
             # If we found the directory, keep it and don't break
63
             # If we found the directory, keep it and don't break
62
             if re.search(path_re, line):
64
             if re.search(path_re, line):
63
                 path = host + re.sub(path_re, r'\1', line)
65
                 path = host + re.sub(path_re, r'\1', line)

Loading…
Annulla
Salva