Skip to content

Commit aa94d5e

Browse files
committed
Making the answers available on a git branch
1 parent ad6e64c commit aa94d5e

File tree

67 files changed

+1014
-865
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1014
-865
lines changed

python 2/koans/about_asserts.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,33 @@ def test_assert_truth(self):
99
"""
1010
We shall contemplate truth by testing reality, via asserts.
1111
"""
12-
self.assertTrue(False) # This should be true
12+
self.assertTrue(True) # This should be true
1313

1414
def test_assert_with_message(self):
1515
"""
1616
Enlightenment may be more easily achieved with appropriate messages.
1717
"""
18-
self.assertTrue(False, "This should be true -- Please fix this")
18+
self.assertTrue(True, "This should be true -- Please fix this")
1919

2020
def test_fill_in_values(self):
2121
"""
2222
Sometimes we will ask you to fill in the values
2323
"""
24-
self.assertEqual(__, 1 + 1)
24+
self.assertEqual(2, 1 + 1)
2525

2626
def test_assert_equality(self):
2727
"""
2828
To understand reality, we must compare our expectations against reality.
2929
"""
30-
expected_value = __
30+
expected_value = 2
3131
actual_value = 1 + 1
3232
self.assertTrue(expected_value == actual_value)
3333

3434
def test_a_better_way_of_asserting_equality(self):
3535
"""
3636
Some ways of asserting equality are better than others.
3737
"""
38-
expected_value = __
38+
expected_value = 2
3939
actual_value = 1 + 1
4040

