Skip to content

Commit cb87dfc

Browse files
committed
contemplate monkey patching and with statements
1 parent ec05123 commit cb87dfc

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

koans/about_monkey_patching.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def bark(self):
1414

1515
def test_as_defined_dogs_do_bark(self):
1616
fido = self.Dog()
17-
self.assertEqual(__, fido.bark())
17+
self.assertEqual("WOOF", fido.bark())
1818

1919
# ------------------------------------------------------------------
2020

@@ -24,8 +24,8 @@ def wag(self): return "HAPPY"
2424
self.Dog.wag = wag
2525

2626
fido = self.Dog()
27-
self.assertEqual(__, fido.wag())
28-
self.assertEqual(__, fido.bark())
27+
self.assertEqual("HAPPY", fido.wag())
28+
self.assertEqual("WOOF", fido.bark())
2929

3030
# ------------------------------------------------------------------
3131

@@ -35,7 +35,7 @@ def test_most_built_in_classes_cannot_be_monkey_patched(self):
3535
except Exception as ex:
3636
err_msg = ex.args[0]
3737

38-
self.assertRegex(err_msg, __)
38+
self.assertRegex(err_msg, "can't set attributes of built-in")
3939

4040
# ------------------------------------------------------------------
4141

@@ -44,5 +44,5 @@ class MyInt(int): pass
4444
def test_subclasses_of_built_in_classes_can_be_be_monkey_patched(self):
4545
self.MyInt.is_even = lambda self: (self % 2) == 0
4646

47-
self.assertEqual(__, self.MyInt(1).is_even())
48-
self.assertEqual(__, self.MyInt(2).is_even())
47+
self.assertEqual(False, self.MyInt(1).is_even())
48+
self.assertEqual(True, self.MyInt(2).is_even())

koans/about_with_statements.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def count_lines(self, file_name):
2222
self.fail()
2323

2424
def test_counting_lines(self):
25-
self.assertEqual(__, self.count_lines("example_file.txt"))
25+
self.assertEqual(4, self.count_lines("example_file.txt"))
2626

2727
# ------------------------------------------------------------------
2828

@@ -41,7 +41,7 @@ def find_line(self, file_name):
4141
self.fail()
4242

4343
def test_finding_lines(self):
44-
self.assertEqual(__, self.find_line("example_file.txt"))
44+
self.assertEqual("test\n", self.find_line("example_file.txt"))
4545

4646
## ------------------------------------------------------------------
4747
## THINK ABOUT IT:
@@ -85,13 +85,18 @@ def count_lines2(self, file_name):
8585
return len(file.readlines())
8686

8787
def test_counting_lines2(self):
88-
self.assertEqual(__, self.count_lines2("example_file.txt"))
88+
self.assertEqual(4, self.count_lines2("example_file.txt"))
8989

9090
# ------------------------------------------------------------------
9191

9292
def find_line2(self, file_name):
9393
# Using the context manager self.FileContextManager, rewrite this
9494
# function to return the first line containing the letter 'e'.
95+
with self.FileContextManager("example_file.txt") as f:
96+
for line in f:
97+
match = re.search("e", line)
98+
if match:
99+
return line
95100
return None
96101

97102
def test_finding_lines2(self):
@@ -105,4 +110,4 @@ def count_lines3(self, file_name):
105110
return len(file.readlines())
106111

107112
def test_open_already_has_its_own_built_in_context_manager(self):
108-
self.assertEqual(__, self.count_lines3("example_file.txt"))
113+
self.assertEqual(4, self.count_lines3("example_file.txt"))

0 commit comments

Comments
 (0)