Skip to content

Commit dd4f1e0

Browse files
committed
attribute access, deleting obiects
1 parent b7bd3be commit dd4f1e0

2 files changed

Lines changed: 26 additions & 26 deletions

File tree

python3/koans/about_attribute_access.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ class TypicalObject:
1515
def test_calling_undefined_functions_normally_results_in_errors(self):
1616
typical = self.TypicalObject()
1717

18-
with self.assertRaises(___): typical.foobar()
18+
with self.assertRaises(AttributeError): typical.foobar()
1919

2020
def test_calling_getattribute_causes_an_attribute_error(self):
2121
typical = self.TypicalObject()
2222

23-
with self.assertRaises(___): typical.__getattribute__('foobar')
23+
with self.assertRaises(AttributeError): typical.__getattribute__('foobar')
2424

2525
# THINK ABOUT IT:
2626
#
@@ -36,19 +36,19 @@ def __getattribute__(self, attr_name):
3636
def test_all_attribute_reads_are_caught(self):
3737
catcher = self.CatchAllAttributeReads()
3838

39-
self.assertRegex(catcher.foobar, __)
39+
self.assertRegex(catcher.foobar, 'could not be found')
4040

4141
def test_intercepting_return_values_can_disrupt_the_call_chain(self):
4242
catcher = self.CatchAllAttributeReads()
4343

44-
self.assertRegex(catcher.foobaz, __) # This is fine
44+
self.assertRegex(catcher.foobaz, "not be found") # This is fine
4545

4646
try:
4747
catcher.foobaz(1)
4848
except TypeError as ex:
4949
err_msg = ex.args[0]
5050

51-
self.assertRegex(err_msg, __)
51+
self.assertRegex(err_msg,'object is not callable')
5252

5353
# foobaz returns a string. What happens to the '(1)' part?
5454
# Try entering this into a python console to reproduce the issue:
@@ -59,7 +59,7 @@ def test_intercepting_return_values_can_disrupt_the_call_chain(self):
5959
def test_changes_to_the_getattribute_implementation_affects_getattr_function(self):
6060
catcher = self.CatchAllAttributeReads()
6161

62-
self.assertRegex(getattr(catcher, 'any_attribute'), __)
62+
self.assertRegex(getattr(catcher, 'any_attribute'), 'not be found')
6363

6464
# ------------------------------------------------------------------
6565

@@ -73,13 +73,13 @@ def __getattribute__(self, attr_name):
7373
def test_foo_attributes_are_caught(self):
7474
catcher = self.WellBehavedFooCatcher()
7575

76-
self.assertEqual(__, catcher.foo_bar)
77-
self.assertEqual(__, catcher.foo_baz)
76+
self.assertEqual('Foo to you too', catcher.foo_bar)
77+
self.assertEqual('Foo to you too', catcher.foo_baz)
7878

7979
def test_non_foo_messages_are_treated_normally(self):
8080
catcher = self.WellBehavedFooCatcher()
8181

82-
with self.assertRaises(___): catcher.normal_undefined_attribute
82+
with self.assertRaises(AttributeError): catcher.normal_undefined_attribute
8383

8484
# ------------------------------------------------------------------
8585

@@ -114,7 +114,7 @@ def test_getattribute_is_a_bit_overzealous_sometimes(self):
114114
catcher = self.RecursiveCatcher()
115115
catcher.my_method()
116116
global stack_depth
117-
self.assertEqual(__, stack_depth)
117+
self.assertEqual(11, stack_depth)
118118

119119
# ------------------------------------------------------------------
120120

@@ -135,17 +135,17 @@ def test_getattr_ignores_known_attributes(self):
135135
catcher = self.MinimalCatcher()
136136
catcher.my_method()
137137

138-
self.assertEqual(__, catcher.no_of_getattr_calls)
138+
self.assertEqual(0, catcher.no_of_getattr_calls)
139139

140140
def test_getattr_only_catches_unknown_attributes(self):
141141
catcher = self.MinimalCatcher()
142142
catcher.purple_flamingos()
143143
catcher.free_pie()
144144

