Anonymous avatar Anonymous committed dcc104c

Implement the right try/finally idiom with files.

Comments (0)

Files changed (4)

python 2/koans/about_iteration.py

         try:
             # Files act like a collection of lines
             file = open("example_file.txt")
-    
-            def make_upcase(line):
-                return line.strip().upper()
-            upcase_lines = map(make_upcase, file.readlines())
-            self.assertEqual(__, list(upcase_lines))
+
+            try:   
+                def make_upcase(line):
+                    return line.strip().upper()
+                upcase_lines = map(make_upcase, file.readlines())
+                self.assertEqual(__, list(upcase_lines))
             
-            # NOTE: You can create your own collections that work with each,
-            # map, select, etc.
-        finally:
-            # Arg, this is ugly.
-            # We will figure out how to fix this later.
-            if file:
+                # NOTE: You can create your own collections that work with each,
+                # map, select, etc.
+            finally:
+                # Arg, this is ugly.
+                # We will figure out how to fix this later.
                 file.close()
+        except IOError:
+            # should never happen
+            self.fail()

python 2/koans/about_with_statements.py

 
 class AboutWithStatements(Koan):
     def count_lines(self, file_name):
-        file = open(file_name)
         try:
-            count = 0
-            for line in file.readlines():
-                count += 1
-            return count
-        finally:
-            if file: file.close()
+            file = open(file_name)
+            try:
+                count = 0
+                for line in file.readlines():
+                    count += 1
+                return count
+            finally:
+                file.close()
+        except IOError:
+            # should never happen
+            self.fail()
     
     def test_counting_lines(self):
         self.assertEqual(__, self.count_lines("example_file.txt"))
     # ------------------------------------------------------------------
         
     def find_line(self, file_name):
-        file = open(file_name)
         try:
-            for line in file.readlines():
-                match = re.search('e', line)
-                if match:
-                    return line
-        finally:
-            if file: file.close()
+            file = open(file_name)
+            try:
+                for line in file.readlines():
+                    match = re.search('e', line)
+                    if match:
+                        return line
+            finally:
+                file.close()
+        except IOError:
+            # should never happen
+            self.fail()
     
     def test_finding_lines(self):
         self.assertEqual(__, self.find_line("example_file.txt"))

python 3/koans/about_iteration.py

         try:
             # Files act like a collection of lines
             file = open("example_file.txt")
-    
-            def make_upcase(line):
-                return line.strip().upper()
-            upcase_lines = map(make_upcase, file.readlines())
-            self.assertEqual(__, list(upcase_lines))
+
+            try:
+                def make_upcase(line):
+                    return line.strip().upper()
+                upcase_lines = map(make_upcase, file.readlines())
+                self.assertEqual(__, list(upcase_lines))
             
-            # NOTE: You can create your own collections that work with each,
-            # map, select, etc.
-        finally:
-            # Arg, this is ugly.
-            # We will figure out how to fix this later.
-            if file:
-                file.close()
+                # NOTE: You can create your own collections that work with each,
+                # map, select, etc.
+            finally:
+                # Arg, this is ugly.
+                # We will figure out how to fix this later.
+                file.close()
+        except IOError:
+            # should never happen
+            self.fail()

python 3/koans/about_with_statements.py

 
 class AboutWithStatements(Koan):
     def count_lines(self, file_name):
-        file = open(file_name)
         try:
-            count = 0
-            for line in file.readlines():
-                count += 1
-            return count
-        finally:
-            if file: file.close()
+            file = open(file_name)
+            try:
+                count = 0
+                for line in file.readlines():
+                    count += 1
+                return count
+            finally:
+                file.close()
+        except IOError:
+            # should never happen
+            self.fail()
     
     def test_counting_lines(self):
         self.assertEqual(__, self.count_lines("example_file.txt"))
     # ------------------------------------------------------------------
         
     def find_line(self, file_name):
-        file = open(file_name)
         try:
-            for line in file.readlines():
-                match = re.search('e', line)
-                if match: return line
-        finally:
-            if file: file.close()
+            file = open(file_name)
+            try:
+                for line in file.readlines():
+                    match = re.search('e', line)
+                    if match:
+                        return line
+            finally:
+                file.close()
+        except IOError:
+            # should never happen
+            self.fail()
     
     def test_finding_lines(self):
         self.assertEqual(__, self.find_line("example_file.txt"))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.