Skip to content

Commit

Permalink
Merge from bitbucket mirror: 'gpiancastelli - Fix for issue gregmalco…
Browse files Browse the repository at this point in the history
…lm#21 and some other tweaks'
  • Loading branch information
gregmalcolm committed Mar 17, 2012
1 parent d65e690 commit 0042d79
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 63 deletions.
2 changes: 1 addition & 1 deletion python 2/koans/about_decorating_with_classes.py
Expand Up @@ -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):
Expand Down
29 changes: 14 additions & 15 deletions python 2/koans/about_iteration.py
Expand Up @@ -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()
44 changes: 26 additions & 18 deletions python 2/koans/about_with_statements.py
Expand Up @@ -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"))
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions python 3/koans/about_decorating_with_classes.py
Expand Up @@ -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"
Expand All @@ -125,4 +125,4 @@ def homer(self):
def test_we_can_chain_decorators(self):
self.assertEqual(__, self.homer())
self.assertEqual(__, self.homer.__doc__)


27 changes: 13 additions & 14 deletions python 3/koans/about_iteration.py
Expand Up @@ -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()
35 changes: 22 additions & 13 deletions python 3/koans/about_with_statements.py
Expand Up @@ -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"))
Expand Down

0 comments on commit 0042d79

Please sign in to comment.