Permalink
Browse files

Merge from bitbucket mirror: 'gpiancastelli - Fix for issue #21 and s…

…ome other tweaks'
  • Loading branch information...
1 parent d65e690 commit 0042d792ebc691db3023c576f52efaa28bf4a3bb @gregmalcolm gregmalcolm committed Mar 17, 2012
@@ -102,7 +102,7 @@ def count_badly(self, num):
if num == 3:
return 5
else:
- num
+ return num
@documenter("Does nothing")
def idler(self, num):
@@ -108,18 +108,17 @@ def test_all_iteration_methods_work_on_any_sequence_not_just_lists(self):
self.assertEqual(__, list(result))
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))
-
- # 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()
+ f = open("example_file.txt")
+
+ try:
+ def make_upcase(line):
+ return line.strip().upper()
+ upcase_lines = map(make_upcase, f.readlines())
+ self.assertEqual(__, list(upcase_lines))
+ finally:
+ # Arg, this is ugly.
+ # We will figure out how to fix this later.
+ f.close()
+ except IOError:
+ # should never happen
+ self.fail()
@@ -12,29 +12,37 @@
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()
+ f = open(file_name)
+ try:
+ count = 0
+ for line in f.readlines():
+ count += 1
+ return count
+ finally:
+ f.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()
+ f = open(file_name)
+ try:
+ for line in f.readlines():
+ match = re.search('e', line)
+ if match:
+ return line
+ finally:
+ f.close()
+ except IOError:
+ # should never happen
+ self.fail()
def test_finding_lines(self):
self.assertEqual(__, self.find_line("example_file.txt"))
@@ -77,9 +85,9 @@ def __exit__(self, cls, value, tb):
# Now we write:
def count_lines2(self, file_name):
- with self.FileContextManager(file_name) as file:
+ with self.FileContextManager(file_name) as f:
count = 0
- for line in file.readlines():
+ for line in f.readlines():
count += 1
return count
@@ -99,9 +107,9 @@ def test_finding_lines2(self):
# ------------------------------------------------------------------
def count_lines3(self, file_name):
- with open(file_name) as file:
+ with open(file_name) as f:
count = 0
- for line in file.readlines():
+ for line in f.readlines():
count += 1
return count
@@ -101,7 +101,7 @@ def count_badly(self, num):
if num==3:
return 5
else:
- num
+ return num
@documenter("Does nothing")
def idler(self, num):
"Idler"
@@ -125,4 +125,4 @@ def homer(self):
def test_we_can_chain_decorators(self):
self.assertEqual(__, self.homer())
self.assertEqual(__, self.homer.__doc__)
-
+
@@ -129,18 +129,17 @@ def test_all_iteration_methods_work_on_any_sequence_not_just_lists(self):
self.assertEqual(__, list(result))
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))
-
- # 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()
+
+ try:
+ def make_upcase(line):
+ return line.strip().upper()
+ upcase_lines = map(make_upcase, file.readlines())
+ self.assertEqual(__, list(upcase_lines))
+ finally:
+ # Arg, this is ugly.
+ # We will figure out how to fix this later.
+ file.close()
+ except IOError:
+ # should never happen
+ self.fail()
@@ -11,28 +11,37 @@
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"))

0 comments on commit 0042d79

Please sign in to comment.