145-
self.assertEqual(__,
145+
self.assertEqual('DuffObject',
146146
type(catcher.give_me_duff_or_give_me_death()).__name__)
147147

148-
self.assertEqual(__, catcher.no_of_getattr_calls)
148+
self.assertEqual(3, catcher.no_of_getattr_calls)
149149

150150
# ------------------------------------------------------------------
151151

@@ -166,13 +166,13 @@ def test_setattr_intercepts_attribute_assignments(self):
166166
fanboy.comic = 'The Laminator, issue #1'
167167
fanboy.pie = 'blueberry'
168168

169-
self.assertEqual(__, fanboy.a_pie)
169+
self.assertEqual('blueberry', fanboy.a_pie)
170170

171171
#
172172
# NOTE: Change the prefix to make this next assert pass
173173
#
174174

175-
prefix = '__'
175+
prefix = 'my'
176176
self.assertEqual("The Laminator, issue #1", getattr(fanboy, prefix + '_comic'))
177177

178178
# ------------------------------------------------------------------
@@ -194,17 +194,17 @@ def test_it_modifies_external_attribute_as_expected(self):
194194
setter = self.ScarySetter()
195195
setter.e = "mc hammer"
196196

197-
self.assertEqual(__, setter.altered_e)
197+
self.assertEqual('mc hammer', setter.altered_e)
198198

199199
def test_it_mangles_some_internal_attributes(self):
200200
setter = self.ScarySetter()
201201

202202
try:
203203
coconuts = setter.num_of_coconuts
204204
except AttributeError:
205-
self.assertEqual(__, setter.altered_num_of_coconuts)
205+
self.assertEqual(9, setter.altered_num_of_coconuts)
206206

207207
def test_in_this_case_private_attributes_remain_unmangled(self):
208208
setter = self.ScarySetter()
209209

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

python3/koans/about_deleting_objects.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ def test_del_can_remove_slices(self):
99
del lottery_nums[1]
1010
del lottery_nums[2:4]
1111

12-
self.assertEqual(___, lottery_nums)
12+
self.assertEqual([4,15,42], lottery_nums)
1313

1414
def test_del_can_remove_entire_lists(self):
1515
lottery_nums = [4, 8, 15, 16, 23, 42]
1616
del lottery_nums
1717

18-
with self.assertRaises(___): win = lottery_nums
18+
with self.assertRaises(NameError): win = lottery_nums
1919

2020
# ====================================================================
2121

@@ -48,8 +48,8 @@ def test_del_can_remove_attributes(self):
4848
except AttributeError as e:
4949
err_msg2 = e.args[0]
5050

51-
self.assertRegex(err_msg1, __)
52-
self.assertRegex(err_msg2, __)
51+
self.assertRegex(err_msg1, 'no attribute')
52+
self.assertRegex(err_msg2, 'no attribute')
5353

5454
# ====================================================================
5555

@@ -78,7 +78,7 @@ def test_del_works_with_properties(self):
7878
self.assertEqual('Senor Ninguno', cowboy.name)
7979

8080
del cowboy.name
81-
self.assertEqual(__, cowboy.name)
81+
self.assertEqual('The man with no name', cowboy.name)
8282

8383

8484
# ====================================================================
@@ -105,7 +105,7 @@ def test_another_way_to_make_a_deletable_property(self):
105105
self.assertEqual('Patrick', citizen.name)
106106

107107
del citizen.name
108-
self.assertEqual(__, citizen.name)
108+
self.assertEqual('Number Six', citizen.name)
109109

110110
# ====================================================================
111111

@@ -119,6 +119,6 @@ def __delattr__(self, attr_name):
119119

120120
def tests_del_can_be_overriden(self):
121121
sale = self.MoreOrganisedClosingSale()
122-
self.assertEqual(__, sale.jellies())
122+
self.assertEqual(5, sale.jellies())
123123
del sale.jellies
124-
self.assertEqual(__, sale.last_deletion)
124+
self.assertEqual('jellies', sale.last_deletion)

0 commit comments

Comments
 (0)