4141
self.assertEqual(expected_value, actual_value)
@@ -46,5 +46,5 @@ def test_that_unittest_asserts_work_the_same_way_as_python_asserts(self):
4646
"""
4747

4848
# This throws an AssertionError exception
49-
assert False
49+
assert True
5050

python 2/koans/about_attribute_access.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ def test_calling_undefined_functions_normally_results_in_errors(self):
1818
try:
1919
typical.foobar()
2020
except Exception as exception:
21-
self.assertEqual(__, type(exception).__name__)
22-
self.assertMatch(__, exception[0])
21+
self.assertEqual('AttributeError', type(exception).__name__)
22+
self.assertMatch("'TypicalObject' object has no attribute 'foobar'", exception[0])
2323

2424
def test_calling_getattribute_causes_an_attribute_error(self):
2525
typical = self.TypicalObject()
2626

2727
try:
2828
typical.__getattribute__('foobar')
2929
except AttributeError as exception:
30-
self.assertMatch(__, exception[0])
30+
self.assertMatch("'TypicalObject' object has no attribute 'foobar'", exception[0])
3131

3232
# THINK ABOUT IT:
3333
#
@@ -43,17 +43,17 @@ def __getattribute__(self, attr_name):
4343
def test_all_attribute_reads_are_caught(self):
4444
catcher = self.CatchAllAttributeReads()
4545

46-
self.assertMatch(__, catcher.foobar)
46+
self.assertMatch("Someone called 'foobar' and it could not be found", catcher.foobar)
4747

4848
def test_intercepting_return_values_can_disrupt_the_call_chain(self):
4949
catcher = self.CatchAllAttributeReads()
5050

51-
self.assertMatch(__, catcher.foobaz) # This is fine
51+
self.assertMatch("Someone called 'foobaz' and it could not be found", catcher.foobaz) # This is fine
5252

5353
try:
5454
catcher.foobaz(1)
5555
except TypeError as ex:
56-
self.assertMatch(__, ex[0])
56+
self.assertMatch("'str' object is not callable", ex[0])
5757

5858
# foobaz returns a string. What happens to the '(1)' part?
5959
# Try entering this into a python console to reproduce the issue:
@@ -64,7 +64,7 @@ def test_intercepting_return_values_can_disrupt_the_call_chain(self):
6464
def test_changes_to_the_getattribute_implementation_affects_getattr_function(self):
6565
catcher = self.CatchAllAttributeReads()
6666

67-
self.assertMatch(__, getattr(catcher, 'any_attribute'))
67+
self.assertMatch("Someone called 'any_attribute' and it could not be found", getattr(catcher, 'any_attribute'))
6868

6969
# ------------------------------------------------------------------
7070

@@ -79,16 +79,16 @@ def __getattribute__(self, attr_name):
7979
def test_foo_attributes_are_caught(self):
8080
catcher = self.WellBehavedFooCatcher()
8181

82-
self.assertEqual(__, catcher.foo_bar)
83-
self.assertEqual(__, catcher.foo_baz)
82+
self.assertEqual('Foo to you too', catcher.foo_bar)
83+
self.assertEqual('Foo to you too', catcher.foo_baz)
8484

8585
def test_non_foo_messages_are_treated_normally(self):
8686
catcher = self.WellBehavedFooCatcher()
8787

8888
try:
8989
catcher.normal_undefined_attribute
9090
except AttributeError as ex:
91-
self.assertMatch(__, ex[0])
91+
self.assertMatch("'WellBehavedFooCatcher' object has no attribute 'normal_undefined_attribute'", ex[0])
9292

9393
# ------------------------------------------------------------------
9494

@@ -125,7 +125,7 @@ def test_getattribute_is_a_bit_overzealous_sometimes(self):
125125
catcher = self.RecursiveCatcher()
126126
catcher.my_method()
127127
global stack_depth
128-
self.assertEqual(__, stack_depth)
128+
self.assertEqual(11, stack_depth)
129129

130130
# ------------------------------------------------------------------
131131

@@ -146,17 +146,17 @@ def test_getattr_ignores_known_attributes(self):
146146
catcher = self.MinimalCatcher()
147147
catcher.my_method()
148148

149-
self.assertEqual(__, catcher.no_of_getattr_calls)
149+
self.assertEqual(0, catcher.no_of_getattr_calls)
150150

151151
def test_getattr_only_catches_unknown_attributes(self):
152152
catcher = self.MinimalCatcher()
153153
catcher.purple_flamingos()
154154
catcher.free_pie()
155155

156-
self.assertEqual(__,
156+
self.assertEqual('DuffObject',
157157
type(catcher.give_me_duff_or_give_me_death()).__name__)
158158

159-
self.assertEqual(__, catcher.no_of_getattr_calls)
159+
self.assertEqual(3, catcher.no_of_getattr_calls)
160160

161161
# ------------------------------------------------------------------
162162

@@ -177,9 +177,9 @@ def test_setattr_intercepts_attribute_assignments(self):
177177
fanboy.comic = 'The Laminator, issue #1'
178178
fanboy.pie = 'blueberry'
179179

180-
self.assertEqual(__, fanboy.a_pie)
180+
self.assertEqual('blueberry', fanboy.a_pie)
181181

182-
prefix = '__'
182+
prefix = 'my'
183183
self.assertEqual("The Laminator, issue #1", getattr(fanboy, prefix + '_comic'))
184184

185185
# ------------------------------------------------------------------
@@ -201,17 +201,17 @@ def test_it_modifies_external_attribute_as_expected(self):
201201
setter = self.ScarySetter()
202202
setter.e = "mc hammer"
203203

204-
self.assertEqual(__, setter.altered_e)
204+
self.assertEqual("mc hammer", setter.altered_e)
205205

206206
def test_it_mangles_some_internal_attributes(self):
207207
setter = self.ScarySetter()
208208

209209
try:
210210
coconuts = setter.num_of_coconuts
211211
except AttributeError:
212-
self.assertEqual(__, setter.altered_num_of_coconuts)
212+
self.assertEqual(9, setter.altered_num_of_coconuts)
213213

214214
def test_in_this_case_private_attributes_remain_unmangled(self):
215215
setter = self.ScarySetter()
216216

217-
self.assertEqual(__, setter._num_of_private_coconuts)
217+
self.assertEqual(2, setter._num_of_private_coconuts)

python 2/koans/about_class_attributes.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,36 @@ def test_new_style_class_objects_are_objects(self):
1616
# phased out it Python 3.
1717

1818
fido = self.Dog()
19-
self.assertEqual(__, isinstance(fido, object))
19+
self.assertEqual(True, isinstance(fido, object))
2020

2121
def test_classes_are_types(self):
22-
self.assertEqual(__, self.Dog.__class__ == type)
22+
self.assertEqual(True, self.Dog.__class__ == type)
2323

2424
def test_classes_are_objects_too(self):
25-
self.assertEqual(__, issubclass(self.Dog, object))
25+
self.assertEqual(True, issubclass(self.Dog, object))
2626

2727
def test_objects_have_methods(self):
2828
fido = self.Dog()
29-
self.assertEqual(__, len(dir(fido)))
29+
self.assertEqual(18, len(dir(fido)))
3030

3131
def test_classes_have_methods(self):
32-
self.assertEqual(__, len(dir(self.Dog)))
32+
self.assertEqual(18, len(dir(self.Dog)))
3333

3434
def test_creating_objects_without_defining_a_class(self):
3535
singularity = object()
36-
self.assertEqual(__, len(dir(singularity)))
36+
self.assertEqual(15, len(dir(singularity)))
3737

3838
def test_defining_attributes_on_individual_objects(self):
3939
fido = self.Dog()
4040
fido.legs = 4
4141

42-
self.assertEqual(__, fido.legs)
42+
self.assertEqual(4, fido.legs)
4343

4444
def test_defining_functions_on_individual_objects(self):
4545
fido = self.Dog()
4646
fido.wag = lambda : 'fidos wag'
4747

48-
self.assertEqual(__, fido.wag())
48+
self.assertEqual('fidos wag', fido.wag())
4949

5050
def test_other_objects_are_not_affected_by_these_singleton_functions(self):
5151
fido = self.Dog()
@@ -58,7 +58,7 @@ def wag():
5858
try:
5959
rover.wag()
6060
except Exception as ex:
61-
self.assertMatch(__, ex[0])
61+
self.assertMatch("'Dog' object has no attribute 'wag'", ex[0])
6262

6363
# ------------------------------------------------------------------
6464

@@ -81,19 +81,19 @@ def growl(cls):
8181
return "classmethod growl, arg: cls=" + cls.__name__
8282

8383
def test_since_classes_are_objects_you_can_define_singleton_methods_on_them_too(self):
84-
self.assertMatch(__, self.Dog2.growl())
84+
self.assertMatch('classmethod growl, arg: cls=Dog2', self.Dog2.growl())
8585

8686
def test_classmethods_are_not_independent_of_instance_methods(self):
8787
fido = self.Dog2()
88-
self.assertMatch(__, fido.growl())
89-
self.assertMatch(__, self.Dog2.growl())
88+
self.assertMatch('classmethod growl, arg: cls=Dog2', fido.growl())
89+
self.assertMatch('classmethod growl, arg: cls=Dog2', self.Dog2.growl())
9090

9191
def test_staticmethods_are_unbound_functions_housed_in_a_class(self):
92-
self.assertMatch(__, self.Dog2.bark())
92+
self.assertMatch('staticmethod bark, arg: None', self.Dog2.bark())
9393

9494
def test_staticmethods_also_overshadow_instance_methods(self):
9595
fido = self.Dog2()
96-
self.assertMatch(__, fido.bark())
96+
self.assertMatch('staticmethod bark, arg: None', fido.bark())
9797

9898
# ------------------------------------------------------------------
9999

@@ -123,20 +123,20 @@ def test_classmethods_can_not_be_used_as_properties(self):
123123
try:
124124
fido.name = "Fido"
125125
except Exception as ex:
126-
self.assertMatch(__, ex[0])
126+
self.assertMatch("'classmethod' object is not callable", ex[0])
127127

128128
def test_classes_and_instances_do_not_share_instance_attributes(self):
129129
fido = self.Dog3()
130130
fido.set_name_from_instance("Fido")
131131
fido.set_name("Rover")
132-
self.assertEqual(__, fido.get_name_from_instance())
133-
self.assertEqual(__, self.Dog3.get_name())
132+
self.assertEqual('Fido', fido.get_name_from_instance())
133+
self.assertEqual('Rover', self.Dog3.get_name())
134134

135135
def test_classes_and_instances_do_share_class_attributes(self):
136136
fido = self.Dog3()
137137
fido.set_name("Fido")
138-
self.assertEqual(__, fido.get_name())
139-
self.assertEqual(__, self.Dog3.get_name())
138+
self.assertEqual('Fido', fido.get_name())
139+
self.assertEqual('Fido', self.Dog3.get_name())
140140

141141
# ------------------------------------------------------------------
142142

@@ -151,13 +151,13 @@ def a_static_method():
151151
a_static_method = staticmethod(a_static_method)
152152

153153
def test_you_can_define_class_methods_without_using_a_decorator(self):
154-
self.assertEqual(__, self.Dog4.a_class_method())
154+
self.assertEqual('dogs class method', self.Dog4.a_class_method())
155155

156156
def test_you_can_define_static_methods_without_using_a_decorator(self):
157-
self.assertEqual(__, self.Dog4.a_static_method())
157+
self.assertEqual('dogs static method', self.Dog4.a_static_method())
158158

159159
# ------------------------------------------------------------------
160160

161161
def test_heres_an_easy_way_to_explicitly_call_class_methods_from_instance_methods(self):
162162
fido = self.Dog4()
163-
self.assertEqual(__, fido.__class__.a_class_method())
163+
self.assertEqual('dogs class method', fido.__class__.a_class_method())

0 commit comments

Comments
 (0)